How to make ANTLR consume all visible elements?

前端 未结 1 926
醉酒成梦
醉酒成梦 2021-01-23 14:09

This is my grammar:

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


        
相关标签:
1条回答
  • 2021-01-23 14:40

    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.

    0 讨论(0)
提交回复
热议问题