Ambiguous call expression in ANTLR4 grammar

前端 未结 1 1585
星月不相逢
星月不相逢 2020-12-12 03:10

I have a simple grammar (for demonstration)

grammar Test;


program
    :   expression* EOF
    ;


expression
    :   Identifier
    |   expression \'(\' ex         


        
相关标签:
1条回答
  • 2020-12-12 03:17

    The solution to this is to temporary "unhide" whitespace in your second alternative. Have a look at this question for how this can be done.

    With that solution your code could look somthing like this

    expression
        :   Identifier
        |   {enableWS();} expression '(' {disableWS();} expression? ')'
        |   '(' expression ')'
        ;
    

    That way the second alternative matches the input WS-sensitive and will therefore only be matched if the identifier is directly followed by the bracket.

    See here for the implementation of the MultiChannelTokenStream that is mentioned in the linked question.

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