12
1 Efficient Parsing and Ambiguity

1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

Embed Size (px)

Citation preview

Page 1: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

1

Efficient Parsing and Ambiguity

Page 2: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

Ambiguity

A grammar G is ambiguous if– There exists w ∊ L(G) such that– There are two parse trees for w

Allows multiple interpretations of meaning of w Removing ambiguity: not always possible Inherently Ambiguous:

– L is inherently ambiguous if– For all grammars G such that L(G) = L– G is ambiguous

s-grammars are always unambiguous

2

Page 3: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

3

Simple Grammar (s-grammar)

G = (V,T,S,P) is a simple grammar if All rules in G are of the form A -> ax

– A V– a T– x V*

For each A V and each a T there is at most one A -> ax rule.

Page 4: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

4

Parsing

Given a grammar G and string w find derivation of w in G (if it exists.)

Brute force parsing – Go through all possibilities of a left-most derivation until the derivation of w is found.

Page 5: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

Example

G = ({S,E,A,I}, {a,b}, P, S), where P containsS -> ES | EE -> aIb | AA -> aA | bA | a | bI -> b |

Is aabb L(G)?

5

Page 6: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

6

Creating Parse Tree-Backtracking

S

E S

a bI

Stuck, backtrack one step

Page 7: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

7

Creating Parse Tree-Backtracking

S

E S

A

Stuck, backtrack one step

a A

a A

b A

b A

Page 8: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

8

Creating Parse Tree-Backtracking

S

E S

A

Stuck, backtrack one step

a A

a A

b A

b

E S

A

No other choices, backtrack another

step

Page 9: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

9

Creating Parse Tree-Backtracking

S

E S

A

Stuck, backtrack one step

a A

a A

b A

b

No other choices, backtrack another

stepE

A

No other choices, backtrack another

step

Page 10: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

10

Creating Parse Tree-Backtracking

S

Stuck, backtrack one step

E

a bI

Page 11: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

11

Creating Parse Tree-Backtracking

S

E

A

a A

a A

b A

b

Page 12: 1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple

12

Efficient Parsing

Backtracking– Runtime is exponential in length of string

Can improve to polynomial time (with degree 3) if in special form (Normal Forms)

Can improve to linear time with simple grammar.