First time here? Checkout the FAQ!
0 votes
An unambiguous grammar has same leftmost and rightmost derivation

Please provide an example to show this.
asked in Compiler Design by Junior (775 points) 2 10 36 | 166 views

1 Answer

+1 vote

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:



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:


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.

answered by Loyal (4k points) 6 12 27

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23338 Points

  2. Bikram

    17048 Points

  3. Habibkhan

    7912 Points

  4. srestha

    6228 Points

  5. Debashish Deka

    5438 Points

  6. jothee

    4968 Points

  7. Sachin Mittal 1

    4772 Points

  8. joshi_nitish

    4286 Points

  9. sushmita

    3964 Points

  10. Rishi yadav

    3794 Points

Recent Badges

Popular Question makhdoom ghaya
Popular Question junaid ahmad
Notable Question learner_geek
Notable Question jothee
Popular Question jothee
Notable Question Jeffrey Jose
Notable Question air1ankit
Nice Question jothee
Verified Human shaleen25
Popular Question jothee
27,290 questions
35,142 answers
33,231 users