When I compile the following ANTLR grammar file,
conditional_expression
: (logical_or_expression -> logical_or_expression) ('?' expression ':' rhs=conditional_expression -> ^('?' $conditional_expression expression $rhs))?
;
I get the following error message.
error(132): nesC.g:769:109: reference $conditional_expression is
ambiguous; rule conditional_expression is enclosing rule and
referenced in the production (assu ming enclosing rule)
Can anybody tell me the solution to this? Thanks. 🙂
ANTLR cannot decide what you mean by
$conditional_expression: the rule itself, or the one you labeled$rhs.To get it working as you now tried, you’ll need to move
rhs=conditional_expressionto a rule of its own, in which case there is no ambiguous naming:But wouldn’t this also do the trick for you:
or:
?