javacc学习总结

来源:互联网 发布:js中怎么获取name属性 编辑:程序博客网 时间:2024/06/10 03:53

       在学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 :{  " "| "\t"| "\n"| "\r"}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”在程序里面已经是“关键字”了,是和ID并行的,就像java程序里面的new关键字和变量名字

 


 

1 0
原创粉丝点击