1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G)...

Preview:

Citation preview

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 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

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.

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.

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

6

Creating Parse Tree-Backtracking

S

E S

a bI

Stuck, backtrack one step

7

Creating Parse Tree-Backtracking

S

E S

A

Stuck, backtrack one step

a A

a A

b A

b A

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

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

10

Creating Parse Tree-Backtracking

S

Stuck, backtrack one step

E

a bI

11

Creating Parse Tree-Backtracking

S

E

A

a A

a A

b A

b

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.

Recommended