Download presentation
Presentation is loading. Please wait.
Published byAlexis Page Modified over 9 years ago
1
1 Efficient Parsing and Ambiguity
2
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
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
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.
5
Example G = ({S,E,A,I}, {a,b}, P, S), where P contains S -> ES | E E -> aIb | A A -> aA | bA | a | b I -> b | Is aabb L(G)? 5
6
6 Creating Parse Tree-Backtracking S ES ab I Stuck, backtrack one step
7
7 Creating Parse Tree-Backtracking S ES A Stuck, backtrack one step aA aA bA bA
8
8 Creating Parse Tree-Backtracking S ES A Stuck, backtrack one step aA aA bA b ES A No other choices, backtrack another step
9
9 Creating Parse Tree-Backtracking S ES A Stuck, backtrack one step aA aA bA b No other choices, backtrack another step E A
10
10 Creating Parse Tree-Backtracking S Stuck, backtrack one step E ab I
11
11 Creating Parse Tree-Backtracking S E A aA aA bA b
12
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.