Consider the following grammar
grammar precedence;
s: expr EOF;
expr: '!' expr | expr '.' ID | ID;
ID: [a-zA-Z];
WS: [
] -> skip;
Given the precedence rules of this grammar, the expression !a.b
should produce the following parse tree
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: [
] -> skip;
When running it, we get the following parse tree
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?
question from:
https://stackoverflow.com/questions/66056215/anltr-precedence-across-multiple-parser-rules 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…