15
Lecture # 9 Chap 4: Ambiguous Grammar

Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

Embed Size (px)

Citation preview

Page 1: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

Lecture # 9

Chap 4: Ambiguous Grammar

Page 2: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

2

Chomsky Hierarchy: Language Classification

• A grammar G is said to be– Regular if it is right linear where each production is of the

formA w B or A w

or left linear where each production is of the formA B w or A w

– Context free if each production is of the formA

where A N and (NT)*– Context sensitive if each production is of the form

A where A N, ,, (NT)*, || > 0

– Unrestricted

Page 3: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

3

Chomsky Hierarchy

L(regular) L(context free) L(context sensitive) L(unrestricted)

Where L(T) = { L(G) | G is of type T }That is: the set of all languages

generated by grammars G of type T

L1 = { anbn | n 1 } is context free

L2 = { anbncn | n 1 } is context sensitive

Every finite language is regular! (construct a FSA for strings in L(G))

Examples:

Page 4: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

4

Parse Trees

• The root of the tree is labeled by the start symbol• Each leaf of the tree is labeled by a terminal (=token)

or • Each interior node is labeled by a nonterminal• If A X1 X2 … Xn is a production, then node A has

immediate children X1, X2, …, Xn where Xi is a (non)terminal or ( denotes the empty string)

Page 5: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

5

Parse Tree for the Example Grammar

Parse tree of the string 9-5+2 using grammar G

list

digit

9 - 5 + 2

list

list digit

digitThe sequence ofleafs is called the

yield of the parse tree

Page 6: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

Example of Parse Tree

• Suppose we have the following grammar E → E + E E → E * E E → ( E ) E → - E E → id

Perform Left most derivation, right most derivation and construct a parse tree for the string

id+id*id

Page 7: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

Two possible Parse Trees using Leftmost derivation

Page 8: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

Parse Tree via Right most derivation

Page 9: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

Ambiguity

• Grammar is ambiguous if more than one parse tree is possible for some string as shown in the previous example. If there are more than one left most derivations or more than one right most derivations.

• Ambiguity is not acceptable– Unfortunately, it’s undecidable to check whether a given

CFG is ambiguous– Some CFLs are inherently ambiguous (do not have an

unambiguous CFG)

Page 10: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

10

Ambiguity (cont’d)

string string + string | string - string | 0 | 1 | … | 9

G = <{string}, {+,-,0,1,2,3,4,5,6,7,8,9}, P, string>

with production P =

Consider the following context-free grammar:

This grammar is ambiguous, because more than one parse treerepresents the string 9-5+2

Page 11: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

11

Two Parse Trees for the same string

string

string

9 - 5 + 2

string

string string

string

string

9 - 5 + 2

string

string string

Page 12: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

Practice

• Show that the following grammar is ambiguous: (Find out strings and two parse trees)

1) S AB | aaB 2) S a | abSb |aAb A a | Aa A bS | aAAb Bb

3) S aSb | SS | ε

Page 13: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

How to eliminate Ambiguity?

• If ambiguity is of the form: S α S β S | α1 |……| αn Rewrite: S α S β S’ | S’ S’ α1 |……| αn• Make the following grammar unambiguous: E E+E | E*E | (E) | id

Page 14: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

Eliminate Left Recursion

If S S α for some α then there exists left recursion

If there are productions: S S α | β Rewrite: S β S’ S’ α S’ |

Page 15: Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each

Example

• Remove Left Recursion: S A A S d | b