Flex and Bison Associativity difficulty
bison, boolean, flex-lexer, search
Solution
From bison docs:
Operator precedence is determined by the line ordering of the declarations; the higher the line number of the declaration (lower on the page or screen), the higher the precedence.
So in your case OR is lower on the screen and has higher precedence. Change the order to
%left OR
%left AND
(I haven't tested it though)
Problem
Using Flex and Bison, I have a grammar specification for a boolean query language, which supports logical "and", "or", and "not" operations, as well as nested subexpressions using "()". All was well until I noticed that queries like "A and B or C and D" which I'd like parsed as "(A & B) | (C & D)" was actually being interpreted as "A & ( B | ( C & D ) )". I'm nearly certain this is an associativity issue, but can't seem to find a proper explanation or example anywhere - that or I'm missing something important. Pertinent information from boolpars.y: ``` %token TOKEN %token OPEN_PAREN CLOSE_PAREN %right NOT %left AND %left OR %% query: expression { ... } ; expression: expression AND expression { ... } | expression OR expression { ... } | NOT expression { ... } | OPEN_PAREN expression CLOSE_PAREN { ... } | TOKEN { ... } ; ``` Can anyone find the flaw? I can't see why Bison isn't giving "or" appropriate precedence.