How to make ANTLR consume all available elements using recursion?

孤者浪人 提交于 2021-01-07 02:28:26

问题


This is my grammar:

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

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


回答1:


Thanks to @kaby76, the solution is found:

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


来源:https://stackoverflow.com/questions/64635366/how-to-make-antlr-consume-all-available-elements-using-recursion

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!