Ambiguity and Precedence
List every parse tree of a string, then remove the ambiguity by rewriting the grammar or by declaring precedence and associativity.
- Lecture reference: Ambiguity, Precedence, Associativity & Top-Down Parsing
Grammar and string
Two parse trees: + at the root, or * at the root.
Which one do we want?
Show answer
The tree with + at the root, which groups the string as (int * int) + int: * has higher precedence than +. For the operand values 2 3 4 it computes 10; the tree with * at the root computes 14.
Parse trees every tree of the string
2 parse trees: the grammar is ambiguous
A grammar is ambiguous if it has more than one parse tree for some string. Lecture reference: Ambiguity, Precedence, Associativity & Top-Down Parsing
Tree 1
(int * int) + intLeftmost derivation
Tree 2
int * (int + int)Leftmost derivation
Open the grammar in Context-Free Grammars Open the grammar in Conflicts & Precedence
Dealing with ambiguity
Precedence cascade
Operator levels from lowest to highest precedence; the operators of a level are separated by
spaces (== != is two operators). Each level gets a non-terminal (E, T, F, …) that refers
to the next one; the last non-terminal derives the atoms.
- 1 E
- 2 T
Enter a rewritten grammar, or build one with the precedence cascade and choose “Use as the rewritten grammar”. The trees of the string under it appear here, next to the trees of the original grammar.
C operator precedence: 15 levels
C has 15 precedence levels; level 1 binds tightest. The C expression grammar is a precedence cascade with a non-terminal for each level. Lecture reference: Ambiguity, Precedence, Associativity & Top-Down Parsing
| Precedence | Operator | Description | Associativity |
|---|---|---|---|
| 1 | ++ -- | Postfix increment and decrement | Left-to-right |
| () | Function call | ||
| [] | Array subscript | ||
| . | Member access | ||
| -> | Member access through a pointer | ||
| (type){list} | Compound literal | ||
| 2 | ++ -- | Prefix increment and decrement | Right-to-left |
| + - | Unary plus and minus | ||
| ! ~ | Logical NOT and bitwise NOT | ||
| (type) | Cast | ||
| * | Indirection (dereference) | ||
| & | Address-of | ||
| sizeof | Size of | ||
| _Alignof | Alignment requirement | ||
| 3 | * / % | Multiplication, division, remainder | Left-to-right |
| 4 | + - | Addition and subtraction | Left-to-right |
| 5 | << >> | Bitwise left and right shift | Left-to-right |
| 6 | < <= | Less than, less than or equal | Left-to-right |
| > >= | Greater than, greater than or equal | ||
| 7 | == != | Equal, not equal | Left-to-right |
| 8 | & | Bitwise AND | Left-to-right |
| 9 | ^ | Bitwise XOR (exclusive or) | Left-to-right |
| 10 | | | Bitwise OR (inclusive or) | Left-to-right |
| 11 | && | Logical AND | Left-to-right |
| 12 | || | Logical OR | Left-to-right |
| 13 | ?: | Ternary conditional | Right-to-left |
| 14 | = | Assignment | Right-to-left |
| += -= | Assignment by sum and difference | ||
| *= /= %= | Assignment by product, quotient, remainder | ||
| <<= >>= | Assignment by left and right shift | ||
| &= ^= |= | Assignment by bitwise AND, XOR, OR | ||
| 15 | , | Comma | Left-to-right |