Search code examples
parsingantlr4operator-precedence

Preferring one alternative


An excerpt of my ANTLR v4 grammar looks like this:

expression:
    | expression BINARY_OPERATOR expression
    | unaryExpression
    | nularExpression
;
unaryExpression:
    ID expression
;

nularExpression:
    ID
    | NUMBER
    | STRING
;

My goal is to match the language without knowing all the necessary keywords and therefore I'm simply matching keywords as IDs.
However there are binary operators that take an argument to both sides of the keyword (e.g. keyword ) and therefore they need "special treatment". As you can see I already included this "special treatment" in the expression rule.

The actual problem now consists of the fact that some of these binary operators can be used as unary operators (=normal keywords) as well meaning that the left argument does not have to be specified.
The above grammar can't habdle this case because everytime I tried to implement this I ended up with every binary operator being consumed as a unary operator.

Example:
Let's assume count is a binary operator.
Possible syntaxes are <arg1> count <arg2> and count <arg>

All my attempts to implement the above mentioned case ended up grouping myArgument count otherArgument like (myArgument (count (otherArgument) ) ) instead of (myArgument) count (otherArgument)


My brain tellsme that the solution to this problem is to tell the parser always to take two arguments for a binary operator and if this fails it should try to consume the binary operator as an unary one.
Does anybody know how to accomplish this?


Solution

  • How about something like this:

    lower_precedence_expression
     : ID higher_precedence_expression
     | higher_precedence_expression
     ;
    
    higher_precedence_expression
     : higher_precedence_expression ID lower_precedence_expression
     | ID
     | NUMBER
     | STRING
     ;
    

    ?