Prove that the following grammar is ambiguous; S → a | S + S | S S | S* | ( S ) checking ambiguity in grammar. the subject of this question is compiler construction and automation. would you please help me find the answer we only have to figure out whether its ambiguous or not
Asked
Active
Viewed 6,753 times
2 Answers
2
To show the grammar is ambiguous, you must find a string derived by the grammar which has two or more distinct parse trees. I think we have a few options here. One that should work is S + S*:
S
/
/
S S--+
/ / \
/ / \
S--+ S S
\ \
\ \
S--S *
\
\
*
These derivations are fundamentally different (not just different orderings of the same derivation) and so the string S + S* is fundamentally ambiguous. Does it mean (S + S)* or does it mean S + (S*)?

Patrick87
- 12,824
- 1
- 44
- 76
2
A grammar is ambiguous if it can generate a string in more than two ways, i.e. a string generated by the grammar does not have a unique parse tree.
The grammar you have can generate the string a + a + a
by the following parse trees.
S S
/ | \ / | \
/ | S S | \
/ | | | | \
/ | | | | \
S | | | | S
/ | \ | | | | / | \
S | S | | | | S | S
| | | | | | | | | |
a + a + a a + a + a
This shows that the grammar is ambiguous.

tylerh111
- 392
- 1
- 7