Redirected
retagged by
1,865 views
0 votes
0 votes
An unambiguous grammar has same leftmost and rightmost derivation

Please provide an example to show this.
retagged by

1 Answer

2 votes
2 votes

From Wikipedia:

An ambiguous grammar is a context-free grammar for which there exists a string that can have more than one leftmost derivation, while an unambiguous grammar is a context-free grammar for which every valid string has a unique leftmost derivation.

For a grammar to be unambiguous, they should generate same parse trees, which is only possible if there is unique LMD (and unique RMD) but it's not necessary for LMD and RMD to be same.

So your conclusion that LMD and RMD have to be same for a grammar to be unambiguous, is incorrect.

Consider the grammar:

S→AS|a

A→a

Now string "aa" can have different LMD and RMD:

RMD: S→AS→Aa→aa

LMD: S→AS→aS→aa

But they generate the same parse tree (because the generation of any string has a unique LMD and a unique RMD). So the language is unambiguous.

Now consider the following grammar:

S→SS|a

Now string "aaa" has two LMD derivations:

LMD1: S -> SS -> SSS -> aSS -> aaS -> aaa

LMD2: S -> SS -> aS -> aSS -> aaS -> aaa

So there will be two different parse trees, which you can confirm by drawing and hence the compiler will get confused which parse tree to follow in order to generate the string. So this grammar is ambiguous.

Notice that both grammars generate the same language.

Related questions

1 votes
1 votes
1 answer
1
0 votes
0 votes
5 answers
2
Abhishek Malik asked Apr 5, 2018
1,228 views
Total number of Handles for $(w=aa)$ in the following grammar ?$S\rightarrow DT$$D\rightarrow aa$$T\rightarrow \epsilon$
0 votes
0 votes
1 answer
3
1 votes
1 votes
1 answer
4
Hira Thakur asked Nov 16, 2015
611 views
how to verify weather given grammer is ambigious or not???