Ambiguous call expression in ANTLR4 grammar

耗尽温柔 提交于 2019-11-27 08:25:52

问题


I have a simple grammar (for demonstration)

grammar Test;


program
    :   expression* EOF
    ;


expression
    :   Identifier
    |   expression '(' expression? ')'
    |   '(' expression ')'
    ;


Identifier
    :   [a-zA-Z_] [a-zA-Z_0-9?]*
    ;

WS
    :   [ \r\t\n]+ -> channel(HIDDEN)
    ;

Obviously the second and third alternatives in the expression rule are ambiguous. I want to resolve this ambiguity by permitting the second alternative only if an expression is immediately followed by a '('.

So the following

bar(foo)

should match the second alternative while

bar
(foo)

should match the 1st and 3rd alternatives (even if the token between them is in the HIDDEN channel).

How can I do that? I have seen these ambiguities, between call expressions and parenthesized expressions, present in languages that have no (or have optional) expression terminator tokens (or rules) - example


回答1:


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.



来源:https://stackoverflow.com/questions/45504124/ambiguous-call-expression-in-antlr4-grammar

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