Ambiguity 1010

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Ambiguity 1010 as PDF for free.

More details

  • Words: 249
  • Pages: 2
AMBIGUITY IN GRAMMAR 04CS1010 N. Raghu Ram A grammar is said to be ambiguous if it produces more than one parse tree for some sentence in the grammar. For general parsing , a grammar should be made unambiguous , for if it is ambiguous the parser cannot uniquely determine which parse tree to select for a given sentence. For example consider the following grammar that is ambiguous . stmt → if cond then stmt | if cond then stmt else stmt The above stated grammar is ambiguous as associating else with an if will cause ambiguity.For example the sentence if C1 then E1 if C2 then S1 else S2 can lead to two parse trees which are show below stmt if

cond C1

then

stmt

if

cond

then

C2

stmt

S1

else

stmt

S2

stmt if

cond C1

then if

stmt cond C2

else then

stmt stmt

S2

S1

Thus from the above parse trees it can be seen that the above grammar is ambiguous. ambiguity in the above grammar can be eliminated by associating the else with the closest if. Then the grammar can be written as stmt → ustat | mstat mstat → if cond then mstat else mstat ustat → if cond then stmt | if cond then mstat else ustat

The grammar is broken in to two pieces matched statements and unmatched statements. Matched statements contain as many if 's as else 's and unmatched statements contain greater number of if 's than else 's.

Related Documents

Ambiguity 1010
November 2019 15
1010
October 2019 32
1010
June 2020 23
Anaphoric Ambiguity
November 2019 9
Pr 1010
June 2020 18
Miller 1010
April 2020 22