如何使用递归使 ANTLR 消耗所有可用元素? [英] How to make ANTLR consume all available elements using recursion?

查看:21
本文介绍了如何使用递归使 ANTLR 消耗所有可用元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的语法:

grammar test;

text: foo EOF;

foo:
    'X'
    |
    '('
    foo
    ')'
    |
    foo
    '!'
    |
    foo
    tail
    ;

tail: (' ' foo)+;

这是它完美解析的输入:

This is the input it perfectly parses:

X (X! (X)! (X X X)!!!) X

然而,输出树有太多 tail 元素,正如我之前解释的 此处.可以解决这个问题吗?

However, the output tree has too many tail elements, as I explained earlier here. Is it possible to fix this?

推荐答案

感谢@kaby76,找到了解决方案:

Thanks to @kaby76, the solution is found:

foo:  
  'X' tail? 
  | 
  '(' foo ')' tail? 
  | 
  foo '!' tail? 
  ; 
tail: 
  (
    ' ' 'X' 
    | 
    ' ' '(' foo ')' 
    | 
    ' ' foo '!'
  )+ 
  ;

这篇关于如何使用递归使 ANTLR 消耗所有可用元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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