Ch19 - Chapter 19 LL(k) Grammars LL(k) Parsers s s Can be...

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

View Full Document Right Arrow Icon
Chapter 19 LL(k) Grammars
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 LL(k) Parsers Can be developed using PDAs for parsing CFGs by converting the machines directly into program statements Describe the parsing strategy: i) the input string is scanned in a l eft-to-right manner ii) the parsers generate a l eftmost derivation, and iii) a deterministic top-down parsing using a k -symbol lookahead, attempting to construct a leftmost derivation of an input string The lookahead principle can be used to construct programs that overcome the non-determinism found in some PDA.
Background image of page 2
3 The Lookahead principle Resolve the non-determinism of different production rules by converting the non-deterministic transitions into the deterministic program segments Predicts which one of the several production rules (in an unambiguous CFG) should be used to process the remaining input symbols Example . Consider a derivation of the string acbb using G : S aS | cA A bA | cB | λ B cB | a | λ Comparing the lookahead (input) symbol with the terminal symbol in each of the appropriate production rules permits the deterministic construction of each derivation in G Prefix Generated Lookahead Symbol Production Rule Derivation λ a ac acb acbb a S aS S aS c S cA acA b A bA acbA b A bA acbbA λ A λ acbb
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Lookahead Strings and Lookahead Sets Let p be a terminal string. An intermediate step in a derivation of p has the form S uAv , where p = ux . Defn. 19.1.1 . Let G = ( V , , P , S ) be a CFG and A V i) The lookahead set of the variable A , LA( A ), is defined by LA( A ) = { x | S uAv ux * } ii) For each rule A w in P , the lookahead set of the rule A w is defined by LA( A w ) = { x | wv x , where x * S uAv } Note: LA( A w ) LA( A ) such that LA ( A w ) dictates the derivations Av x , which are initiated with the rule A w * * * * * * The string x is called a lookahead string for the variable A . The lookahead set of A consists of all lookahead strings for A .
Background image of page 4
5 Lookahead Strings and Lookahead Sets Example 19.1.4. Grammar Rule # of lookahead symbols to be considered G 1 : S aSc | aabc 3: aaa…, aab… G 2 : S aA A Sc | abc 2 (for A ): aa… , ab… G 3 : S aaAc A aAc | b 1 (for A ): a…, b Example 19.1.2. G 2 : S ABCabcd , A a | λ , B b | λ , C c | λ LA( S ) = LA( A a ) = LA( A λ ) = LA( B b ) = LA( B λ ) = LA( C c ) = LA( C λ ) = { abc abcd , ab abcd , ac abcd , bc abcd , a abcd , b abcd , c abcd , abcd } { abc abcd , ab abcd , ac abcd , a abcd } { bc abcd , b abcd , c abcd , abcd } { bc abcd , b abcd } { c abcd , abcd } { c abcd } { abcd }
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Lookahead Sets in CFGs Example 19.1.1 . Given the following grammar G 1 : S Aabd | cAbcd A a | b | λ LA( S ) = LA( S Aabd ) = LA( S cAbcd ) = We can select the appropriate S rule above using the 1 st symbol of the LA strings. LA(
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2012 for the course C S 252 taught by Professor Dennisng during the Winter '12 term at BYU.

Page1 / 27

Ch19 - Chapter 19 LL(k) Grammars LL(k) Parsers s s Can be...

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

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