Syntax analysis

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 · slides 2–19

Grammar and string

Terminals of the grammar, separated by spaces.
One number for each int of the string, in order.
OTHER = E shows the k-th OTHER as E with the subscript k.
Lecture reference: Ambiguity, Precedence, Associativity & Top-Down Parsing · slide 5

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 · slide 6

Tree 1

(int * int) + int
Bracket form
E( E( E(int) * E(int) ) + E(int) )
Leftmost derivation
Values
(2 * 3) + 4 = 10

Tree 2

int * (int + int)
Bracket form
E( E(int) * E( E(int) + E(int) ) )
Leftmost derivation
Values
2 * (3 + 4) = 14

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. 1 E
  2. 2 T
F
Alternatives of the atoms are separated by |; E is the start symbol.
Read only.

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 · slide 9

C operator precedence
PrecedenceOperatorDescriptionAssociativity
1++ --Postfix increment and decrementLeft-to-right
()Function call
[]Array subscript
.Member access
->Member access through a pointer
(type){list}Compound literal
2++ --Prefix increment and decrementRight-to-left
+ -Unary plus and minus
! ~Logical NOT and bitwise NOT
(type)Cast
*Indirection (dereference)
&Address-of
sizeofSize of
_AlignofAlignment requirement
3* / %Multiplication, division, remainderLeft-to-right
4+ -Addition and subtractionLeft-to-right
5<< >>Bitwise left and right shiftLeft-to-right
6< <=Less than, less than or equalLeft-to-right
> >=Greater than, greater than or equal
7== !=Equal, not equalLeft-to-right
8&Bitwise ANDLeft-to-right
9^Bitwise XOR (exclusive or)Left-to-right
10|Bitwise OR (inclusive or)Left-to-right
11&&Logical ANDLeft-to-right
12||Logical ORLeft-to-right
13?:Ternary conditionalRight-to-left
14=AssignmentRight-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,CommaLeft-to-right