Chapter 19 LL(k) Grammars. 2 LL(k) Parsers n Can be developed using PDAs for parsing CFGs by converting the machines directly into program statements.

Slides:



Advertisements
Similar presentations
1 Chapter Parsing Techniques. 2 Section 12.3 Parsing Techniques We know (via a theorem) that the context- free languages are exactly those languages.
Advertisements

Context free languages 1. Equivalence of context free grammars 2. Normal forms.
Compiler Designs and Constructions
1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-ε with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a.
Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
LR-Grammars LR(0), LR(1), and LR(K).
Simplifying CFGs There are several ways in which context-free grammars can be simplified. One natural way is to eliminate useless symbols those that cannot.
Lecture # 11 Grammar Problems.
142 Parsing start (a, Z 0 /aZ 0 ) ( a, a/aa ) (b, a/  ) ( , Z 0 /Z 0 ) For a given CFG G, parsing a string w is to check if w  L(G) and, if it is,
Pushdown Automata Part II: PDAs and CFG Chapter 12.
Chapter 4 Normal Forms for CFGs Chomsky Normal Form n Defn A CFG G = (V, , P, S) is in chomsky normal form if each rule in G has one of.
CS5371 Theory of Computation
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
CH4.1 CSE244 Introduction to LR Parsing Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box.
CS 3240 – Chapter 8.  Is a n b n c n context-free? CS Properties of Context-Free Languages2.
Chapter 6: Top-Down Parser1 Compiler Designs and Constructions Chapter 6: Top-Down Parser Objectives: Types of Parsers Backtracking Vs. Non-backtracking.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
Chapter 9 Turing Machine (TMs).
Finite State Machines Data Structures and Algorithms for Information Processing 1.
CS 3813 Introduction to Formal Languages and Automata Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
1 Dr. torng CFG → PDA construction Shows that for any CFL L, there exists a PDA M such that L(M) = L The reverse is true, but we skip the proof Parsing.
Pushdown Automata (PDA) Part 2 While I am distributing graded exams: Design a PDA to accept L = { a m b n : m  n; m, n > 0} MA/CSSE 474 Theory of Computation.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
Chapter 7 PDA and CFLs.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
Pushdown Automata (PDA) Intro
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Chapter 5 Context-Free Grammars
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
1 Section 12.3 Context-Free Parsing We know (via a theorem) that the context-free languages are exactly those languages that are accepted by PDAs. When.
Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on material from our textbook, An Introduction to Formal.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Chapter 6 Properties of Regular Languages. 2 Regular Sets and Languages  Claim(1). The family of languages accepted by FSAs consists of precisely the.
1 Chapter 6 Simplification of CFGs and Normal Forms.
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Context-Free Languages
1 Alphabets: An Alphabet is a finite set of symbols. We will usually use  to denote the alphabet of input symbols or “terminal characters.” String: A.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Bottom-Up Parsing Algorithms LR(k) parsing L: scan input Left to right R: produce Rightmost derivation k tokens of lookahead LR(0) zero tokens of look-ahead.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
Theory of Languages and Automata By: Mojtaba Khezrian.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Chapter 4 - Parsing CSCE 343.
Introduction to LR Parsing
Programming Languages Translator
Compiler Construction
Complexity and Computability Theory I
Top-Down Parsing.
Context-Free Languages
REGULAR LANGUAGES AND REGULAR GRAMMARS
LR(1) grammars The Chinese University of Hong Kong Fall 2010
Compiler Design 7. Top-Down Table-Driven Parsing
Chapter 6 Simplification of Context-free Grammars and Normal Forms
Chapter 2 Context-Free Language - 01
CFGs: Formal Definition
LR(1) grammars The Chinese University of Hong Kong Fall 2011
lec02-parserCFG May 27, 2019 Syntax Analyzer
Presentation transcript:

Chapter 19 LL(k) Grammars

2 LL(k) Parsers n Can be developed using PDAs for parsing CFGs by converting the machines directly into program statements n Describe the parsing strategy: i) the input string is scanned in a left-to-right manner ii) the parsers generate a leftmost 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 n Converting the non-deterministic transitions into the deterministic program segments n Predicts which one of the several production rules (in an unambiguous CFG) should be used to process the remaining input symbols n 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

4 Lookahead Strings and Lookahead Sets n Let p be a terminal string. An intermediate step in a derivation of p has the form S uAv, where p = ux. n Defn 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 n Example 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… n Example 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  ) = { abcabcd, ababcd, acabcd, bcabcd, aabcd, babcd, cabcd, abcd } { abcabcd, ababcd, acabcd, aabcd } { bcabcd, babcd, cabcd, abcd } { bcabcd, babcd } { cabcd, abcd } { cabcd } { abcd }

6 Lookahead Sets in CFGs n Example 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(A  a) = LA(A  b) = LA(A  ) =  The 3 rd symbol of the LA strings provides sufficient information to discriminate which one of the A rules to use. { aabd, babd, abd, cabcd, cbbcd, cbcd } { aabd, babd, abd } { cabcd, cbbcd, cbcd } { aabd, abcd } { babd, bbcd } { abd, bcd } /* 1 st symbol: { a, b } */ /* 1 st symbol: { c } */ /* 2 nd symbol: { aa, ab }; 3 rd symbol: { aab, abc } */ /* 2 nd symbol: { ba, bb }; 3 rd symbol: { bab, bbc } */ /* 2 nd symbol: { ab, bc }; 3 rd symbol: { abd, bcd } */

7 Lookahead Strings and Lookahead Sets n Example Given the following grammar G 2 : S  ABCabcd, A  a |, B  b |, C  c | LA(S) = { abcabcd, ababcd, acabcd, bcabcd, aabcd, babcd, cabcd, abcd }  No lookahead symbol is required in selecting the only S rule A  a LA(A  a) = { abcabcd, ababcd, acabcd, aabcd } A  LA(A  ) = { bcabcd, babcd, cabcd, abcd }  The 4 th lookahead symbol is required in selecting the A rule B  b LA(B  b) = { bcabcd, babcd } B  LA(B  ) = { cabcd, abcd }  The 1 st lookahead symbol is required in selecting the B rule C  c LA(C  c) = { cabcd } C  LA(C  ) = { abcd }  The 1 st lookahead symbol is required in selecting the C rule

8 FIRST, FOLLOW, and Lookahead Sets n The lookahead set LA k (A) contains prefixes of length up to k of strings that can be derived from the variable A (and after) n If variable A derives strings of length < k, the remainder of the lookahead strings comes from derivations that follow A in the production rules of the grammar. n FIRST k (A) contains prefixes of length up to k of terminal symbols (directly) derivable from A. n FOLLOW k (A) contains prefixes of length up to k of terminal symbols that can follow the strings derivable from A. n Defn Let G be a CFG. For every string u  (V   )* and k > 0, the set FIRST k (u) is defined by FIRST k (u) = trunc k ( { x | u x, x   * } ) where trunc k (X) = { u | u  X w/ length(u)  k *  or uv  X w/ length(u) = k }

9 FIRST, FOLLOW, and Lookahead Sets n Defn Let G be a CFG. For every A  V and k > 0, the set FOLLOW k (A) is defined by FOLLOW k (A) = { x | S uAv and x  FIRST k (v) } n Example Given G 2 (in Example ), S  ABCabcd, A  a |, B  b |, C  c | where ABC  { abc, ab, ac, bc, a, b, c, } FIRST 1 (ABC) = FIRST 2 (ABC) = FIRST 3 (S) = n Example FOLLOW 1 (S) = FOLLOW 1 (A) = FOLLOW 1 (B) = FOLLOW 1 (C) = *  { a, b, c, } { ab, ac, bc, a, b, c, } { abc, aba, aca, bca, aab, bab, cab } { } FOLLOW 2 (S) = { } { b, c, a } FOLLOW 2 (A) = { bc, ba, ca, ab } { c, a } FOLLOW 2 (B) = { ca, ab } { a } FOLLOW 2 (C) = { ab }

10 FIRST, FOLLOW and Lookahead Sets n Lemma For every k > 0, 1. FIRST k ( ) = { } 2. FIRST k (a) = { a } 3. FIRST k (au) = { av | v  FIRST k-1 (u) } 4. FIRST k (uv) = trunc k (FIRST k (u) FIRST k (v)) 5. If A  w  G, then FIRST k (w)  FIRST k (A) n Lemma For every k > 0, 1. FOLLOW k (S) contains, where S is the start symbol of G 2. If A  uB  G, then FOLLOW k (A)  FOLLOW k (B), i.e., any string that follows A can also follow B 3. If A  uBv  G, then trunc k (FIRST k (v) FOLLOW k (A))  FOLLOW k (B) i.e., the strings that follow B include those generated by v concatenated with all terminal strings that follow A n Example: Given S  aSc | bSc | FIRST 1 (S) = FOLLOW 1 (S) = { a, b, } FIRST 2 (S) = { aa, ab, ac, ba, bb, bc, } { c, } FOLLOW 2 (S) = { c, cc, }

11 LL( K ) Grammars n Theorem Let G be a CFG. For every k > 0, A  V, and rule A  w = u 1 u 2 …u n in P, i) LA k (A) = trunc k (FIRST k (A) FOLLOW k (A)) ii) LA k (A  w) = trunc k (FIRST k (w) FOLLOW k (A)) = trunc k (FIRST k (u 1 )…FIRST k (u n ) FOLLOW k (A))

Construction of FIRST k Sets n Algorithm Construction of FIRST k Sets Input: a CFG G = (V, , P, S) 1. For each a  , do F’(a) := { a } 2. For each A  V, do F(A) := 3. Repeat 3.1 for each A  V, do F’(A) := F(A) 3.2 for each rule A  u 1 u 2 …u n with n > 0 do F(A) := F(A)  trunc k (F’(u 1 )F’(u 2 ) … F’(u n )) UNTIL F( A ) = F’ ( A ),  A  V 4. FIRST k (A) = F(A) { } if A   P  otherwise

Construction of FIRST k Sets n Example Construct the FIRST 2 sets for the variables of S  A## A  aAd | BC B  bBc | C  acC | ad F(S) := F(S)  trunc 2 (F ’(A) { # } { # }) F(A) := F(A)  trunc 2 ({ a } F ’(A) { d })  trunc 2 (F ’(B) F ’(C)) F(B) := F(B)  trunc 2 ({ b } F ’(B) { c }) F(C) := F(C)  trunc 2 ({ a } { c } F ’(C))  trunc 2 ({ a } { d }) {, bc } { ad } { ad, bc } {, bc, bb } { ad, ac } { ad, bc }{ ad, bc, aa, ab, ac, bb } {, bc, bb } {ad, ac } { ad, bc, aa, ab, ac, bb } {, bc, bb } { ad, ac } { ad, bc, aa, ab, ac, bb } {, bc, bb } { ad, ac } F(S)F(S)F(A)F(A)F(B)F(B)F(C)F(C)      { }  F ’(a) = a F ’(b) = b F ’(c) = c F ’(d) = d F ’(#) = # F(S) =  F(A) =  F(B) = { } F(C) = 

Construction of FOLLOW k Sets n Algorithm Construction of FOLLOW k Sets Input: a CFG G = (V, , P, S), FIRST k (A) for every A  V 1. FL(S) := { } 2. for each A  V – { S }, do FL(A) :=  3. repeat 3.1 for each A  V, do FL’(A) := FL(A) 3.2 for each rule A  w = u 1 u 2 … u n with w   * do L := FL’(A) if u n  V, then FL(u n ) := FL(u n )  L for i := n – 1 to 1 do L := trunc k (FIRST k (u i+1 ) L) if u i  V, then FL(u i ) := FL(u i )  L until FL(A) = FL’(A),  A  V 4. FOLLOW k (A) = FL(A)

Construction of FOLLOW k Sets n Example Construct the FOLLOW 2 sets for the variables of Rule S  A## A  aAd A  BC B  bBc C  acC | ad ## ##, d# ad, ac ## ##, d#, dd ad, ac, ca ##, d# ##, d#, dd ad, ac, ca, cc ##, d#, dd ad, ac, ca, cc ##, d#, dd FL(S)FL(A)FL(B)FL(C) 0 1 { } { }      FL(A) := FL(A)  trunc 2 ({ # } { # } FL’(S)) FL(A) := FL(A)  trunc 2 ({ d } FL’(A)) FL(C) := FL(C)  FL’(A) FL(B) := FL(B)  trunc 2 (FIRST 2 (C) FL’(A)) := FL(B)  trunc 2 ({ ad, ac } FL’(A)) FL(B) := FL(B)  trunc 2 ({ c } FL’(B)) FL(C) := FL(C)  FL’(C) Assignments * *

Construction of LA k Sets n Example Construct the LA 2 sets for the rules of LA 2 ( S  A##) = LA 2 ( A  aAd) = LA 2 ( A  BC) = LA 2 ( B  bBc) = LA 2 ( B  ) = LA 2 ( C  acC) = LA 2 ( C  ad) = FOLLOW 2 (S)FOLLOW 2 (A)FOLLOW 2 (B)FOLLOW 2 (C) { } { ##, d#, dd }{ ad, ac, ca, cc } { ##, d#, dd } FIRST 2 (S)FIRST 2 (A)FIRST 2 (B)FIRST 2 (C) { ad, bc, aa, ab, bb, ac } {, bc, bb }{ ad, ac } { ad, bc, aa, ab, bb, ac } { aa, ab } { ad, ac, bc, bb } { bc, bb } { ad, ac, ca, cc } { ac } { ad }

Strong LL( K ) Grammars n In strong LL(k) grammars   A  V, LA k (A) is partitioned by LA k ( A  w i ), i  1  An endmarker # k is attached to the end of each string in the grammar to guarantee that every LA string contains exactly k symbols  Definition Let G = (V, , P, S) be a CFG w/ endmarker # k. G is strong LL(k) if there are two leftmost derivations S u 1 Av 1 u 1 xv 1 u 1 zw 1 S u 2 Av 2 u 2 yv 2 u 2 zw 2 where u i, w i, z   * (i = 1 or 2) and length(z) = k, then x = y.  Theorem A grammar G is strong LL(k) if and only if  i, LA k ( A  w i ) partition LA(A) for each variable A  V. *  *  *  *  *  * 

A Strong LL( 1 ) Grammar n Given the following grammar G: S  A# A  TB B  Z | Z  +TY Y  Z | T  b | (A) G is a strong LL(1) since the LA 1 sets for the rules are disjoint LA 1 (S  A#) = { b, ( } LA 1 (A  TB) = { b, ( } LA 1 (B  Z) = { + } LA 1 (B  ) = { #, ) } LA 1 (Z  +TY) = { + } LA 1 (Y  Z ) = { + } LA 1 (Y  ) = { #, ) } LA 1 (T  b) = { b } LA 1 (T  (A)) = { ( }

A Strong LL( k ) Parser n Example LA 1 (S  A#) = { b, ( } LA 1 (B  Z) = { + } LA 1 (A  TB) = { b, ( } LA 1 (B  ) = { #, ) } Input String: LA 1 (Y  Z ) = { + } LA 1 (T  b) = { b } p = (b+b)# LA 1 (Y  ) = { #, ) } LA 1 (T  (A)) = { ( } LA 1 (Z  +TY) = { + } u A V LA Rule Derivation S ( S  A# S  A# A # ( A  TB  TB# T B# ( T  (A)  (A)B# ( A )B# b A  TB  (TB)B# ( T B)B# b T  b  (bB)B# (b B )B# + B  Z  (bZ)B# (b Z )B# + Z  +TY  (b+TY)B# (b+ T Y)B# b T  b  (b+bY)B# (b+b Y )B# ) Y   (b+b)B# (b+b) B # # B   (b+b)#

LL( K ) Grammars  Definition Let G = (V, , P, S) be a CFG w/ endmarker # k. G is LL(k) if whenever there are two leftmost derivations S uAv uxv uzw 1 S uAv uyv uzw 2 where u, w i, z   * (i = 1 or 2) and length(z) = k, then x = y.  Theorem Let G = (V, , P, S) be a CFG w/ endmarker # k & uAv a sentential form of G. 1) The lookahead set of the sentential form uAv is defined by LA k (uAv) = FIRST k (Av). 2) The lookahead set for the sentential form uAv & rule A  w is defined by LA k (uAv, A  w). *  *  *  *  *  * 

21 Lookahead Sets in CFGs n Example Given the LA sets of grammar G 1 : LA(S) = { aabd, babd, abd, cabcd, cbbcd, cbcd } LA(S  Aabd) = { aabd, babd, abd } /* 1 st symbol: {a, b} */ LA(S  cAbcd) = { cabcd, cbbcd, cbcd } /* 1 st symbol: { c } */ LA(A  a) = { aabd, abcd } /* 2 nd symbol: {aa, ab}; 3 rd symbol: {aab, abc} */ LA(A  b) = { babd, bbcd } /* 2 nd symbol: {ba, bb}; 3 rd symbol: {bab, bbc} */ LA(A  ) = { abd, bcd } /* 2 nd symbol: {ab, bc}; 3 rd symbol: {abd, bcd} */ G 1 is not strong LL(2), but it is strong LL(3) since LA 2 (S, S  Aabd) = { aa, ba, ab } LA 2 (S, S  cAbcd) = { ca, cb } LA 2 (cAbcd, A  a) = { ab } LA 2 (cAbcd, A  b) = { bb } LA 2 (cAbcd, A  ) = { bc } LA 2 (Aabd, A  a) = { aa } LA 2 (Aabd, A  b) = { ba } LA 2 (Aabd, A  ) = { ab }

A Strong LL( k ) Parser n Algorithm Deterministic Parser for a Strong LL(k) Grammar Input: A strong LL(k) grammar G = (V, , P, S), p   *, LA k (A  w),  A  w  P. Output: p  L(G) or p  L(G). 1. q := S 2. repeat 2.0. Let q = uAv, where A is the leftmost variable in q. Let p = uyz, where length(y) = k If y  LA k (A  w) in P, then q := uwv. until q = p or y  LA k (A  w),  A rules in P. 3. If q = p, then accept else reject

23 Lookahead Sets in CFGs n Example Given the LA sets of grammar G: S  aBAd | bBbAd A  abA | c B  ab | a Consider LA 3 (B): LA 3 (aBAd, B  ab) = { aba, abc } LA 3 (aBAd, B  a) = { aab, acd } LA 3 (bBbAd, B  ab) = { abb } LA 3 (bBbAd, B  a) = { aba, abc } G is not strong LL(k), for any k  1, since LA 3 (B  ab) = ab(ab)*cd  abb(ab)*cd LA 3 (B  a) = a(ab)*cd  ab(ab)*cd

LL( k ) Parser n Algorithm Deterministic Parser for an LL(k) Grammar. Input: An LL(k) grammar G = (V, , P, S), p   *, FIRST k (A),  A  V Output: p  L(G) or p  L(G). 1. q := S 2. Repeat 2.0. Let q = uAv, where A is the leftmost variable in q. Let p = uyz, where length(y) = k For each rule A  w, construct LA k (uAv, A  w) 2.2. If y  LA k (uAv, A  w) in P, then q := uwv. Until q = p or y  LA k (uAv, A  w),  A rules in P. 3. If q = p, then accept else reject

25 LR( K ) Grammars n A deterministic bottom-up parser can be adopted in an attempt to reduce the input string to the start symbol of a grammar n Read the input string from left to right while constructing a rightmost derivation of the input string using a lookahead system involving k symbols n Process (of recognizing input strings of a CFG G): ● Step 1. Transfers symbols from its input to a stack till the uppermost stack symbols match the R.H.S. of some production rule R ● Step 2. Replace these symbols with the L.H.S. of R ● Step 3. Repeat steps 1 and 2 till the top stack symbol is the grammar’s start symbol or halt (i.e., the input string cannot be derived from G)

26 LR( K ) Grammars n Constructing a PDA from a CFG G that behaves as a LR(k) parser: ● Step 1. Create states q 0 (initial), q f (final), q 1 and q 2 ● Step 2. Create transitions  (q 0,, ) = { [q 1, #] } and  (q 2,, #) = { [q f, ] } ● Step 3. For each terminal symbol x  ,  Create the transition  (q 1, x, ) = { [q 1, X] }, where X  , a shift ● Step 4. For each production rule N  w in P, where w  (V   )*  Create the transition  (q 1,, w) = { [q 1, N] }, a reduce ● Step 5. Create the transition  (q1,, S) = { [q2, ] }, where S is the start symbol in G

27 LR( K ) Grammars n Example: Let G be the CFG S  zMNz M  aMa | z N  bNb | z A left-to-right, rightmost derivation of the string zazabzbz is: S  zMNz  zMbNbz  zMbzbz  zaMabzbz  zazabzbz