JavaCC的Hello World

时间:2015-09-08 00:33:05

标签: javacc

抱歉这么愚蠢的问题, 我必须读取一个文件(hello_world.txt)并打印它。 我试图不使用HELLO_WORLD令牌,但我做得不好,所以我评论了我的尝试。 如何使用STR令牌读取它?

感谢。

这是一个简单的文件。

    begin
    write "Hello World!"
    end

以下是代码:

    /**
    * 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) ;
     }
   }

1 个答案:

答案 0 :(得分:0)

通过识别STR&gt;上的引用字符串找到解决方案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) ;
            }
        }