How to make ANTLR consume all visible elements?

邮差的信 提交于 2020-12-26 11:06:20

问题


This is my grammar:

grammar test;
text: foo EOF;
foo:
    'X'
    |
    foo
    '!'
    |
    foo
    '?'
    |
    foo
    tail
    ;
tail: (' ' foo)+;

I'm parsing this text:

X? X! X X

This is the tree I'm getting:

What should change in the grammar so that I get only one tail element with a collection of all foo elements inside?

In the real world the task is way more complex, and using only a scanner is no solution to it.


回答1:


As far as I can tell, what you want is this:

item: 'X' ('!' | '?')*;
// Alternatively to get a tree per operator instead of a list of operators:
// item
//   : 'X'
//   | item '!'
//   | item '?'
//   ;
foo: item (' ' item)*;

Maybe this, if you want the tail still to have its own node in the tree:

item: 'X' ('!' | '?')*;
foo: item tail;
tail: (' ' item)*;

The reason that your version only gave you 1-item lists is that the mutual recursion between foo and tail consumed all the items, so there's nothing left for the repetition to consume.

Generally when you have something that can be repeated, you either want to implement this using */+ (if you want lists in the resulting tree) or using recursion (if you want a more tree-like tree) - not both.



来源:https://stackoverflow.com/questions/64634034/how-to-make-antlr-consume-all-visible-elements

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