如何删除这个 ANTLR 语法中的左递归?
我正在尝试解析 CSP(通信顺序进程)CSP 参考手册 。我定义了以下语法规则。
assignment
: IDENT '=' processExpression
;
processExpression
: ( STOP
| SKIP
| chaos
| prefix
| prefixWithValue
| seqComposition
| interleaving
| externalChoice
....
seqComposition
: processExpression ';' processExpression
;
interleaving
: processExpression '|||' processExpression
;
externalChoice
: processExpression '[]' processExpression
;
现在 ANTLR 报告
seqComposition
interleaving
externalChoice
是左递归的。有什么方法可以删除这个,或者我应该更好地使用 Bison Flex 来处理这种类型的语法。 (这样的规则还有很多)
I am trying to parse CSP(Communicating Sequential Processes) CSP Reference Manual. I have defined following grammar rules.
assignment
: IDENT '=' processExpression
;
processExpression
: ( STOP
| SKIP
| chaos
| prefix
| prefixWithValue
| seqComposition
| interleaving
| externalChoice
....
seqComposition
: processExpression ';' processExpression
;
interleaving
: processExpression '|||' processExpression
;
externalChoice
: processExpression '[]' processExpression
;
Now ANTLR reports that
seqComposition
interleaving
externalChoice
are left recursive . Is there any way to remove this or I should better used Bison Flex for this type of grammar. (There are many such rules)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义一个
processTerm
。然后编写如下规则:如果您希望仍然定义诸如 seqComposition 之类的内容,我认为这也可以。但您需要确保在执行规则时,
processExpansion
的解析始终会消耗更多文本。Define a
processTerm
. Then write rules looking likeIf you want to have things like
seqComposition
still defined, I think that would be OK as well. But you need to make sure that the parsing ofprocessExpansion
is going to always consume more text as you proceed through your rules.阅读 ANTLR wiki 上的删除左递归指南。这对我帮助很大。
Read the guide to removing left recursion in on the ANTLR wiki. It helped me a lot.