
A grammar is ambiguous if a single string can have more than one parse tree (or derivation) .
A grammar is ambiguous if a single string can have more than one parse tree (or derivation).
E β E + E | E * E | id
id + id * id
This string can be interpreted in two different ways:
(id + id) * id
id + (id * id)
E
/ | \
E * E
/|\ |
E + E id
| |
id id
E
/ | \
E + E
| /|\
id E * E
| |
id id
π Same input β Different meanings β β Ambiguity
Associativity defines the order in which operators of the same precedence are evaluated.
π Evaluation from left to right
a - b - c
π Interpreted as:
(a - b) - c
+, -, *, /π Evaluation from right to left
a = b = c
π Interpreted as:
a = (b = c)
=)^)Precedence defines which operator is evaluated first when different operators appear together.
a + b * c
π Multiplication has higher precedence:
a + (b * c)
| Level | Operator | Meaning |
|---|---|---|
| High | ^ |
Power |
| Medium | *, / |
Multiply/Divide |
| Low | +, - |
Add/Subtract |
| Lowest | = |
Assignment |
E β E + E | E * E | id
E β E + T | T
T β T * F | F
F β id
* has higher precedence than +Ambiguity β Resolved by β Precedence + Associativity
a + b * c - d
* first β b * c+ and - left to right((a + (b * c)) - d)
π Frequently asked:
| Aspect | Ambiguity | Associativity | Precedence |
|---|---|---|---|
| Definition | Multiple parse trees | Order for same operators | Priority of operators |
| Purpose | Identify problem | Resolve order | Resolve priority |
| Example | id + id * id | a - b - c | a + b * c |
| Type | β | Left/Right | Levels |
| Role | Problem | Solution | Solution |
| Importance | Very High | Very High | Very High |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.