ANLTR precedence across multiple parser rules

Viewed 38

Consider the following grammar

grammar precedence;

s: expr EOF;
expr: '!' expr | expr '.' ID | ID;

ID: [a-zA-Z];
WS: [ \n] -> skip;

Given the precedence rules of this grammar, the expression !a.b should produce the following parse tree

Parse tree with (!a) being accessed

In an attempt to organize my grammar, I changed it to

grammar precedence;

s: expr EOF;
expr: not | expr '.' ID | ID;
not: '!' expr;

ID: [a-zA-Z];
WS: [ \n] -> skip;

When running it, we get the following parse tree

Parse three where a.b has the ! applied to it

The ANLTR reference states

ANTLR resolves ambiguities in favor of the alternative given first, implicitly allowing us to specify operator precedence.

However, the position of the not alternative has not changed, yet is given a different precedence. Why? Is there a different rule when there's a layer of indirection like this?

0 Answers
Related