Ambiguous call expression in ANTLR4 grammar

2019-01-20 16:48发布

问题:

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.