-2

Sorry for such a dumb question, I have to read a file (hello_world.txt) and printline it. I'm trying not to use the HELLO_WORLD token, but im not doing well, so I've comment my attempts. How can i read it using as STR token?

Thanks.

This the simple file.

    begin
    write "Hello World!"
    end

Here is the code:

    /**
    * JavaCC template file created by SF JavaCC plugin 1.5.28+ wizard for JavaCC 1.5.0+
     */
    options
    {
      static = true;
      //DEBUG_PARSER = true;
      // DEBUG_TOKEN_MANAGER = true;
     }

    PARSER_BEGIN(CJComp)
    package DefinirSintaxe;
    import java.io.*;
    public class CJComp
    {
      public static void main(String args []) throws ParseException
      {
        CJComp analiser = null;  
        try {      
              analiser = new CJComp(new FileInputStream("hello_world.txt"));
              analiser.start();      
            }
       catch(FileNotFoundException e) 
            {
               System.out.println("Error: not found");
            }
             catch(TokenMgrError e) {
              System.out.println("Erro léxico\n" + e.getMessage());
            }
             catch(ParseException e) {
              System.out.println("Erro sintático\n" + e.getMessage());
            }
      }    
    }

    PARSER_END(CJComp)

    SKIP : { " " | "\t" | "\r" | "\n" }

    SKIP :
    {
      "//" : COMMEMT_LINE
    }
   <COMMEMT_LINE>
   SKIP: 
   {
     "\n" : DEFAULT | < ~["\n"] >
   }

   TOKEN : 
   {

    //  < STR : (["A"-"Z","a"-"z"])(["A"-"Z","a"-"z","0"-"9"])* > |
    //  < CMD_BEGIN    : ("begin") (< STR >)* | (< CMD_WRITE >) > |
    //  < CMD_END      : (< STR>)* "end"    > |    
    //  < CMD_WRITE    : (<STR >) *  ("write" )(<STR >)* >                             


   < CMD_BEGIN   : begin" > |
   < CMD_WRITE   : "whrite"> |
   < CMD_END     : "end"    > |
   < HELLO_WORLD   : "Hello World!" >


   }

   void inicio() :{}
   {
      ( CommandLine() )*
    }

   void ComandLine():{}
   {    
      ( write())
   }

   void write(): {Token t1 = null,  t2 = null,  t3 = null,  t4 = null;}
   {
      //t1 = <CMD_BEGIN> <CMD_WRITE> <STR> <CMD_END>    

      t1 = <CMD_BEGIN> 
      t2 = <CMD_WRITE>
      t3 = <HELLO_WORLD>
      t4 = <CMD_END>   

     {
      System.out.println(t3.image) ;
     }
   }
Marisco
  • 125
  • 1
  • 3
  • 16
  • I'm voting to close this question as off-topic because it is not in English. – J0e3gan Sep 08 '15 at 00:43
  • 1
    It's hard to understand exactly what you are trying to do. Please explain further. And BTW @J0e3gan, that isn't a valid reason for a vote to close ( not saying it shouldn't be closed). By your logic, if the user learned English and named his variables and wrote his comments in English, it would be a fine question? – CaptJak Sep 08 '15 at 00:49
  • i'll redo the question thanks. – Marisco Sep 08 '15 at 00:51
  • @CaptJak: It's the site's logic. Questions and answers on SO are to be in English. I have always been happy to help improve questions and answers where posters are clearly not native/fluent English speakers; I try to be helpful, which is where I sense you are coming from as well; but there is little others can do with a question that is not in English at all. See [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [Jon Skeet's question checklist](http://meta.stackoverflow.com/q/260648/1810429) for references to English being the expected language on SO. – J0e3gan Sep 08 '15 at 01:01
  • 1
    Thank's again @ J0e3gan, i do apreciate you help, maybe now its a little bit more clear than before. if not no biggie, just its realy hard to find help on non english forum... thanks – Marisco Sep 08 '15 at 01:07

1 Answers1

0

Found a solution by recognizing quoted string on STR > TOKEN.

        /**
         * JavaCC template file created by SF JavaCC plugin 1.5.28+ wizard for JavaCC 1.5.0+
         */
        options
        {
          static = true;
         // DEBUG_PARSER = true;
         // DEBUG_TOKEN_MANAGER = true;
        }

        PARSER_BEGIN(CJComp)
        package DefinirSintaxe;
        import java.io.*;
        public class CJComp
        {
          public static void main(String args []) throws ParseException
          {

            CJComp analiser = null;  
              try {      
                 analiser = new CJComp(new FileInputStream("hello_world.cjc"));
                 analiser.begin();      
              }
              catch(FileNotFoundException e) {
                 System.out.println("Error: file not found!");
              }
              catch(TokenMgrError e) {
                 System.out.println("lexicon error:\n" + e.getMessage());
              }
              catch(ParseException e) {
                 System.out.println("syntactic error:\n" + e.getMessage());
              }
           }    
        }

        PARSER_END(CJComp)

        SKIP : { " "  |
               "\t" |
               "\r" |
                "\n" }



        TOKEN : 
        {

          <STR          : "\"" (~["\"", "\\"] | "\\\"" | "\\\\")* "\"" >|
          < CMD_BEGIN   : ("begin")> |
          < CMD_END     : ("end")   > |    
          < CMD_WRITE   : ("write" ) >

        }

        void begin() :{}
        {
         write()
        }



        void write(): {Token t1 = null,  t2 = null,  t3 = null,  t4 = null;}
        {
           t1 = <CMD_BEGIN> 
           t2 = <CMD_WRITE> 
           t3 = <STR>
           t4 = <CMD_END>   

            {
             System.out.println(t3.image) ;
            }
        }
Marisco
  • 125
  • 1
  • 3
  • 16