解决ANTLR相互左递归规则 [英] Solving ANTLR Mutually left-recursive rules

查看:203
本文介绍了解决ANTLR相互左递归规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的ANTLR语法中的"expr"规则显然是相互左递归的.作为ANTLR的新手,很难解决这个问题.我已经阅读了ANTLR参考书中的解决非LL(*)冲突",但是仍然看不到解决方案.有指针吗?

the 'expr' rule in the ANTLR grammar below obviously mutually left-recursive. As a ANTLR newbie it's difficult to get my head around solving this. I've read "Resolving Non-LL(*) Conflicts" in the ANTLR reference book, but I still don't see the solution. Any pointers?


LPAREN : ( '(' ) ;
RPAREN : ( ')' );
AND : ( 'AND' | '&' | 'EN' ) ;
OR : ( 'OR' | '|' | 'OF' );
NOT : ('-' | 'NOT' | 'NIET' );
WS :  ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;}  ;
WORD :  (~( ' ' | '\t' | '\r' | '\n' | '(' | ')' | '"' ))*;

input : expr EOF;
expr : (andexpr | orexpr | notexpr | atom);
andexpr : expr AND expr;
orexpr :  expr OR expr;
notexpr : NOT expr;
phrase : '"' WORD* '"';
atom : (phrase | WORD); 

推荐答案

我建议看看antlr网站上的示例语法. Java语法可以满足您的要求.

I would suggest to have a look at the example grammers on the antlr site. The java grammar does what you want.

基本上,您可以执行以下操作:

Basicly you can do something like this:

expr : andexpr;
andexpr : orexpr (AND andexpr)*;
orexpr : notexpr (OR orexpr)*;
notexpr : atom | NOT expr;

关键是,每个表达式都可以以一个原子结尾.

The key is, that every expression can end to be an atom.

这篇关于解决ANTLR相互左递归规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆