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 :
{
  " "
| "\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”在程序里面已经是“keyword”了。是和ID并行的,就像java程序里面的newkeyword和变量名字

 


 

posted @ 2016-04-11 19:37  blfshiye  阅读(234)  评论(0编辑  收藏  举报