Reputation: 1038
I'm writing a Bison inspired parsing framework, and there's something in the Bison manual that I don't really understand.
From the Bison manual 5.3.5 How Precedence Works:
...each rule gets its precedence from the last terminal symbol mentioned in the components.
and
Not all rules and not all tokens have precedence. If either the rule or the lookahead token has no precedence, then the default is to shift.
As far as I can understand, this comes down to:
But wouldn't that mean that a rule that doesn't contain any terminal symbol with a declared precedence would never be reduced? What am I missing?
For example, the below grammar:
S -> E + E
E -> foo1
E -> foo2
Gives a dfa with seven states, the first two are:
When parsing the string foo1 + foo2, the parser shifts the token foo1 and ends up in state 1. The next lookahead token is +, neither '+' nor the rule E -> foo1 has any declared precedence, which means the parser should shift, but there is no such edge in state 1, which leads the parser to return a syntax error.
Thanks in advance
Upvotes: 1
Views: 184
Reputation: 241901
Precedence rules only apply in the case of ambiguity; that is to say, if there is a shift/reduce conflict. Absent any conflict, the parser will shift or reduce based on the grammar.
Upvotes: 3