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

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

Chapter 19 LL(k) Grammars

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

View Full Document
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.
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

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

View Full Document
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 .
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 }

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

View Full Document
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(
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online