This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: COP5555 Parsing 2 2 • Recall Grammars generate, parsers recognize – There are parsing algorithms for any contextfree grammar, but may be too slow to be useful in practice – We want to identify special classes of contextfree grammars that can be parsed with linear algorithms – We mentioned (but did not define) • LL grammars (top down parsing alg.) • LR grammars (bottom up parsing alg.) – Now we will explore LL(1) grammars in more detail • For motivation, look at some examples that cause trouble parsing • State rules for grammars that rule out the troublesome situations 3 Topdown parsing S ::= AB A ::= xA  y B ::= xB  z String to parse: xxz 4 Topdown parsing S ::= AB A ::= xA  y B ::= xB  z String to parse: xxz S xxz 5 Topdown parsing S ::= A B A ::= xA  y B ::= xB  z String to parse: xxz S xxz → A xxz 6 Topdown parsing S ::= AB A ::= x A  y B ::= xB  z String to parse: xxz S xxz → A x xz → xA xxz 7 Topdown parsing S ::= AB A ::= xA  y B ::= xB  z String to parse: xxz S xxz → A xxz → x A x xz → consume x A xz 8 Topdown parsing S ::= AB A ::= x A  y B ::= xB  z String to parse: xxz FAILURE But string is in language!! S xxz → (choose A) A xxz → x A x xz → (consume x) A x z → xA xz → (consume x) A z 9 Topdown parsing S ::= A B A ::= x A  y B ::= xB  z String to parse: xxz S xxz S xxz → (choose A) → (choose B) A xxz B xxz → x A x xz xB xxz → → (consume x) A x z B xz → → xA xz xB xz fail → (consume x) B z → Success!!! z z 10 • We need to predict which production to choose, preferably by looking only at one symbol • In this example, we must look at the last symbol, thus required lookahead not even bounded. S ::= AB A ::= x A  y 0 more x’s followed by a y or a z B ::= xB  z • Need to formulate restrictions on grammars that will allow top down parsing with bounded lookahead. – An LL(k) grammar can be parsed by a topdown parser with max ktoken lookahead. 11 Notational conventions • It is conventional in general discussions of grammars to use – lower case letters near the beginning of the alphabet for terminals – lower case letters near the end of the alphabet for strings of terminals – upper case letters near the beginning of the alphabet for nonterminals – upper case letters near the end of the alphabet for arbitrary symbols – Greek letters for arbitrary strings of symbols 12 FIRST sets • FIRST( α ) is the set of tokens (or ε ) that appear as the first symbol in some string generated from α • FIRST( α ) ≡ {a : α → * a β } – recall α and β are arbitrary strings of symbols while a is a terminal 13 Example 1 S ::= AB A ::= x  y B ::= z  w Sentences in language: xz, xw, yz, yw FIRST(x) = ????? 14 Example 1 S ::= AB A ::= x  y B ::= z  w Sentences in language: xz, xw, yz, yw FIRST(x) = {x} FIRST(y) = ????? 15 Example 1 S ::= AB A ::= x  y B ::= z  w Sentences in language: xz, xw, yz, yw FIRST(x) = {x} FIRST(y) = {y} FIRST(w) = ????? 16...
View
Full
Document
This note was uploaded on 12/09/2009 for the course COP 5555 taught by Professor Staff during the Fall '08 term at University of Florida.
 Fall '08
 Staff

Click to edit the document details