Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

parsing - ANLTR precedence across multiple parser rules

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

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: [ 
] -> 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?

question from:https://stackoverflow.com/questions/66056215/anltr-precedence-across-multiple-parser-rules

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...