GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Demonstrate ambiguity with distinct parse trees

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Enumerate distinct parse trees for the supplied terminal string under the supplied grammar. The 2001 grammar S -> aSbS | bSaS | epsilon uses input abab; the 2002 arithmetic-expression grammar uses i+j*k.

Descriptive
Report issue

All parse trees for i+j*k in an ambiguous grammar

Parsing & Syntax Analysis

Question 22(a), GATE 2002: Grammar E→E+E | E*E | id. Original22(a): Construct all parse trees for i+j*k, where i,j,k are identifiers.
Question 22(a), GATE 2002: Grammar E→E+E | E*E | id. Original22(a): Construct all parse trees for i+j*k, where i,j,k are identifiers.
Question 22(a), GATE 2002: Grammar E→E+E | E*E | id. Original22(a): Construct all parse trees for i+j*k, where i,j,k are identifiers.
Tap to enlarge
Answer & solution

Answer: Two parse trees: +(i,*(j,k)) and *(+(i,j),k), with E→id at every identifier leaf. (no official key was published)

With productions E→E+E, E→E*E, E→id, the two full parse trees are:

Tree 1, root addition:
E
├── E
│   └── id(i)
├── +
└── E
    ├── E
    │   └── id(j)
    ├── *
    └── E
        └── id(k)

Tree 2, root multiplication

E
├── E
│   ├── E
│   │   └── id(i)
│   ├── +
│   └── E
│       └── id(j)
├── *
└── E
    └── id(k)

Here id(i), id(j), and id(k) denote the id tokens with those lexemes. Both yields are i + j * k. There are exactly two trees because the root split must be at one of the two operators, and each remaining two-identifier subtree has a unique possible split. Their grouping is i+(j*k) versus (i+j)*k.

Original paper, page 10

Descriptive
Report issue

Enumerate parse trees for abab

Parsing & Syntax Analysis

Question 18(b), GATE 2001: Grammar S→aSbS | bSaS | ε; construct all parse trees of abab and determine ambiguity.
Question 18(b), GATE 2001: Grammar S→aSbS | bSaS | ε; construct all parse trees of abab and determine ambiguity.
Tap to enlarge
Answer & solution

Answer: Exactly two parse trees; grammar is ambiguous. (no official key was published)

Step through it (2 derivations)

The root must use S→aSbS because the yield begins with a. Its explicit b can be the b at position 2 or position 4. These exhaust the possibilities. Use S[children] for an ordered parse-tree node; S[ε] is an empty production.

Tree 1: S[a, S[ε], b, S[a, S[ε], b, S[ε]]]
Tree 2: S[a, S[b, S[ε], a, S[ε]], b, S[ε]]

Accessible tree description: in tree 1 the root’s first S is empty and its final S generates ab. In tree 2 its first S generates ba and its final S is empty. Their leftmost derivations are:
S⇒aSbS⇒abS⇒abaSbS⇒ababS⇒abab.
S⇒aSbS⇒abSaSbS⇒abaSbS⇒ababS⇒abab.
The inner two-letter strings ab and ba each have only their direct production with both S children empty. Consequently there are precisely two trees, not merely at least two. Distinct trees with the same terminal yield prove ambiguity.

Original paper, page 10