javacc学习总结

45 篇文章 1 订阅
3 篇文章 1 订阅

       在学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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值