zoukankan      html  css  js  c++  java
  • javacc学习总结

           在学javacc的时候。发现一个问题,见下:

    Example.jj文件

    PARSER_BEGIN(Example)
    
    public class Example {
    
      public static void main(String args[]) throws ParseException {
        Example parser = new Example(System.in);
        parser.basic_expr();
      }
    
    }
    
    PARSER_END(Example)
    
    SKIP :
    {
      " "
    | "	"
    | "
    "
    | "
    "
    }
    
    void basic_expr() :
    {}
    {
      <ID> {System.out.println("got 333");} "(" expr() ")"
    |
      "(" expr() ")"
    |
      "weichaofan"{System.out.println("got 111");} <ID>{System.out.println("got 222");} 
    
    |
    
      "weichao"{System.out.println("got 44");} <ID>{System.out.println("got 55");} 
    
    }
    
    void expr() :
    {}
    {
      "TBD"
    }
    
    TOKEN [IGNORE_CASE] :
    {
      <ID: (["a"-"z"])+>
    }
    

    看官网文档,有一下重要说明:
    1、若jj文件里含有冲突。则会提示。例如以下图所看到的。


    In situations where it does not work well, Java Compiler Compiler provides you with warning messages like the ones shown above.
    If you have a grammar that goes through Java Compiler Compiler without producing any warnings, then the grammar is a LL(1) grammar. Essentially, LL(1) grammars are those that can be handled by top-down parsers (such as those generated by Java Compiler Compiler) using at most one token of LOOKAHEAD.
    解释:
        假设jj文件里出现警告信息,则说明不能正常执行,假设jj文件没有警告信息,则说明此是LL(1)文法。LL(1)文法可以从上而下解析最多仅仅用lookahead 1 个token。

     

     

    疑问


        看上面样例,ID是否包括符号“weichaofan”和“weichao”?

     

     

    看样例:



        经过拿样例验证。得出下面结论:


       1、“weichaofan”和“weichao”不是ID,即ID已经不包括这两个符号。
       2、语法分析程序在选择时,首先依据已经确定的,然后再匹配,在这个样例中匹配顺序例如以下(“(”,“weichaofan”。“weichao”。ID)。


       假设把这两个结论合成一个,那就是“weichaofan”和“weichao”在程序里面已经是“keyword”了。是和ID并行的,就像java程序里面的newkeyword和变量名字


     

  • 相关阅读:
    对于es6新增的async 和 await的使用和理解
    如何实现文本的竖向排版 && js 打印指定区域
    鼠标透过蒙版点击触发下一层事件,上一层点击事件失效
    小程序的弊端以及Taro优点
    服务器端渲染和客户端渲染区别
    使用mobx 示例
    C语言
    C语言
    C语言
    [转]linux tar 解压命令总结
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5379836.html
Copyright © 2011-2022 走看看