04 Parsing-2 - COP5555 Parsing 2 2 • Recall Grammars...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COP5555 Parsing 2 2 • Recall Grammars generate, parsers recognize – There are parsing algorithms for any context-free grammar, but may be too slow to be useful in practice – We want to identify special classes of context-free 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 Top-down parsing S ::= A|B A ::= xA | y B ::= xB | z String to parse: xxz 4 Top-down parsing S ::= A|B A ::= xA | y B ::= xB | z String to parse: xxz S xxz 5 Top-down parsing S ::= A |B A ::= xA | y B ::= xB | z String to parse: xxz S xxz → A xxz 6 Top-down parsing S ::= A|B A ::= x A | y B ::= xB | z String to parse: xxz S xxz → A x xz → xA xxz 7 Top-down parsing S ::= A|B A ::= xA | y B ::= xB | z String to parse: xxz S xxz → A xxz → x A x xz → consume x A xz 8 Top-down parsing S ::= A|B 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 Top-down 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 ::= A|B 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 top-down parser with max k-token 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 non-terminals – 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.

Page1 / 177

04 Parsing-2 - COP5555 Parsing 2 2 • Recall Grammars...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online