All parse trees for i+j*k in an ambiguous grammar
Parsing & Syntax Analysis



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.

