On 8/9/16, Kraus, Stefan <[email protected]> wrote: > Hello, > > i have a question regarding the LEMON parser generator: > > I am writing an expression grammar. The token LT means "<", while GT means > ">". > When i use this grammar, everything works as expected: > > %nonassoc LT GT . > ex ::= ex LT ex . > ex ::= ex GT ex . > > There are no conflicts, and the non-associativity is handled correctly, e.g. > the expression "2 < 3 < 4" is reported as a syntax error. > > However, changing the grammar to > > %nonassoc COMPARISON . > ex ::= ex LT ex . [COMPARISON] > ex ::= ex GT ex . [COMPARISON] > > does not work as i expected. First, the expression "2 < 3 < 4" is not > reported as a syntax error, but is processed instead. Moreover, parsing > conflicts are reported between LT and GT. I thought that both versions > should be equivalent?
The two grammars are *not* equivalent. Precedence can be associated with both tokens and rules. The %nonassoc directive assigns a precedence to a token. The [ID] construct assigns a precedence to a rule. Any rule that lacks the [...] construct (which is to say, most rules) is assigned the precedence of the left-most token in the rule. When a SHIFT/REDUCE conflict occurs, the conflict is resolved by comparing the precedence of the SHIFT token against the precedence of the REDUCE rule. In the first grammar above, both the SHIFT token and the REDUCE rule have NONASSOC precedence, which means the SHIFT/REDUCE conflict is resolved by generating a syntax error, which is what you want. But in the second grammar, the LT and GT tokens have no precedence. And hence the SHIFT/REDUCE conflict resolution rules cannot be applied. A SHIFT/REDUCE conflict is reported. -- D. Richard Hipp [email protected] _______________________________________________ sqlite-users mailing list [email protected] http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

