8. LL(k) Parsing. Canonical LL(k) parser dual of the canonical LR(k) parser generalization of strong LL(k) parser

Size: px
Start display at page:

Download "8. LL(k) Parsing. Canonical LL(k) parser dual of the canonical LR(k) parser generalization of strong LL(k) parser"

Transcription

1 3/8/ LL(k) Parsing LL(k) Parsing Canonical LL(k) parser The most general deterministic parsing method in which the input string is parsed (1) in a single Left-to-right scan, (2) producing a Left parse, and (3) using lookahead of length k. Canonical LL(k) parser dual of the canonical LR(k) parser generalization of strong LL(k) parser Generalization of shift-reduce pareser produce-shift parser Stack symbols valid stack string valid stack string viable prefix viable suffix LR(k) equivalence LL(k) equivalence

2 3/8/ LL(k) Parsing Viable Suffixes produce-shift parser adding lookahead adding lookback G abl : S aaab bab A cab a B SLL(k) parser LL(k) parser L(G abl ) = a{c}*{, a}ab b{c}*{, a}b. G abl is not SLL(k) for any k. First k ( Follow k (A)) = {k:ab, k:b} First k (afollow k (A)) = {k:aab, k:ab} First k ( Follow k (A)) First k (afollow k (A)) = {k:ab} produce-produce conflict for A a and A. adding lookahead A ab A a a ab A ab G abl is not SLL(k) for any k. adding lookahaed and lookback bb m A ab A a bb m a ab bab m A ab A bab m ab G abl is LL(2) A a A. A a A.

3 3/8/ LL(k) Parsing 3 3 A string is a viable stack string of pda M, if $ s xy$ * $ y$ * $ f $ in M. stack string in some accepting computation M. G abl : S aaab bab A cab a B. Viable stack string of G abl (in regular exp) {S} {baaa} {baa} {ba} {b} { } {bab} {ba} {bab n Ac n 1} {bb n Ac n 1} {bab n A n 1} {bb n A n 1} {bab n a n 1} {bb n a n 1} {bab n n 1} {bb n n 1} An action r is valid for viable stack string of M if $ y $ r $ y$ in M. k:y$ First k ( R $). Note that the action r is valid for not for. Two viable stack string belongs to the same equivalent class if they have same set of valid actions. At most + k P G k+1 distinct actions at most 2 G k+1 equivalent classes

4 3/8/ LL(k) Parsing 4 4 prev. class: valid action equi. class [S] S a baaa a [baaa] [S] S b bab b [bab] [baaa] a a [baa] [bab] b b [ba] [baa] A c BAc c [bab + Ac] [baa] A aa a aa [baa] [baa] A ab ab [ba] [baa] a a [ba] [ba] a a [b] [b] b b [ ] [ba] A c BAc c [bb + Ac] [ba] A ab a ab [ba] [ba] A b$ b$ [b] [bab + Ac] c c [bab + A] [bb + Ac] c c [bb + A] [bab + A] A c BAc c [bab + Ac] [bab + A] A aa a aa [bab + a] [bab + A] A ab ab [bab + ] [bab + a] a a [bab + ] [bab + ] B ab ab [bab + ] [bab + ] B ab ab [ba] [bb + A] A c BAc c [bb + Ac] [bb + A] A ab a ab [bb + Aa] [bb + A] A b$ b$ [bb + ] [bb + Ac] c c [bb + A] [bb + Aa] a a [bb + A] [bb + ] B b$ [bb + ] [bb + ] B b$ [b] [S]

5 3/8/ LL(k) Parsing 5 5 The equivalence should be (1) right-invariant (2) any two equivalent viable stack strings should end with the same symbol {, ba} { }, {ba} {bb n n 0} {b}, {bb n n 1} {bb n A n 1} {bab n A n 1}, {bb n A n 1}, {bab n A n 1} {baa} {bab n a n 1} {A aa a aa} {bab n a n 0} same valid actions 16 equivalent classes [ ], [S], [b], [ba[, [baa], [baaa], [ba], [bab], [bab + ], [bab + A], [bab + c], [bab * a], [bb + ], [bb + A], [bb + c], [bb + a].

6 3/8/ LL(k) Parsing 6 6 S aaab bab A cab a B a [bb + a] c [bb + Ac] B [bb + ] A [bb + A] a [ ] b B [b] a [ba] B B a [bab + ] S A A [S] [ba] [baa] b [bab] a [baaa] [bab * a] A [bab + A] c [bab + Ac]

7 3/8/ LL(k) Parsing 7 7 A simplified canonical LL(k) parser the new produce action [ A] y Y n Y 1 y, where A is viable stack string, A X 1... X n is a rule, y First k (X 1 X n R $), Y i = [ X n X i ] X i N X i X i. same shift action a a. the shift action is same as that of SLL(k) parser. Example: simplified canonical LL(2) parser of G abl (p. 202 ) [S] 2 aa ba[baa] 2 a aa, S aaab. in the SLL(2) parser A ab a ab, A ab ab. in the simplified canonical LL(2) parser [ba] 2 ab a ab, [bb + A] 2 ab a ab. [baa] 2 ab ab, [bab + A] 2 ab ab. G is not SLL(2) but simlified canonical LL(2).

8 3/8/ LL(k) Parsing 8 8 String ( ( ) R R R ) V * is a viable suffix of G, if S lm * xa lm x ( x R ) where V *, x *, and A P. is a complete viable suffix, if =. Note that the viable suffix is reversed, since the top of the stack is the rightmost symbol. Fact 8.1 Any (reversal of) viable suffix is a (suffix) prefix of some complete viable suffix. Lemma 8.2 n 0, 1, 2 V*, r (a) 1 1 r n rm 2 in G iff 1 R lm r 1R r n R 2 R in G R (b) 1 lm r 1 r n 2 in G iff 1 R rm r 1R r n R Proof induction on n. 2 R in G R r R : A R in G R whenever r: A in G. The set of viable suffixes of G coincides with the set of viable prefixes of G R.

9 3/8/ LL(k) Parsing 9 9 Lemma8.3 (a) V * is a (complete) viable prefix of G iff is a (complete) viable suffix of G R. (b) V * is a (complete) viable suffix of G iff is a (complete) viable prefix of G R. Proof. (a) (only if) If S rm * Ay r rm y (= y) holds in G, then, S * lm ( Ay) R = y R A R * lm y R ( ) R R = y R R R = y R R R holds in G R. (L 8.2) (if) (G R ) R = G. only if part of (b) = if part of (a) only if part of (a) = if part of (b)

10 3/8/ LL(k) Parsing Theorem 8.4 the set of all viable suffixes of G is a regular language over V Given a grammar G = (N,, P, S), Let G VS = (N VS, VS, P VS, [S]) where N VS = {[A] A N}, VS = N, and P VS = {[A] R A P} {[A] R [B] A B P, B N}. (G abl ) VS : G abl : [S] b ba baa baaa ba bab ba[a] b[a] S aaab bab, [A] B BA BAc [B] B[A] a A cab a, [B] B. a A b a A b b [S] c A a B [A] [B]

11 3/8/ LL(k) Parsing [ ] S S S aaab S bab [(b ba)b + B ] b [b] S aaa b A ca B S ba b A cab B A cab A A A a B B A a B a [(b ba)b + A] A S aa ab A c AB A a A cab c A [(b ba)b + Ac] A a A cab a B a A a S S S [S] A [ba] S b Ab [bab] b S bab [ba] A [baa] S a Aab [baaa] a S aaab [(bb + bab * )a] B [(b ba)b + ] A [(b ba)b + A] c S [ ] B b A [b] a a B [ba] A a [(b ba)b + Ac] [(bb + bab * )a] [S] [ba] [baa] b a [bab] [baaa]

12 3/8/ LL(k) Parsing Lemma 8.5 If S lm x = xa in G, and. Then S rm x A lm r x = x x rm x r " =, and 1: = 1:. (L6.2) If is a prefix of some nontrivially derived left sentential form not extending over the first nonterminal, then there is the derivation contains a segment that proves R to be a viable prefix, even so that the righthand side of the rule r cuts properly. S A x x = A

13 3/8/ LL(k) Parsing Lemma 8.6 S lm + xa. Then R A is a viable suffix. Proof. = A =. (L 8.5) Lemma 8.7 Any (suffix) prefix of a reversal of viable suffix is a (reversal of) viable suffix. Proof. Let 1 2 is a viable suffix,then 1 2 is a viable prefix of G R.If 1 is a viable prefix of G R (L 6.4), then a viable suffix of (G R ) R = G. (L 8.3(a)) Lemma 8.8 A P, if A is a viable suffix of G, then so is R. Lemma 8.9 Let M is its SLL(k) parser for G and $ y$ $ $ in M. Then $ y$ $ z$ " $ $ in M,and = ". Proof. induction on. (L 6.6) Lemma 8.10 Any prefix of a viable stack string of an SLL(k) parser is a viable stack string.(l 8.9)

14 3/8/ LL(k) Parsing Theorem 8.11 Any viable stack string of SLL(k) parser M is either S or viable suffix of G. Conversely, any viable suffix of G is a viable stack string of M, provided that G is reduced. (T6.7) Proof. (the first part of theorem) S, r the last produce action in $S w$ $ z$ * $ $ in M $S w$ $ A yz$ r $ R yz$ = $ y R yz$ $ z$, where 1 r 2 =, w=xyz 2 is a y}-lengthed shift actions. S lm *xa R in G, A P, y R = R.(L 5.27) S lm * xa R lm x R = x( y R ) R, y R is viable suffix, so is. Q.E.D.

15 3/8/ LL(k) Parsing Valid LL(k) Items Let A P. Then [A, y] is a k-item. A is a core item, y k: * is a lookahead string. A k-item [A, y] is LL(k)-valid(or valid) for string V * if S lm * xa lm x (=x R ) and y First k ( R ). The stack string is the prediction of remained input string. Fact 8.12 If [A, y] is a LL(k) valid item for string, then is a viable suffix,[a, y] is a k- item, R is a suffix of (= R R ) and y First k ( R ) = First k ( ) First k ( Follow k (A)) Conversely, if a string is a viable suffix, then some item is LL(k)-valid for.(f6.12) Example : LL(k) valid item of G abl

16 3/8/ LL(k) Parsing Let V *. Then LL(k) G = Valid LL(k) G ( ) = {I I is an LL(k)-valid item for } = {[A, y] S lm * xa lm x =x R y First k ( R )} Valid G LL(k) Valid LL(k) Valid k Valid G LL(k) LL(k) k. String 1 is LL(k)-equivalent(equivalent) to string 2, written 1 LL(k) 2 (or 1 k 2 ), if Valid k ( 1 ) = Valid k ( 2 ). The relation k is the LL(k)-equivalence for G. [ ] k denotes an equivalent class of under k [ ] k = { k } [ ] k [ ] k [ ]. We denote [ ] k by [ ] k (or even [ ]).

17 3/8/ LL(k) Parsing Fact 8.13 From the definition of a viable suffix implies that [A 1 1 1, y 1 ], [A 2 2 2, y 2 ] k {z [A 1 1 1, z] k } = {z [A 2 2 2, z] k } = First k ( R ). Note that the viable suffix is reversed in LL parsing. Theorem 8.14 The LL(k)-equivalence k for G is of finite index, and the index of k is at most 2 G ( +1)k.(T6.13) Proof. The number of distinct subsets of k: * is at most 2 ( +1)k, each of there subsets may occur in at most 2 G sets 0. (F 8.13) Hence, 2 G 2 ( +1)k =2 G ( +1)k. Q.E.D.

18 3/8/ LL(k) Parsing Lemma 8.15 Let k l. Then k = {[A, k:y] [A,y] l }.(L6.14) Lemma 8.16 Let k l. Then LL(l)-equivalence is a refinement of LL(k)-equivalence. More specifically [ ] k = [ ] l.(l6.15) canonical collection of set of LL(k)-valid items for G = canonical LL(k) collection for G: C k. Simplified canonical LL(k) machine M (or deterministic LL(k) machine) M = (C k, V, { k X X k }, k, ) Example : the canonical LL(2) machine for G abl

19 3/8/ LL(k) Parsing Construction of the canonical LL(k) machine LL(k) : relation on LL(k) items [A B, y] LL(k) [B y] desc LL(k), LL(k) k. I 2 is an immediate LL(k)-descendant of I 1, if I 1 I 2. I 2 is an LL(k)-descendant of I 1, if I 1 * I 2. I 1 is an (immediate) LL(k)-ancestor of I 2, if I 2 is an (immediate) LL(k)-descendant of I 1. k n = {[A,y] S lm n xa lm x (=x R ), y First k ( R )} Fact 8.17 k 0, k = n=0 n k.(f6.16) Lemma 8.18 If[A B, y] k n and m v *.Then B P, [B, y] k n+1.(l6.17) Proof. S lm n xa lm x B lm m xvb lm xv Lemma 8.19 k is closed under, i.e., * ( k ) = k.(l6.18)

20 3/8/ LL(k) Parsing Lemma 8.20 If [B, y] n k and n 0. Then [A B, y] m k, n-m-1 v, m n.(l6.19) Fact 8.21 k 0 = {[S R, ] S R P}. (F6.20) [A ] is LL-essential(or essential), if inessential, otherwise. Ess LL(k) (q) = {I q I is LL-essential}. Lemma 8.22 Let I k n, k, n 0. (1) n = 0, =, I = [S, ]. (2) and I is essential. (3) n 0, and I has an immediate ancestor in some k m, m n.(l6.21) Lemma 8.23 n k * ({S, ] S P}), n k * (Ess( n k )), if.(l6.22) Lemma 8.24 n k = * ({S, ] S P}) n k = * (Ess( n k )), if.(l6.23)

21 3/8/ LL(k) Parsing X k : relation on set of LL(k) items. [A X, y] X k [A X, z], z First k (Xy) pass-x, or X for short Basis LL(k) (q, X) = {[A X, z] [A X, y] q, and z First k (Xy)} X k (q). X k : relation on set of LL(k) items. Goto LL(k) (q, X) = * k (Basis LL (q, X)) = * k ( X k (q)) X k (q). X-successor, X k for short Lemma 8.25 If [A, y] k n, then R is a viable suffix and [A, z] R n k. If [A, z] n k, then s.t. [A, y] n k = R, z First k ( y).(f6.24) Lemma 8.26 Ess( X k ) = Basis( k, X)(L6.25) Lemma 8.27 X k = Goto( k, X)(L6.26)

22 3/8/ LL(k) Parsing Algorithm for constructing the canonical LL(k) machine begin compute the relation for X V do compute the X k relation q s := * k ({[S S, ]}); Q M := {q s }; P M := ; repeat for q Q M and X V do begin p := * k ( X k (q)); Q M := Q M {p}; P M := P M {q X p} end until nothing is added to Q M and P M. end

23 3/8/ LL(k) Parsing Lemma 8.28 Let M = (Q M, V, P M, q s, F) be a canonical LL(k) machine for G. Then the followings hold (a) M is deterministic. (b) q Q M, q has unique entry symbol. (c) q is accessible upon reading, iff q = k. (d) If F = { k for a given }, L M = [ ] k. (e) If F = { k k }, L M = Set of viable suffixes of X. (f) If F = { k for all }, L M = V *.(L6.27) Theorem 8.29 (a) The LL(k) equivalence of G is the equivalence induced by the canonical LL(k) machine of G. [ 1 ] = [ k 2 ], iff k q s 1 * q s q and q s 2 * q s q. (b) The LL(k) equivalence of G is right invariance. If [ 1 ] = [ k 2 ], then [ k 1 X] = [ k 2 X]. k (c) The LL(k) equivalence of G is ends with same symbols. If [ 1 ] = [ k 2 ], then k 1 :1 = 2 :1.(T6.28)

24 3/8/ LL(k) Parsing Canonical LL(k) Parser The canonical LL(k) parser for G is a pushdown transducer M = ([G] k,,,, [ ] k [S] k, {[ ] k }) where [G] k = {[ ] k V * } = {[ ] k [ A] k y [ ] k [ X n ] k [ X n X 1 ] k y [A X 1 X n, y] X n X 1 k } (pa) {[ ] k [ a] k ay [ ] k y [A a, k:ay] a k } (sa) ([ ] k [ A] k y [ ] k [ X n ] k [ X n X 1 ] k y) = A X 1 X n, ([ ] k [ a] k ay [ ] k y) =. compare in canonical LR(k) parser = {[ ] k [ X 1 ] k [ X 1 X n ] k y [ ] k [ A] k y [A X 1 X n, y] X 1 X n k } (ra) {[ ] k ay [ ] k [ a] k y a, [A a, z] k, y First max{k-1, 0} ( z)} (sa)

25 3/8/ LL(k) Parsing [B A, x] k q [B A,y] A k q A [A X 1 X n, x] k q (= q 0 ) [A X 1 X 2 X n, x n ] X n k q n [A X 1 X n, x 1 ] X n X 1 k q 1 Fig. 8.5 Algorithm for computing the parsing action of the canonical LL(k) parser. for q do for A X 1 X n P s.t. [A X 1 X n, x] q 0 do where q i+1 = GOTO(q i, X i+1 ) for 1 i n q A = GOTO(q,A) for [A X 1 X n, y] q 1 do where q q 0 generate qq A y qq n q 1 y od od for [A a, ay] q do generate q ay y od od

26 3/8/ LL(k) Parsing Fig 8.2 The Canonical LL(1) machine of G abl First 1 (S) = {a, b} First 1 (A) = {, a, c} First 1 (B) = { } [ ] S S, S S aaab, S S, {a,b} S bab, [S] [bb + B ] b [b] [ba] S aaa b, b A A ca B, b S ba b, b S b Ab,{a,b,c} A cab, b B A cab, b A, b A, b [bab] b A a, b A a, b S bab, b B, b B, b a [bb + A [ba] A] S aa ab, a A [baa] A c AB, A a, a S a Aab, {a,b,c} {a,c} A cab, a c A, a [(b ba)b + Ac] [baaa] a A a, a A cab, c a B, a S aaab,a a B B [(bb + bab * )a] [bab + ] A ca B, a A a, a [bab + A] A cab, a a A c AB,{a,c} A, a A a, a c B, a

27 3/8/ LL(k) Parsing Fig 8.3 The Canonical LL(2) machine of G abl First 2 (S) = {aa, ac, ba, bb, bc} First 2 (A) = {, a, c, ca} First 2 (B) = { } [ ] S S, S S aaab, S S, {aa,ac,ba,bb,bc} S bab, [S] [bb + B ] b [b] [ba] S aaa b,b A A ca B, b S ba b, b S b Ab, {ab,b,ca,cb,cc} A cab, b B A cab, b A, b A, b [bab] b A a, b A a, b S bab, B, b {ba,bb,bc} B, b a [bb + A [ba] A] S aa ab,ab A [baa] A c AB, A a, ab S a Aab, {ab,b,ca,cb,cc} A cab, ab {aa,ab,ca,cc} c A, ab [bb + Ac] [baaa] a A a, ab A cab, a B, ab S aaab,a {ca,cb,cc} B [bab + {aa,ac} B [bb + a] ] A ca B,ab A a,ab [bab + A] a A cab,ab A c AB,{a,c} A, ab {aa,ab,ca,cc} [bab * a] A a, ab c B, ab [bab + Ac] A a,aa a A cab, {ca,cc}

28 3/8/ LL(k) Parsing $[ ][S] accab$ S aaab $[ ][b][ba][baa][baaa] accab$ a $[ ][b][ba][baa] ccab$ A cab $[ ][b][ba][bab][baba][babac] ccab$ c $[ ][b][ba][bab][baba] cab$ A cab $[ ][b][ba][bab][babb][babba][babbac] cab$ c $[ ][b][ba][bab][babb][babba] ab$ A $[ ][b][ba][bab][babb] ab$ B $[ ][b][ba][bab] ab$ B $[ ][b][ba] ab$ a $[ ][b] b$ b $[ ] $ $[ ][S] bccb$ S bab $[ ][b][ba][bab] bccb$ b $[ ][b][ba] ccb$ A cab $[ ][b][bb][bba][bbac] ccb$ c $[ ][b][bb][bba] cb$ A cab $[ ][b][bb][bbb][bbba][bbbac] cb$ c $[ ][b][bb][bbb][bbba] b$ A $[ ][b][bb][bbb] b$ B $[ ][b][bb] b$ B $[ ][b] b$ b $[ ] $

29 3/8/ LL(k) Parsing Example: the difference between the simplified canonical LL(k) parser and the true canonical LL(k)parser.(p. 222 ) in simplified canonical LL(k) parser, [ A] k y in true canonical LL(k)parser [ ] k [ A] k y.

30 3/8/ LL(k) Parsing The canonical LL(k) parser yields a valid left parser. LL(k) parser left parser L8.30, L8.31(L6.29, L6.30) left parser LL(k) parser L8.32, L8.33, L8.34(L6.31, L6.32, L6.33) LL(k) parser left parser T8.35(T6.34) Lemma 8.30 Let M be a LL(k) parser for G. If $[$] k [$X 1 ] k [X 1 X m ] k w$ in M. Then = $[$] k [$Y 1 ] k [$Y 1 Y n ] k y$, w=xy, X m X 1 ( ) lm xy n Y 1 in G, and = ( ) + x. Proof Induction on. (L 5.12 or L 5.27) Lemma 8.31 Let M be a canonical LL(k) parser for G. Then (1) L(M) L(G), (2) is a left parse of w in M, ( ) is a left parse of w in G, (3) Time G (w) Time M (w) - w.

31 3/8/ LL(k) Parsing Lemma 8.32 Let M be a LL(k) parserfor G. If a n a 1 is a viable suffix of G, k:y$ k First k ( R ). Then s.t. is a string of an n-length shift actions of M, $ [ ] k [ a n a 1 ] k a 1 a n y$ k $ [ ] k y$ k Lemma 8.33 Let M be a LL(k) parser for G. If X m X 1 lm xy n Y 1 in G, [A ] $X 1 X m 0, k:y$ First k (Y n Y 1 ), and either Y n Y 1 = or Y n is nonterminal, Then ( ) =, = + x, and $[$] k [$X 1 ] k [$X 1 X m ] k xy$ $[$] k [$Y 1 ] k [$Y 1 Y n ] k y$ Proof. Induction on.

32 3/8/ LL(k) Parsing Lemma 8.34 Let M be a canonical LL(k) parser for G. Then (1) L(G) L(M), (2) is a left parse of w in G, ( ) =,s.t. is a left parse of w in M, (3) Time G (w) Time M (w) + w. Theorem 8.35 Let M be a canonical LL(k) parser for G. Then (1) M is a left parser for G. (2) w L(G), M produces all left parses of w. (3) Time M (w) = Time G (w) + w.

33 3/8/ LL(k) Parsing Lewis-Sterns Canonical LL(k) Parser The canonical LL(k) parser for G is a pushdown transducer M = ([L] k,, [S, {$ k }] k, { },, ) where [L] k = {[X, R] k X V, R Follow k (X)} = {[A, R] k y [X n, R n ] k [X 1, R 1 ] k y A X 1 X n P, y First k (X 1 X n R), R n = R, R i = First k (X i+1 X n R)} (pa) {[a, R] k ay y a } (sa) ([A, R] k y [X n, R n ] k [X 1, R 1 ] k y) = A X 1 X n, ([a, R] k ay y) =.

34 3/8/ LL(k) Parsing Algorithm Construction of [L] k and. [L] k := {[S, {$ k }] k }; := {}; repeat for [A, R] k [L] k do for A X 1 X n P do R n := R; for i :=n downto 1 do [L] k := [L] k [X i, R i ] k ; R i-1 := First k (X i R i ) if X i then := {[X i, R i ] k X i y y y R i } fi od; := {[A, R] k y [X n, R n ] k [X 1, R 1 ] k y y R 0 } od od until [L] k does not change

35 3/8/ LL(k) Parsing LL(k) Grammars A grammar G is an LL(k) grammar, if its canonical LL(k) parser is deterministic. K-item [A 1 1,y 1 ] and [A 2 2,y 2 ] of G exhibit a produce-produce conflict if A 1 =A 2, y 1 =y 2 but 1 2. Lemma 8.36 The canonical LL(k) parser for G is nondeterministic iff there is a viable suffix, and k- item [A 1,y] [A 2,y] exhibiting a produceproduce conflict s.t. (a) [A 1,y] 1 R k [A 2,y] 2 R k.(l6.36) Proof. ( ) Assume (a) holds, then [ ] k [ A] k y [ ] k [ X m ] k [ X m X 1 ] k y, X 1 X m = 1 [ ] k [ A] k y [ ] k [ Y n ] k [ Y n Y 1 ] k y, Y 1 Y n = 2 1 2, the parser is nondeterministic.

36 3/8/ LL(k) Parsing ( ) Assume the parser is nondeterministic Let be configuration to which r 1, r 2 are applicable. then (1) r 1 =[ ] k [ A] k y [ ] k [ X m ] k [ X m X 1 ] y, r 2 =[ ] k [ B] k z [ ] k [ Y n ] k [ Y n Y 1 ] z, (2) r 1 =[ ] k [ A] k y [ ] k [ X m ] k [ X m X 1 ] y, r 2 =[ a] k az z, (3) r 1 =[ a] k ay y, r 2 =[ b] k bz z. r 1, r 2 are both in same configuration, (2),(3) impossible. only case (1) remains. [A X 1 X m,y] X m X 1 k, [B Y 1 Y n,z] Y n Y 1 k = Y n Y 1 k A=B, y=z, but X 1 X m Y 1 Y n,otherwise r 1 =r 2. Q.E.D.

37 3/8/ LL(k) Parsing Nonterminal A N has the LL(k) property if First k ( 1 ) First k ( 2 )=. xa is left sentential forms, A 1, A 2 P (L6.37, L6.38; SLL(k) property) Theorem 8.37 The following statements are logically equivalent.(l6.39; T5.34) (a) G is an LL(k) grammar (b) [A 1,y] R 1 k, and [A 2,y] 2 R k always imply that 1 = 2. (c) A N have the LL(k) property. (d) S lm * xa lm x 1 lm * xv 1, S lm * xa lm x 2 lm * xv 2, and k:v 1 =k:v 2 always imply that 1 = 2. Proof. (a)=(b) (L 8.36) (b)=(b ) by LL(k)-validity(p. 207 ), (b ) S lm *x 1 A R lm x 1 1 R, y First k ( 1 R ), S lm *x 2 A R lm x 2 2 R, y First k ( 2 R ), always imply that 1 = 2. G G, S S, x 2 x 1 (S lm * x 1 A R, A 2 P)

38 3/8/ LL(k) Parsing (b") S lm * xa R lm x 1 R, y First k ( 1 R ), S lm * xa R lm x 2 R, y First k ( 2 R ), always imply that 1 = 2.(b")=(c)=(d). Theorem 8.38 for 1 l k, LA(k+1)LL(l) grammars LA(k+1)LL(l+1) grammars. for 0 l k, LA(k)LL(l) grammars LA(k+1)LL(l) grammars. Proof. examples(p. 231 ).

39 3/8/ LL(k) Parsing Equivalence of LL(1) and SLL(1) Lemma 8.39 Every LL(1) grammar is an SLL(1) grammar. Proof. Let G be an LL(1) grammar and consider, (1) S * lm x 1 A 1 lm x * lm x 1 v 1, S * lm x 2 A 2 lm x * lm x 2 v 2. Assume 1 * lm y 1, 1 * lm z 1, y 1 z 1 = v 1 2 lm * y 2, 2 lm * z 2, y 2 z 2 = v 2 (2) S lm * x 1 A 1 lm x lm * x 1 y 1 z 1 S * lm x 1 A 1 lm x * lm x 1 y 2 z 1, and (3) S * lm x 2 A 2 lm x * lm x 2 y 1 z 2 S lm * x 2 A 2 lm x lm * x 2 y 2 z 2 if y 1, then 1:v 1 = 1:v 2 1:y 1 = 1:y 2 z 2. (3) if y 2, then 1:v 1 = 1:v 2 1:y 1 z 1 = 1:y 2. (2) if y 1 =, y 2 =, then 1:v 1 = 1:v 2 1:z 1 = 1:z 2. If G = LL(1), (1) and 1:v 1 = 1:v 2 implies 1 = 2. G is SLL(1). (T 5.34) Q.E.D.

40 3/8/ LL(k) Parsing Lemma 8.40 k 0, any SLL(k) grammar is an LA- LL(k) grammar and any LALL(k) grammar is an LL(k) grammar. Thoerem 8.41 The classes of SLL(1), LALL(1), and LL(1) grammars coincide. Theorem 8.42 k 1, the class of SLL(k) grammar is properly contained in the class of LALL(k) grammar. Proof. counter example grammar which is LALL(k) but not SLL(k), for k 1. (p. 233 ) Properly containment of theorem 8.42 are properties of grammar only, but not properties of language. i.e. any LL(k) grammar can be transformed into an equivalent SLL(k) grammar.

41 3/8/ LL(k) Parsing Transformation of LL(k) grammar into SLL(k) T k (G) = ({[A, R] A N, R Follow k (A)},, P T(k), [S, {$ k }]), where P T(k) = [A, R] X 1...X m, and X i = [X i, First k (X i+1...x m R)], X i N, X i, X i. Nonterminal A is split into several nonterminals [A, R 1 ]...[A, R n ], each representing an occurrence of the original nonterminals in a particular context. Let h k homomorphism s.t. h k ([A, R] X 1...X m ) = A X 1...X m. G abl : LL(2) S aaab bab A cab a B. T k (G abl ): SLL(2) [S, {$ 2 }] a [A, {ab}] a b b [A, {b$}] b [A, {ab}] c [A, {ab}] [B, {ab}] a [A, {b$}] c [A, {b$}] [B, {b$}] a [B, {ab}] [B, {b$}]

42 3/8/ LL(k) Parsing Covering LL(k) grammars by SLL(k) grammars (1) T k (G) left-to-left covers G. T8.45(L8.43, L8.44) (2) T k (G) is SLL(k), iff G is LL(k). T8.47 T8.48, T8.49 Lemma 8.43 Let [A, R] lm xx 1...X n in T k (G).Then A lm h k ( ) xx 1...X n in G. where X 1 = [X i, First k (X i+1 X n R)], if X i N, X i, if X i. Proof. induction on. Lemma 8.44 Let A lm xx 1...X n in G, an either X 1...X n = or X 1 N. Then P^*. h k ( ) =, [A, R] lm xx 1...X n in T k (G). Proof. By induction on. Theorem 8.45 T k (G) left-to-left covers G w.r.t. h k. Corollary 8.46 If (M, ) is a left parser of T k (G), then (M, h k ) is a left parser of G.

43 3/8/ LL(k) Parsing Theorem 8.47 T k (G) is SLL(k) whenever G is LL(k). G is LL(k) whenever T k (G) is so. Proof. (proof of the first part of theorem) Let [A, R] X 1 X m, [A, R] Y 1 Y n P Tk, 0 m p, 0 n q, s.t. (1) [S, {$}] * lm x 1 [A, R] X m+1 X p lm x 1 X 1 X m X m+1 X p lm * x 1 y 1, [S, {$}] lm * x 2 [A, R]Y n+1 Y q lm x 2 Y 1 Y n Y n+1 Y q lm * x 2 y 2, k: y 1 = k: y 2 implies X 1 X? m = Y 1 Y n. (4) X 1 X m * lm u 1, X m+1 X p * lm v 1, u 1 v 1 = y 1. Y 1 Y n lm * u 2, Y n+1 Y q lm * v 2, u 2 v 2 = y 2. (5) X 1 X m lm * u 1, X m+1 X p lm * v 1. Y 1 Y n lm * u 2, Y n+1 Y q lm * v 2. (6) First k (X m+1 X p $) = R = First k (Y n+1 Y q $) (7) S lm * x 1 AX m+1 X p.(l 8.43)

44 3/8/ LL(k) Parsing (8) X m+1 X p * lm v 2 s.t. k:v 2 = k:v 2, so k: u 1 v 1 = k: y 1 = k: y 2 = k: u 2 v 2 = k: u 2 v 2 then S lm *x 1 AX m+1...x p lm x 1 X 1...X m X m+1...x p lm *x 1 y 1, S lm *x 1 AX m+1...x p lm x 1 Y 1...Y n X m+1...x p lm *x 1 y 2, k: y 1 = k: y 2 where y 2 = u2v2 Thus if G is LL(k) (i.e. X 1 X m = Y 1 Y n ) then Tk(G) is SLL(k) (i.e. X 1 X m = Y 1 Y n ). (proof of the LL(k)-ness of T k (G) implies the LL(k)- ness of G.) Assume that (8) is true, then (9) [S, {$}] lm *x 1 [A, R]X m+1...x p (10) [S, {$}] * lm x 1 [A, R]X m+1 X p * lm x 1 X 1 X m X m+1 X p * lm x 1 y 1, [S, {$}] * lm x 1 [A, R]X m+1 X p * lm x 1 Y 1 Y n X m+1 X p * lm x 1 y 2, k: y 1 = k: y 2. If T k (G) is LL(k)(i.e. X 1 X m = Y 1 Y n ), then G is LL(k)(i.e. X 1 X m = Y 1 Y n ). Q.E.D.

45 3/8/ LL(k) Parsing Theorem 8.47 T k (G) is SLL(k) whenever G is LL(k). G is LL(k) whenever T k (G) is so. Proof. Consider a derivation in G S * lm xax m+1 X p. Then [S, $ k ] * lm x[a, R]X m+1 X p. X i = [X i, First k (X i+1 X n R)], if X i N, = X i, if X i. First k (X m+1 X p ) = First k (X m+1 X p ) Assume G is LL(k). A N, First k ( 1 ) First k ( 2 ) =. A 1 A 2, S * lm xa. [S, $ k ] * lm x[a, R] where k ( ) =. where k ([A, First k (

46 3/8/ LL(k) Parsing Theorem 8.48 k 1, any grammar can be transformed into an equivalent grammar that left to left covers the original grammar, and is SLL(k) iff the original is LL(k). In fact, the transformed grammar is structurally equivalent to the original grammar. Theorem 8.49 For any k, the families of LL(k) language, LALL(k) languages, and SLL(k) languages coincide. Theorem 8.50 Any LL(k) grammar is unambiguous. Proof. Let S lm w and S lm w. Assume the contrary 1 = r 1 1, 2 = r 2 2, r 1 r 2 s.t. S lm xa lm r 1 1 xy, S r lm xa 2 2 lm xy, where xy = w. As G is LL(k), r 1 = r 2. (T 8.37) Thus 1 = 2 and G is unambiguous.q.e.d. Theorem 8.51 A reduced left recursive grammar is not LL(k), k 0. Proof. (T5.40, C5.41).

47 3/8/ LL(k) Parsing Comparison on the classes of LL(k) and LR(k) grammars. S * xaz x z * xyz LR(k) parser reduce A after scanning xy and seeing k:z LL(k) parser produce A after scanning x and seeing k:yz LL(k) grammars LR(k) grammar rightmost derivation leftmost derivation (viable suffix) leftmost derivation rightmost derivation (viable prefix)

48 3/8/ LL(k) Parsing Lemma8.52 Let G is LL(k), S 1 lm x 1 A 1 r 1 lm x 1 1 1, 1 1 lm v 1, 1 * lm y 1, S 2 lm x 2 A 2 r 2 lm x 2 2 2, 2 2 lm v 2, 2 * lm y 2, x 2 v 2 = x 1 v 1 v and k:y 1 = k:vy 2. Then either 1 r 1 1 is a prefix of 2 r 2 2 or vice versa. Proof. For the sake of contradiction 1 r 1 1 and 2 r 2 2 are not prefix of each other. (case1) 1 = 2 and r 1 r 2. (case2) 1 = r 1 " 1, 2 = r 2 " 2, where r 1 r 2 (case3) 1 r 1 = 2 r 2, 1 = r 1 " 1, and 2 = r 2 " 2, where r 1 r 2 Each case is in contradiction to the assumption that G is LL(k). Lemma 8.53 Let LL(k) grammar G has S * lm x 1 A 1 1 lm x 1 1 1, 1 * lm v 1, 1 * lm y 1, S * lm x 2 A 2 2 lm x 2 2 2, 2 * lm v 2, 2 * lm y 2, x 2 v 2 = x 1 v 1 v and k: y 1 = k: vy 2. Then either 1 * v 2 or v 2 * 1. Proof. (L 8.52).

49 3/8/ LL(k) Parsing S + rm Ay vs. S + lm xa R.( * x and R * y) viable prefix induced by S + lm xa R viable suffix R induced by S rm + Ay Let be a viable prefix of G, P *. Then is a viable prefix induced by, if (p1) = and = or (p2) =, = 1 r 2,. is a viable prefix induced by 1 and S 1 lm xa R r lm x B R 2 u. lm Let be a viable suffix of G, ˆ P * then is a viable suffix induced by ˆ if (s1) = and ˆ = or (s2) = R, ˆ = ˆ1r ˆ2 s.t. is a viable suffix induced by ˆ1 and S ˆ 1 rm Ay r rm B y, ˆ 2 rm v. (refer to figures in p )

50 3/8/ LL(k) Parsing Lemma 8.54 No rule string in P * induces more than one viable prefix (resp. viable suffix). Proof. Assume induces two distinct viable prefixes 1 and 2. S lm 1 x 1 A 1 1 R lm r 1 x 1 1 B R, 1 lm 1 u 1, S lm 2 x 2 A 2 2 R lm r 2 x 2 2 B R, 2 lm 2 u 2 x 1 u 1 B R x 2 u 2 B R 1 r r 2 2 therefore x 1 u 1 x 2 u 2, B 1 B 2, 1 1 R 2 2 R. If 1 2, then 1 r r 2 2 imply that 1 r 1 is a prefix of 2 or 2 r 2 is a prefix of 1. Assume 1 r 1 is a prefix of 2, 2 1 r 1, r 2 2 1, then S 1 r 1 lm x 1 1 B 1 1 R 1 R lm x 2 A 2 2 r 2 lm x 2 2 B 2 2 R 2 2 R lm x 2 u 2 B B 1 1 R 1 can not be involved in the derivation that uses r 2 2. B 1 1 R 1 can not be involved in the derivation x 1 1 B 1 1 R 1 lm x 2 A 2 R 2 r lm x 2 2 B 2 2 R 2.

51 3/8/ LL(k) Parsing B R is a suffix of 2 R, then contradiction because 1 1 R 2 2 R. Q.E.D.

52 3/8/ LL(k) Parsing Lemma 8.55 Let G has at least one rule with left handside S, and (a) S lm xa R and R * y. Then induces some viable prefix, and is a viable suffix induced by ˆ P *, s.t. (b) S ˆ rm Ay and * x Proof. Base case, if =, immediate by definition. If and = 1 r 2, r=a 1 A then (1) S 1 lm x 1 A 1 R r lm x 1 A R x 1 2 lm x (2) S lm 1 x 1 A 1 R, R * y 1 implies(by I.H.) 1 induces some viable prefix, is a viable suffix induced by ˆ1 P*, and ˆ (3) S 1 A rm 1 y 1 and * x 1. ˆ (4) S 1 A rm 1 y 1 r ˆ rm A y 1 and 2v rm (5) S rm ˆ Avy 1 = Ay, = * x 1 u=x (6) S lm 1 x 1 A 1 R lm r x 1 A R, and lm 2 u in (4) = R, is a viable suffix induced by ˆ1 is a viable suffix induced by ˆ= ˆ1r ˆ2 in (6) =, is a viable suffix induced by 1 is a viable suffix induced by = 1 r 2 Q.E.D.

53 3/8/ LL(k) Parsing Lemma 8.56 Let G has at least one rule with left handside S, and ˆ (a) S rm Ay and * x. Then ˆ reduces some viable suffix, and is a viable prefix induced by P *, and (b) S lm xa R and R * y.

54 3/8/ LL(k) Parsing Theorem 8.57 k 0, any reduced LL(k) grammar is an LR(k) grammar. Proof. S + S impossible. Let (1) S ˆ 1 rm 1 A 1 y 1 r 1 rm 1 1 y 1 = y 1 S rm ˆ 2 2 A 2 y 2 rm r y 2 = vy 2 and k:y 1 =k:vy 2 (2) S rm ˆ 1 1 A 1 y 1, 1 * x 1 (G is reduced) S rm ˆ 2 2 A 2 y 2, 2 * x 2 (3) S lm 1x1 A 1 1 R, 1 R * y 1 (L 8.56) S lm 2x2 A 2 2 R, 2 R * y 2 where 1, 2 are viable suffix induced by ˆ1 and ˆ2 respectively, 1, 2 are viable prefix induced by 1 and 2 (4) S lm 1x1 A 1 1 R lm r 1 x R, 1 * v 1, 1 R * y 1 S lm 2x2 A 2 2 R lm r 1 x R, 2 * v 2, 2 R * y 2 x 2 v 2 =x 1 v 1 v and k:y 1 =k:vy 2 (from 3, 1) If G is LL(k) grammar. Then either 1 R derives v 2 R or v 2 R derives 1 R ( L 8.53).

55 3/8/ LL(k) Parsing (case 1) 1 R derives v 2 R (5) S lm 1x1 A 1 1 R and 1 R * v 2 R * vy 2 (6) S rm * 1 A 1 vy 2 rm 1 1 vy 2 = vy 2 S rm * 2 A 2 y 2 rm 2 2 y 2 = vy 2 G is LL(k) and hence unambiguous, then 1 A 1 vy 2 = 2 A 2 y 2, hence 1 = 2, A 1 =A 2, v=, 1 1 = = v= 2 2 = 1 2 implies 1 = 2 (1) implies 1 = 2, A 1 =A 2, and 1 = 2, so G is LR(k). (case 2) v 2 R derives 1 R the proof is analogous to that of case 1. Q.E.D.

56 3/8/ LL(k) Parsing Theorem 8.58 k 0, the class of reduced LL(k) grammars is properly contained in the class of reduced LR(k) grammars Proof. By Theorem 8.57 and LR(0) grammar may be left-recursive. Proposition 8.59 k 0, the family of LL(k) languages is properly contained in the family of LR(1) languages Theorem 8.60 k 1, the class of SLL(k) grammars is incompatible with the class of SLR(k) grammars, and the class of LALL(k) grammars is incompatible with the class of LALR(k) grammars Proof. example grammars(p. 248 ).

57 3/8/ LL(k) Parsing Construction of LL(k) Parsers Construction of canonical LL(0) machine. Let G = (V,, P, S). M 0 = (I 0 {q s }, V, q s, I f, 0 ) is the nondeterministic LL(0) machine for G, where I 0 : set of 0-items, V: input alphabet, q s I 0 : initial state, I f = I 0 : set of final states, 0 : (I 0 {q s }) (V { }) 2 I 0 of the form; q s [S ], [A X ]X [A X ], X V, and [A B ] [B ] Lemma 8.61 The set of viable suffixes of any G is the language accepted by M 0 of G, and for any viable suffix, 0 = {q q I 0 : q 0 '(q s, )} where 0 '(q, X) = {p V *, X V, r 0 '(q, ): p 0 (r, X)}.

58 3/8/ LL(k) Parsing Theorem 8.62 Deterministic LL(0) machine for any grammar G can be computed in time O(2 G +2log G ). (T 3.30) Let G = (V,, P, S). M 1 = (I 1 {q s }, V, q s, I f, 1 ) is the nondeterministic LL(1) machine for G where I 1 : set of 1-items, V: input alphabet, q s I 1 : initial state, I f = I 1 : set of final states, 1 : (I 1 {q s }) (V { }) 2 I 1 of the form; q s [S, ], [A X, y]x [A X, z], X V, z First 1 (Xy). [A B, y] [B, y] Lemma 8.63 The set of viable suffixes of any grammar G is the language accepted by M 1 of G, and for any viable suffix, 1 = {q q I 1 : q 1 '(q s, )} where 1 '(q, X) = {p V *, X V, r 1 '(q, ): p 1 (r, X)}.

59 3/8/ LL(k) Parsing Theorem 8.64 The cannonical LL(1) machine for any grammar G can be constructed in time O(2 2 G +3log G ). Theorem 8.65 The canonical LL(1) parser of any grammar G can be constructed in time O(2 2 G +3log G ).

60 3/8/ LL(k) Parsing Construction of LALL(1) Parsers To construct LALL(1) parser of a grammar G construct canonical LL(0) machine of its $-augmented grammar G' of G and lookahead symbols of states. For state q in the canonical LL(0) machine of G' Lall(q) = {b {$} [A, b] [ ] 0 1 } where Lall(q) is Lall(1) lookahead set of q. Fact 8.66 The LALL(1) parser has a produce action of the form [ ] 0 [ A] 0 b [ ] 0 [ X n ] 0... [ X n... X 1 ] 0 b iff [ X 1... X n ] X n... X 1 0 and b Lall( X n... X 1 0 ) Lemma 8.67 For any state q in the canonical LL(0) machine, Lall(q)={b First 1 ( R ) $V *, 0 = q} In other words, Lall(q) is the union of all sets First 1 ( R ). Proof. (F 8.13)

61 3/8/ LL(k) Parsing Computation of Lall(1) lookahead set q contains-item A, if A q. (q, A) goes-to Goto(q, A), if Goto(q, A) ø q has-null-transition (q, B), if Goto(q, B) ø and B is nullable. has-lall-lookahead = (goes-to -1 has-null-transition -1 ) * contains-item points first-of -1 where A X points X a first-of X if and only if a First 1 (X) X. q '.. q Portion of a canonical LL(0) machine. If is nullable and a First 1 (X) then q (goes-to -1 has-null-tranisiton -1 ) *q contains-item A X points X first-of -1 a that is, q has-lall-lookahead a

62 3/8/ LL(k) Parsing Theorem 8.68 Let G be a reduced grammar. a, a Lall(q) iff q has-lall-lookahead a. Proof. ( ) q has-lall-lookahead a = q (goes-to -1 has-null-transition -1 ) * q' contains-item points first-of -1 a for some q If q = 0 for a viable suffix, then q = 0, *. q contains-item points first-of -1 a implies that a First 1 ( R ). a Lall(q),since a First 1 ( R R ), is nullable. ( )Assume that a Lall(q), of G such that 0 =q and a First 1 ( R ). a First 1 ( R ) implies that R = R X R where R *, a First 1 (X). q (goes-to -1 has-null-transition -1 ) * q' contains-item points first-of -1 a where q = X 0. q has-lall-lookahead a. Q.E.D.

63 3/8/ LL(k) Parsing Non-Left-Recursive Grammatical Covers A reduced left-recursive grammar cannot be an LL(k) grammar. (T 8.51) Nonterminal A is directly left-recursive if the grammar has a rule A A Transformations of left-recursion to right-recursion G: A A Consider A * lm A n lm n * lm y n * lm yxn i = w L (w) = (A A ) n (A ) L (y) L (x i ) n. A rm A * rm Ax n * rm A(x i ) n n rm (x i ) n * rm yx n i. R (w) = R (y)(a ) ( R (x i )(A A )) n. G^:A A A A A lm A * lm ya lm y A * lm y n * lm yxn i = w ^L (w) = (A A ) L (y) ((A A ) L (x i )) n (A ). A rm A rm A * lm n A rm n * rm yx n i. ^R (w) = R (y) ( R (x i ))n (A ) (A A ) n (A A ).

64 3/8/ LL(k) Parsing No way to make a homomorphism that produce a cover. G : A A R 1 R 2 R 1, R 2 G^ : A R 2 A A R 1 A R 1, R 2 h(r 1 ) = A A h(r 2 ) = A h(a R 2 A ) = h(a R 1 A ) = ^L (w)=(a R 2 A ) L (y)(r 2 )((A R 1 A ) L (x i ) (R 1 )) n (A ). h( ^L (w)) = R (y)(a )( R (x i )(A A ))n = R (w) G : A R 1 A R 2 R 1, R 2

65 3/8/ LL(k) Parsing Removing of left recursion from a grammar such that a cover is obtained A reduced grammar, which is -free and does not contain any nonterminal A such that A + A,called a proper grammar Lemma 8.71 Any proper grammar G can be transformed in time O( G ) into a grammar Ĝ =(Vˆ,T,Pˆ,S) such that the following statements hold (1) Ĝ is not directly left-recursive (2) V Vˆ (3) L Ĝ (X) = L G (X) for all X V Proof. A A 1 A n 1 m, n 1, m 1 A 1 A m A, A 1 A n A Q.E.D.

66 3/8/ LL(k) Parsing Theorem 8.72 Any proper grammar G can be transformed in time O( G 2 ) into a grammar Ĝ =(Vˆ,,Pˆ,S) such that the following statements hold (1) Ĝ is not left-recursive (2) V Vˆ (3) L Ĝ (X) = L G (X) for all X V Proof. Algorithm for removing left recursion from a proper grammar G=({A 1,,A n },, P, S}. Pˆ := P; for i := 2 to n do begin for j := 1 to i-1 do if A i A j for some is in Pˆ then replace A i A j by the set of rules end {A i j A j j Pˆ }; eliminate the direst left recursion possibly caused by nnterminal A i (use the method described in the proof of L 8.71).

67 3/8/ LL(k) Parsing A non left-recursive covering grammar, G T, for a given any proper grammar G G T =(V P, P,P T,S},where P T = {A (A, ) (A, ) P}. Lemma 8.73 Let a string w ( P)*, n>0, such that (a) X n w in G T Then, (b) w = y 1 r 1 y 2 r 2 y n r n, for some y i * and r X n r 1 rm y 1 y 2 y n in G. Proof. Base n=1, immediate. Case n>0, follows from construction of G T and I.H. (a) implies that (L 4.1) X X 1 X l (X,X 1 X l ) P T. X X 1 X l (X,X 1 X l ), X i n i w i, i=1,,l, w 1 w l (X,X 1 X l )=w, and n 1 + +n l = n-1 r n r 1 = r n (X X 1 X l ) and r n-1 r 1 Q.E.D. (from I.H.)

68 3/8/ LL(k) Parsing Lemma 8.74 Let n 0, such that (a) X r n r 1 rm y in G Then, (b) y = y 1 y 2 y n, and X n y 1 r 1 y 2 r 2 y n r n in G T. Let Ĝ T =(Vˆ, P,Pˆ T,S) be a grammar obtained by removing left recursion from G T by means of theorem Let Ĝ N =(Vˆ,,PˆN,S) where PˆN = Pˆ T { r r P} Then Ĝ N left-to-right covers G w.r.t. h. where, h : PˆN * P* by: (1) h(r ) = r, where r P (2) h(a ) =, where A Pˆ T Ĝ N interprets each rule in P as a nonterminal.

69 3/8/ LL(k) Parsing Lemma 8.75 Let y be a sentence in L(Ĝ T ) and ˆ a left parse of y in Ĝ T.Then h( ˆ ) is right parse of y in G. Proof. As ˆ is left parse, then ˆ = ˆ 1(r 1 ) ˆ 2(r 2 ) ˆ n(r n ) ˆ n+1 S lm ˆ 1 y 1 r 1 1 lm y 1 1 lm ˆ 2 y 1 y 2 r 2 2 lm y 1 y 2 2 lm ˆ n y 1 y 2 y n r n n lm y 1 y 2 y n n lm ˆ n+1 y 1 y 2 y n y n+1 = y, where ˆ n+1 =, y n+1 = = n (rule in PˆT do not derive * ) S ˆ 1 ˆ n lm y 1 r 1 y n r n in Ĝ T S + lm y 1 r 1 y n r n in G T. (L 8.72) S r n r 1 rm y 1 y n in G. ( L 8.73) h( ˆ )=r 1 r n Q.E.D.

70 3/8/ LL(k) Parsing Lemma 8.76 Let y be a sentence in L(G) and a right parse of y in G.Then y has in Ĝ N a left parse ˆ such that h( ˆ )=. Proof. = r 1 r n, S rm r n r 1 y in G, is right parse, S n y 1 r 1 y n r n in G T, y 1 y n =y, (L 8.74) S + y 1 r 1 y n r n in Ĝ T, (L 8.72) S lm y 1 r 1 y n r n in G T, (L 4.2)where PˆT *. Get ˆ a left parse of Ĝ N from by inserting r i at that place in where r i become the first nonterminal in the derivation. ˆ = ˆ 1(r 1 ) ˆ n(r n ), where ˆ 1 ˆ n = then ˆ S lm y 1 y n = y in Ĝ N with h( ˆ )= Q.E.D. Theorem 8.77 For any proper grammar G, there is a non-left-recursive grammar Ĝ and a homorphism h such that (Ĝ,h) is a left-to-right cover to G.

71 3/8/ LL(k) Parsing Predictive LR(k) Grammars Converting a proper grammar into LL(k) form. A L X if A P, X V { }, 1: = X -augmented grammar G G =(N {S } { P {S S}, S The LL-transformed grammar T LL (G) for G T LL (G)=(V t,,p t,[s, ]) If A Z X P,, V *, X, Z V. Then (1) [A, Z ] a[a, Z a], where X L * a, a. (2) [A, Z Y] [B,Y][A, Z B] where X L * B, B L Y, Y V { }. (3) [A, Z X ].

72 3/8/ LL(k) Parsing Assume X=B 0 L B 1 L B 2 L L B n L a (1)[A, Z ] a[a, Z a] (2)[A, Z a] [B n, a] [A, Z B n ] [A, Z B n ] [B n-1, B n ] [A, Z B n-1 ] [A, Z B 2 ] [B 1, B 2 ] [A, Z B 1 ] [A, Z B 1 ] [B 0, B 1 ] [A, Z B 0 ] A Z B 0 * xb 0 xb 1 1 xb * xb n n 1 xa n+1 n 1 * xay n+1 n 1 * xay n+1 y n y 1 * xay n+1 y n y 1 z [A, Z ] a[a, Z a] a[b n, a] [A, Z B n ] * ay n+1 [A, Z B n ] ay n+1 [B n-1, B n ] [A, Z B n-1 ] * ay n+1 y n [A, Z B n-1 ] * ay n+1 y n y 2 [B 0, B 1 ] [A, Z B 0 ] * ay n+1 y n y 2 y 1 [A, Z B 0 ] * ay n+1 y n y 2 y 1 z

73 3/8/ LL(k) Parsing Lemma 8.78 T LL (G) is of size O( G 2 ), can be constructed from G in time O( G 2 ). G assign : S S S i A i B S L i A A * P P A L A, A L P B A = A B L A P i (A) P L i, P L ( T LL (G assign ) is: [S, ] i[s, i] [S, i] [S,i][S, S] [S, S] [S,i] [S,i ] [S,i ] ([S,i (] i[s,i i] [S,i (] [P,(][S,i P] [S,i i] [P,i][S,i P] [S,i P] [A,P][S,i A] [S,i A] [A,A][S,i A] [B,A][S,i B] [S,i B]

74 3/8/ LL(k) Parsing E E E E + T T T T * F F F a ( E ) [E, ] a [E, a] ( [E, (] a ( [E, a] [F, a] [E, F] [E, (] [F, (], [E, F] [E, F] [T, F] [E, T] [E, T] [T, T] [E, T] [E, T] [E, E] * +, $ [E, E] [E, E] [E, E] + $ [E, E] + [E, E+] [E, E+] a [E, E+a] ( [E, E+(] a ( [E, E+a] [F, a] [E, E+F] [E, E+(] [F, (] [E, E+F] [E, E+F] [T, F] [E, E+T] [E, E+T] [T, T] [E, E+T] * +, $ [E, T] [T, T] * [T, T*] [T, T*] a [T, T*a] ( [T, T*(] a ( [T, T*a] [F, a] [T, T*F] [T, T*(] [F, (] [T, T*F] [T, T*F] [T, F] [F, (] a [F, (a] ( [F, ((] a ( [F, (a] [F, a] [F, (F] [F, ((] [F, (] [F, (F] [F, (F] [T, F] [F, (T] [F, (T] [T, T] [F, (T] [E, T] [F, (E] * +, $ [F, (E] [E, E] [E, (E] ) [F, (E)] + ) [F, (E)] [F, a]

75 3/8/ LL(k) Parsing [E, ] a [E, a] a [F, a] [E, F] a [E, F] (F a) a [T, F] [E, T] a [E, T] (T F) a [E, T] [E, E] a [E, E] (E T) a [E, E] [E, E] a + [E, E+] [E, E] a+ a [E, E+a] [E, E] a+a [F, a] [E, E+F] [E, E] a+a [E, E+F] [E, E] (F a) a+a [T, F] [E, E+T] [E, E] a+a [E, E+T] [E, E] (T F) a+a [T, T] [E, E+T] [E, E] a+a * [T, T*] [E, E+T] [E, E] a+a* a [T, T*a] [E, E+T] [E, E] a+a*a [F, a] [T, T*F] [E, E+T] [E, E] a+a*a [T, T*F] [E, E+T] [E, E] (F a) a+a*a [E, E+T] [E, E] (T T*F) a+a*a [E, E] (E E+T) a+a*a (E E)

76 3/8/ LL(k) Parsing Let homomorphism, h T from the rule strings of T LL (G) to the rule strings of G h T ([A, ] ) = A, = and A P; =, otherwise. Then T LL (G) left-to-right covers G w.r.t. homomorphism h T. Lemma 8.79 Let G be -free, T a rule string of T LL (G), r T a rule of T LL (G) and (a) [A, Y] T r T lm x in T LL (G), Y V { } Then (b) [A, Y] T r lm x[a, X ] Tx lm in TLL (G), X rm h T ( T ) R Yx in G Proof. induction on T Base case, if T =, r T =[A, Y] x, x *, so x= and A Y in G, X=Y, =,so X Y. immediately (a) implies (b).

77 3/8/ LL(k) Parsing (case 1) the first rule in T is of type 1 let r T = [A, Y] a[a, Ya] T =r T T, x=ax where [A, Ya] T r T x lm in TLL (G) by I.H. [A, Ya] T lm x [A, YX ] r T x lm in TLL (G) X h T ( T ) R rm ax in G then [A, Y] T lm ax [A, YX ] r T lm ax in T LL (G) YX h T ( T ) R rm Yax = Yx in G observe that h T ( T ) R =h T (r T T ) R =h T ( T ) R because h T (r T )= for type 1 (b) holds when we choose X=Y and =X

78 3/8/ LL(k) Parsing case 2: the first rule in T is of type 2 let r T = [A, Y] [B,Y][A, B] T r T =r T T r" T " T r T, x=x x" where [B,Y] T r" T lm x and [A, B] " T r T lm x" in T LL (G) by I.H. [B,Y] T lm x [B,X ] r" T lm x in T LL (G) X rm h T ( T ) R Yx in G [A, B] lm " T x"[a, X ] lm r T x" in T LL (G) h X T ( " T ) R rm Bx" in G then [A, Y] T lm x x"[a, X ] r T lm x x" = x in T LL (G) X rm h T ( " T ) R Bx" rm r" X x" rm h T ( T ) R Yx x" = Yx in G observe that r"=b X, h T (r" T )=r" h T ( " T ) R r"h T (r T ) R = (h T (r T )h T ( T )h T (r" T )h T ( " T )) R = h T ( T ) R where h T (r T ) = (r T is of type 2). Q.E.D.

79 3/8/ LL(k) Parsing Lemma 8.80 L(T LL (G)) L(G) moreover left parse of sentence w in T LL (G), h T ( ) is a right parse of w in G. Proof. in Lemma 8.79, let A=S, =, Y=, x=w, T r T = s.t. [S, ] lm Tw[S,X ] lm r T w in T LL (G) and h X T ( T ) R rm w in G. Then S h T ( T ) R rm w in G h T ( T ) R = h T ( T r T ) R = h T ( ) R, because h T (r T )=, r T =[S, S] Q.E.D. Lemma 8.81 Let G be an -free grammar A rm X and X rm Yx in G. Then, [A, Y] lm Tx[A, X ] in TLL (G) and h T ( T )= R Proof. induction on. Lemma 8.82 L(G) L(T LL (G)),and right parse of sentence w in G, left parse of w in T LL (G) such that h T ( ) =.

80 3/8/ LL(k) Parsing Theorem 8.83 (T LL (G), h T ) is a left-to-right cover of G. A grammar G is a Predictive LR(k) grammar (PLR(k)), k 1,if the transformed grammar T LL (G) is LL(k) Proposition 8.84 k 1, LL(k) grammar PLR(k) grammar LR(k) grammar Intuitive characterization PLR(k) grammar Let A X S * waz, X * x, * y (S * wxyz) LL(k) parser emits the rule A X after scanning w, seeing k:xyz PLR(k) parser recorgnize the rule A X after scanning wx, seeing k:yz LR(k) parser recognize the rule A X after scanning wxy, seeing k:z

81 3/8/ LL(k) Parsing Proposition 8.85 If G is proper, then T LL (G) is nonleft-recursive. A grammar is left-factored if it has no two distinct rules A 1 and A 2. Fact 8.86 For any G, T LL (G) is left-factored. Theorem 8.87 Left-factoring cannot produce a PLR(k) grammar from a non-plr(k) grammar, and a PLR(k) grammar cannot be converted into a non- PLR(k) gramma by left-factoring. Proof. Process of constructing T LL (G) also involves the process of left-factoring.

Theory of Computation - Module 3

Theory of Computation - Module 3 Theory of Computation - Module 3 Syllabus Context Free Grammar Simplification of CFG- Normal forms-chomsky Normal form and Greibach Normal formpumping lemma for Context free languages- Applications of

More information

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class LR2: LR(0) Parsing LR Parsing CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Parsing - Roadmap Parser: decision procedure: builds a parse tree Top-down vs. bottom-up

More information

Fundamentele Informatica II

Fundamentele Informatica II Fundamentele Informatica II Answer to selected exercises 5 John C Martin: Introduction to Languages and the Theory of Computation M.M. Bonsangue (and J. Kleijn) Fall 2011 5.1.a (q 0, ab, Z 0 ) (q 1, b,

More information

Chapter 4: Bottom-up Analysis 106 / 338

Chapter 4: Bottom-up Analysis 106 / 338 Syntactic Analysis Chapter 4: Bottom-up Analysis 106 / 338 Bottom-up Analysis Attention: Many grammars are not LL(k)! A reason for that is: Definition Grammar G is called left-recursive, if A + A β for

More information

Parsing -3. A View During TD Parsing

Parsing -3. A View During TD Parsing Parsing -3 Deterministic table-driven parsing techniques Pictorial view of TD and BU parsing BU (shift-reduce) Parsing Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1) Problems with

More information

Fundamentele Informatica 3 Antwoorden op geselecteerde opgaven uit Hoofdstuk 7 en Hoofdstuk 8

Fundamentele Informatica 3 Antwoorden op geselecteerde opgaven uit Hoofdstuk 7 en Hoofdstuk 8 Fundamentele Informatica 3 Antwoorden op geselecteerde opgaven uit Hoofdstuk 7 en Hoofdstuk 8 John Martin: Introduction to Languages and the Theory of Computation Jetty Kleijn Najaar 2008 7.1 (q 0,bbcbb,Z

More information

CS Pushdown Automata

CS Pushdown Automata Chap. 6 Pushdown Automata 6.1 Definition of Pushdown Automata Example 6.2 L ww R = {ww R w (0+1) * } Palindromes over {0, 1}. A cfg P 0 1 0P0 1P1. Consider a FA with a stack(= a Pushdown automaton; PDA).

More information

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write

More information

CSE 468, Fall 2006 Homework solutions 1

CSE 468, Fall 2006 Homework solutions 1 CSE 468, Fall 2006 Homework solutions 1 Homework 1 Problem 1. (a) To accept digit strings that contain 481: Q ={λ,4,48, 481}, Σ ={0,1,...,9}, q 0 = λ, A ={481}. To define δ, weuse a for all letters (well,

More information

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Lecture 12 Simplification of Context-Free Grammars and Normal Forms Lecture 12 Simplification of Context-Free Grammars and Normal Forms COT 4420 Theory of Computation Chapter 6 Normal Forms for CFGs 1. Chomsky Normal Form CNF Productions of form A BC A, B, C V A a a T

More information

Properties of Context-free Languages. Reading: Chapter 7

Properties of Context-free Languages. Reading: Chapter 7 Properties of Context-free Languages Reading: Chapter 7 1 Topics 1) Simplifying CFGs, Normal forms 2) Pumping lemma for CFLs 3) Closure and decision properties of CFLs 2 How to simplify CFGs? 3 Three ways

More information

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100 EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write

More information

Theory of Computation

Theory of Computation Fall 2002 (YEN) Theory of Computation Midterm Exam. Name:... I.D.#:... 1. (30 pts) True or false (mark O for true ; X for false ). (Score=Max{0, Right- 1 2 Wrong}.) (1) X... If L 1 is regular and L 2 L

More information

CS Rewriting System - grammars, fa, and PDA

CS Rewriting System - grammars, fa, and PDA Restricted version of PDA If (p, γ) δ(q, a, X), a Σ {ε}, p. q Q, X Γ. restrict γ Γ in three ways: i) if γ = YX, (q, ay, Xβ) (p, y, YXβ) push Y Γ, ii) if γ = X, (q, ay, Xβ) (p, y, Xβ) no change on stack,

More information

download instant at Assume that (w R ) R = w for all strings w Σ of length n or less.

download instant at  Assume that (w R ) R = w for all strings w Σ of length n or less. Chapter 2 Languages 3. We prove, by induction on the length of the string, that w = (w R ) R for every string w Σ. Basis: The basis consists of the null string. In this case, (λ R ) R = (λ) R = λ as desired.

More information

Theory of Computation Turing Machine and Pushdown Automata

Theory of Computation Turing Machine and Pushdown Automata Theory of Computation Turing Machine and Pushdown Automata 1. What is a Turing Machine? A Turing Machine is an accepting device which accepts the languages (recursively enumerable set) generated by type

More information

Miscellaneous. Closure Properties Decision Properties

Miscellaneous. Closure Properties Decision Properties Miscellaneous Closure Properties Decision Properties 1 Closure Properties of CFL s CFL s are closed under union, concatenation, and Kleene closure. Also, under reversal, homomorphisms and inverse homomorphisms.

More information

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X } Let s try building an SLR parsing table for another simple grammar: S XaY Y X by c Y X S XaY Y X by c Y X Canonical collection: I 0 : {S S, S XaY, S Y, X by, X c, Y X} I 1 : {S S } I 2 : {S X ay, Y X }

More information

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET Regular Languages and FA A language is a set of strings over a finite alphabet Σ. All languages are finite or countably infinite. The set of all languages

More information

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7 Compiler Design 1 LR Parsing Compiler Design 2 LR(0) Parsing An LR(0) parser can take shift-reduce decisions entirely on the basis of the states of LR(0) automaton a of the grammar. Consider the following

More information

Pushdown Automata. Reading: Chapter 6

Pushdown Automata. Reading: Chapter 6 Pushdown Automata Reading: Chapter 6 1 Pushdown Automata (PDA) Informally: A PDA is an NFA-ε with a infinite stack. Transitions are modified to accommodate stack operations. Questions: What is a stack?

More information

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska LECTURE 14 SMALL REVIEW FOR FINAL SOME Y/N QUESTIONS Q1 Given Σ =, there is L over Σ Yes: = {e} and L = {e} Σ Q2 There are uncountably

More information

This lecture covers Chapter 7 of HMU: Properties of CFLs

This lecture covers Chapter 7 of HMU: Properties of CFLs This lecture covers Chapter 7 of HMU: Properties of CFLs Chomsky Normal Form Pumping Lemma for CFs Closure Properties of CFLs Decision Properties of CFLs Additional Reading: Chapter 7 of HMU. Chomsky Normal

More information

Bottom-up Analysis. Theorem: Proof: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k.

Bottom-up Analysis. Theorem: Proof: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Bottom-up Analysis Theorem: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k. Proof: Let A Aβ α P and A be reachable from S Assumption: G is LL(k) A n A S First k (αβ n γ) First

More information

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id Bottom-Up Parsing Attempts to traverse a parse tree bottom up (post-order traversal) Reduces a sequence of tokens to the start symbol At each reduction step, the RHS of a production is replaced with LHS

More information

SFWR ENG 2FA3. Solution to the Assignment #4

SFWR ENG 2FA3. Solution to the Assignment #4 SFWR ENG 2FA3. Solution to the Assignment #4 Total = 131, 100%= 115 The solutions below are often very detailed on purpose. Such level of details is not required from students solutions. Some questions

More information

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor 60-354, Theory of Computation Fall 2013 Asish Mukhopadhyay School of Computer Science University of Windsor Pushdown Automata (PDA) PDA = ε-nfa + stack Acceptance ε-nfa enters a final state or Stack is

More information

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University Simplification of CFG and Normal Forms Wen-Guey Tzeng Computer Science Department National Chiao Tung University Normal Forms We want a cfg with either Chomsky or Greibach normal form Chomsky normal form

More information

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University Simplification of CFG and Normal Forms Wen-Guey Tzeng Computer Science Department National Chiao Tung University Normal Forms We want a cfg with either Chomsky or Greibach normal form Chomsky normal form

More information

MA/CSSE 474 Theory of Computation

MA/CSSE 474 Theory of Computation MA/CSSE 474 Theory of Computation CFL Hierarchy CFL Decision Problems Your Questions? Previous class days' material Reading Assignments HW 12 or 13 problems Anything else I have included some slides online

More information

HW6 Solutions. Micha l Dereziński. March 20, 2015

HW6 Solutions. Micha l Dereziński. March 20, 2015 HW6 Solutions Micha l Dereziński March 20, 2015 1 Exercise 5.5 (a) The PDA accepts odd-length strings whose middle symbol is a and whose other letters are as and bs. Its diagram is below. b, Z 0 /XZ 0

More information

CS 406: Bottom-Up Parsing

CS 406: Bottom-Up Parsing CS 406: Bottom-Up Parsing Stefan D. Bruda Winter 2016 BOTTOM-UP PUSH-DOWN AUTOMATA A different way to construct a push-down automaton equivalent to a given grammar = shift-reduce parser: Given G = (N,

More information

Introduction to Formal Languages, Automata and Computability p.1/42

Introduction to Formal Languages, Automata and Computability p.1/42 Introduction to Formal Languages, Automata and Computability Pushdown Automata K. Krithivasan and R. Rama Introduction to Formal Languages, Automata and Computability p.1/42 Introduction We have considered

More information

CS20a: summary (Oct 24, 2002)

CS20a: summary (Oct 24, 2002) CS20a: summary (Oct 24, 2002) Context-free languages Grammars G = (V, T, P, S) Pushdown automata N-PDA = CFG D-PDA < CFG Today What languages are context-free? Pumping lemma (similar to pumping lemma for

More information

Context-Free Grammar

Context-Free Grammar Context-Free Grammar CFGs are more powerful than regular expressions. They are more powerful in the sense that whatever can be expressed using regular expressions can be expressed using context-free grammars,

More information

CA320 - Computability & Complexity

CA320 - Computability & Complexity CA320 - Computability & Complexity Dav Sinclair Context-Free Grammars A context-free grammar (CFG) is a 4-tuple G = (N,Σ,S,P) where N is a set of nonterminal symbols, or variables, Σ is a set of terminal

More information

6.8 The Post Correspondence Problem

6.8 The Post Correspondence Problem 6.8. THE POST CORRESPONDENCE PROBLEM 423 6.8 The Post Correspondence Problem The Post correspondence problem (due to Emil Post) is another undecidable problem that turns out to be a very helpful tool for

More information

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY REVIEW for MIDTERM 1 THURSDAY Feb 6 Midterm 1 will cover everything we have seen so far The PROBLEMS will be from Sipser, Chapters 1, 2, 3 It will be

More information

Context-Free Grammars and Languages. Reading: Chapter 5

Context-Free Grammars and Languages. Reading: Chapter 5 Context-Free Grammars and Languages Reading: Chapter 5 1 Context-Free Languages The class of context-free languages generalizes the class of regular languages, i.e., every regular language is a context-free

More information

Finite Automata and Regular Languages

Finite Automata and Regular Languages Finite Automata and Regular Languages Topics to be covered in Chapters 1-4 include: deterministic vs. nondeterministic FA, regular expressions, one-way vs. two-way FA, minimization, pumping lemma for regular

More information

Context Free Languages. Automata Theory and Formal Grammars: Lecture 6. Languages That Are Not Regular. Non-Regular Languages

Context Free Languages. Automata Theory and Formal Grammars: Lecture 6. Languages That Are Not Regular. Non-Regular Languages Context Free Languages Automata Theory and Formal Grammars: Lecture 6 Context Free Languages Last Time Decision procedures for FAs Minimum-state DFAs Today The Myhill-Nerode Theorem The Pumping Lemma Context-free

More information

Problem 2.6(d) [4 pts] Problem 2.12 [3pts] Original CFG:

Problem 2.6(d) [4 pts] Problem 2.12 [3pts] Original CFG: Problem 2.6(d) [4 pts] S X T#X X#T T#X#T X axa bxb #T# # T at bt #T ε Problem 2.12 [3pts] Original CFG: R XRX S S atb bta T XTX X ε X a b q start ε, ε $ ε, R X ε, ε R ε, ε X ε, R S ε, T X ε, T ε ε, X a

More information

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully EXAM Please read all instructions, including these, carefully There are 7 questions on the exam, with multiple parts. You have 3 hours to work on the exam. The exam is open book, open notes. Please write

More information

CS 154. Finite Automata vs Regular Expressions, Non-Regular Languages

CS 154. Finite Automata vs Regular Expressions, Non-Regular Languages CS 154 Finite Automata vs Regular Expressions, Non-Regular Languages Deterministic Finite Automata Computation with finite memory Non-Deterministic Finite Automata Computation with finite memory and guessing

More information

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs Harry Lewis October 8, 2013 Reading: Sipser, pp. 119-128. Pushdown Automata (review) Pushdown Automata = Finite automaton

More information

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules). Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules). 1a) G = ({R, S, T}, {0,1}, P, S) where P is: S R0R R R0R1R R1R0R T T 0T ε (S generates the first 0. R generates

More information

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0,

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0, Introduction to Formal Language, Fall 2016 Due: 21-Apr-2016 (Thursday) Instructor: Prof. Wen-Guey Tzeng Homework 4 Solutions Scribe: Yi-Ruei Chen 1. Find context-free grammars for the language L = {a n

More information

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX Automata & languages A primer on the Theory of Computation Laurent Vanbever www.vanbever.eu Part 4 out of 5 ETH Zürich (D-ITET) October, 12 2017 Last week, we showed the equivalence of DFA, NFA and REX

More information

컴파일러입문 제 3 장 정규언어

컴파일러입문 제 3 장 정규언어 컴파일러입문 제 3 장 정규언어 목차 3.1 정규문법과정규언어 3.2 정규표현 3.3 유한오토마타 3.4 정규언어의속성 Regular Language Page 2 정규문법과정규언어 A study of the theory of regular languages is often justified by the fact that they model the lexical

More information

CSE 355 Test 2, Fall 2016

CSE 355 Test 2, Fall 2016 CSE 355 Test 2, Fall 2016 28 October 2016, 8:35-9:25 a.m., LSA 191 Last Name SAMPLE ASU ID 1357924680 First Name(s) Ima Regrading of Midterms If you believe that your grade has not been added up correctly,

More information

Concordia University Department of Computer Science & Software Engineering

Concordia University Department of Computer Science & Software Engineering Concordia University Department of Computer Science & Software Engineering COMP 335/4 Theoretical Computer Science Winter 2015 Assignment 3 1. In each case, what language is generated by CFG s below. Justify

More information

Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008

Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008 Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008 Name: ID #: This is a Close Book examination. Only an A4 cheating sheet belonging to you is acceptable. You can write your answers

More information

Computational Models - Lecture 4

Computational Models - Lecture 4 Computational Models - Lecture 4 Regular languages: The Myhill-Nerode Theorem Context-free Grammars Chomsky Normal Form Pumping Lemma for context free languages Non context-free languages: Examples Push

More information

Lecture 11 Context-Free Languages

Lecture 11 Context-Free Languages Lecture 11 Context-Free Languages COT 4420 Theory of Computation Chapter 5 Context-Free Languages n { a b : n n { ww } 0} R Regular Languages a *b* ( a + b) * Example 1 G = ({S}, {a, b}, S, P) Derivations:

More information

CS 373: Theory of Computation. Fall 2010

CS 373: Theory of Computation. Fall 2010 CS 373: Theory of Computation Gul Agha Mahesh Viswanathan Fall 2010 1 1 Normal Forms for CFG Normal Forms for Grammars It is typically easier to work with a context free language if given a CFG in a normal

More information

Grammars (part II) Prof. Dan A. Simovici UMB

Grammars (part II) Prof. Dan A. Simovici UMB rammars (part II) Prof. Dan A. Simovici UMB 1 / 1 Outline 2 / 1 Length-Increasing vs. Context-Sensitive rammars Theorem The class L 1 equals the class of length-increasing languages. 3 / 1 Length-Increasing

More information

Type 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic.

Type 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic. Course 7 1 Type 3 languages Regular grammars Finite automata Deterministic Nondeterministic Regular expressions a, a, ε, E 1.E 2, E 1 E 2, E 1*, (E 1 ) Type 2 languages Type 2 grammars 2 Brief history

More information

SLR(1) and LALR(1) Parsing for Unrestricted Grammars. Lawrence A. Harris

SLR(1) and LALR(1) Parsing for Unrestricted Grammars. Lawrence A. Harris SLR(1) and LALR(1) Parsing for Unrestricted Grammars Lawrence A. Harris Abstract Simple LR(1) and lookahead LR(1) phrase structure grammars are defined and corresponding deterministic two-pushdown automata

More information

Theory of Computer Science

Theory of Computer Science Theory of Computer Science C1. Formal Languages and Grammars Malte Helmert University of Basel March 14, 2016 Introduction Example: Propositional Formulas from the logic part: Definition (Syntax of Propositional

More information

Foundations of Informatics: a Bridging Course

Foundations of Informatics: a Bridging Course Foundations of Informatics: a Bridging Course Week 3: Formal Languages and Semantics Thomas Noll Lehrstuhl für Informatik 2 RWTH Aachen University noll@cs.rwth-aachen.de http://www.b-it-center.de/wob/en/view/class211_id948.html

More information

Context-Free Grammars: Normal Forms

Context-Free Grammars: Normal Forms Context-Free Grammars: Normal Forms Seungjin Choi Department of Computer Science and Engineering Pohang University of Science and Technology 77 Cheongam-ro, Nam-gu, Pohang 37673, Korea seungjin@postech.ac.kr

More information

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where Recitation 11 Notes Context Free Grammars Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x A V, and x (V T)*. Examples Problem 1. Given the

More information

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY Chomsky Normal Form and TURING MACHINES TUESDAY Feb 4 CHOMSKY NORMAL FORM A context-free grammar is in Chomsky normal form if every rule is of the form:

More information

{a, b, c} {a, b} {a, c} {b, c} {a}

{a, b, c} {a, b} {a, c} {b, c} {a} Section 4.3 Order Relations A binary relation is an partial order if it transitive and antisymmetric. If R is a partial order over the set S, we also say, S is a partially ordered set or S is a poset.

More information

Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. The stack

Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. The stack A pushdown automata (PDA) is essentially: An NFA with a stack A move of a PDA will depend upon Current state of the machine Current symbol being read in Current symbol popped off the top of the stack With

More information

Solution Scoring: SD Reg exp.: a(a

Solution Scoring: SD Reg exp.: a(a MA/CSSE 474 Exam 3 Winter 2013-14 Name Solution_with explanations Section: 02(3 rd ) 03(4 th ) 1. (28 points) For each of the following statements, circle T or F to indicate whether it is True or False.

More information

MA/CSSE 474 Theory of Computation

MA/CSSE 474 Theory of Computation MA/CSSE 474 Theory of Computation Bottom-up parsing Pumping Theorem for CFLs Recap: Going One Way Lemma: Each context-free language is accepted by some PDA. Proof (by construction): The idea: Let the stack

More information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Lecture 14 Ana Bove May 14th 2018 Recap: Context-free Grammars Simplification of grammars: Elimination of ǫ-productions; Elimination of

More information

CS375: Logic and Theory of Computing

CS375: Logic and Theory of Computing CS375: Logic and Theory of Computing Fuhua (Frank) Cheng Department of Computer Science University of Kentucky 1 Table of Contents: Week 1: Preliminaries (set algebra, relations, functions) (read Chapters

More information

Non-context-Free Languages. CS215, Lecture 5 c

Non-context-Free Languages. CS215, Lecture 5 c Non-context-Free Languages CS215, Lecture 5 c 2007 1 The Pumping Lemma Theorem. (Pumping Lemma) Let be context-free. There exists a positive integer divided into five pieces, Proof for for each, and..

More information

Context-Free and Noncontext-Free Languages

Context-Free and Noncontext-Free Languages Examples: Context-Free and Noncontext-Free Languages a*b* is regular. A n B n = {a n b n : n 0} is context-free but not regular. A n B n C n = {a n b n c n : n 0} is not context-free The Regular and the

More information

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition Salil Vadhan October 11, 2012 Reading: Sipser, Section 2.3 and Section 2.1 (material on Chomsky Normal Form). Pumping Lemma for

More information

Bottom-Up Syntax Analysis

Bottom-Up Syntax Analysis Bottom-Up Syntax Analysis Wilhelm/Maurer: Compiler Design, Chapter 8 Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-sb.de and Mooly Sagiv Tel Aviv University sagiv@math.tau.ac.il Subjects Functionality

More information

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing CMSC 330: Organization of Programming Languages Pushdown Automata Parsing Chomsky Hierarchy Categorization of various languages and grammars Each is strictly more restrictive than the previous First described

More information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Lecture 4 Ana Bove March 23rd 2018 Recap: Formal Proofs How formal should a proof be? Depends on its purpose...... but should be convincing......

More information

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer. Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer. Language Generator: Context free grammars are language generators,

More information

Pushdown Automata: Introduction (2)

Pushdown Automata: Introduction (2) Pushdown Automata: Introduction Pushdown automaton (PDA) M = (K, Σ, Γ,, s, A) where K is a set of states Σ is an input alphabet Γ is a set of stack symbols s K is the start state A K is a set of accepting

More information

Chapter 6. Properties of Regular Languages

Chapter 6. Properties of Regular Languages Chapter 6 Properties of Regular Languages Regular Sets and Languages Claim(1). The family of languages accepted by FSAs consists of precisely the regular sets over a given alphabet. Every regular set is

More information

Translator Design Lecture 16 Constructing SLR Parsing Tables

Translator Design Lecture 16 Constructing SLR Parsing Tables SLR Simple LR An LR(0) item (item for short) of a grammar G is a production of G with a dot at some position of the right se. Thus, production A XYZ yields the four items AA XXXXXX AA XX YYYY AA XXXX ZZ

More information

Computer Science 160 Translation of Programming Languages

Computer Science 160 Translation of Programming Languages Computer Science 160 Translation of Programming Languages Instructor: Christopher Kruegel Building a Handle Recognizing Machine: [now, with a look-ahead token, which is LR(1) ] LR(k) items An LR(k) item

More information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2017

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2017 Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2017 Lecture 4 Ana Bove March 24th 2017 Structural induction; Concepts of automata theory. Overview of today s lecture: Recap: Formal Proofs

More information

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad-500 014 Subject: FORMAL LANGUAGES AND AUTOMATA THEORY Class : CSE II PART A (SHORT ANSWER QUESTIONS) UNIT- I 1 Explain transition diagram, transition

More information

VTU QUESTION BANK. Unit 1. Introduction to Finite Automata. 1. Obtain DFAs to accept strings of a s and b s having exactly one a.

VTU QUESTION BANK. Unit 1. Introduction to Finite Automata. 1. Obtain DFAs to accept strings of a s and b s having exactly one a. VTU QUESTION BANK Unit 1 Introduction to Finite Automata 1. Obtain DFAs to accept strings of a s and b s having exactly one a.(5m )( Dec-2014) 2. Obtain a DFA to accept strings of a s and b s having even

More information

Introduction to Theory of Computing

Introduction to Theory of Computing CSCI 2670, Fall 2012 Introduction to Theory of Computing Department of Computer Science University of Georgia Athens, GA 30602 Instructor: Liming Cai www.cs.uga.edu/ cai 0 Lecture Note 3 Context-Free Languages

More information

Pushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata.

Pushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata. Pushdown Automata We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata. Next we consider a more powerful computation model, called a

More information

PUSHDOWN AUTOMATA (PDA)

PUSHDOWN AUTOMATA (PDA) PUSHDOWN AUTOMATA (PDA) FINITE STATE CONTROL INPUT STACK (Last in, first out) input pop push ε,ε $ 0,ε 0 1,0 ε ε,$ ε 1,0 ε PDA that recognizes L = { 0 n 1 n n 0 } Definition: A (non-deterministic) PDA

More information

3.13. PUSHDOWN AUTOMATA Pushdown Automata

3.13. PUSHDOWN AUTOMATA Pushdown Automata 3.13. PUSHDOWN AUTOMATA 317 3.13 Pushdown Automata We have seen that the regular languages are exactly the languages accepted by DFA s or NFA s. The context-free languages are exactly the languages accepted

More information

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions CFG Simplification (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions 1 Eliminating useless symbols 1. A symbol X is generating if there exists: X * w, for

More information

CPS 220 Theory of Computation

CPS 220 Theory of Computation CPS 22 Theory of Computation Review - Regular Languages RL - a simple class of languages that can be represented in two ways: 1 Machine description: Finite Automata are machines with a finite number of

More information

Automata: a short introduction

Automata: a short introduction ILIAS, University of Luxembourg Discrete Mathematics II May 2012 What is a computer? Real computers are complicated; We abstract up to an essential model of computation; We begin with the simplest possible

More information

Chap. 7 Properties of Context-free Languages

Chap. 7 Properties of Context-free Languages Chap. 7 Properties of Context-free Languages 7.1 Normal Forms for Context-free Grammars Context-free grammars A where A N, (N T). 0. Chomsky Normal Form A BC or A a except S where A, B, C N, a T. 1. Eliminating

More information

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. LR(1) Parsers Part III Last Parsing Lecture Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. LR(1) Parsers A table-driven LR(1) parser looks like source code Scanner Table-driven Parser

More information

CS375: Logic and Theory of Computing

CS375: Logic and Theory of Computing CS375: Logic and Theory of Computing Fuhua (Frank) Cheng Department of Computer Science University of Kentucky 1 Table of Contents: Week 1: Preliminaries (set algebra, relations, functions) (read Chapters

More information

LL conflict resolution using the embedded left LR parser

LL conflict resolution using the embedded left LR parser DOI: 10.2298/CSIS111216023S LL conflict resolution using the embedded left LR parser Boštjan Slivnik University of Ljubljana Faculty of Computer and Information Science Tržaška cesta 25, 1000 Ljubljana,

More information

UNIT-VI PUSHDOWN AUTOMATA

UNIT-VI PUSHDOWN AUTOMATA Syllabus R09 Regulation UNIT-VI PUSHDOWN AUTOMATA The context free languages have a type of automaton that defined them. This automaton, called a pushdown automaton, is an extension of the nondeterministic

More information

Lecture 4: More on Regexps, Non-Regular Languages

Lecture 4: More on Regexps, Non-Regular Languages 6.045 Lecture 4: More on Regexps, Non-Regular Languages 6.045 Announcements: - Pset 1 is on piazza (as of last night) - If you don t have piazza access but are registered for 6.045, send email to TAs with

More information

Notes for Comp 497 (454) Week 10

Notes for Comp 497 (454) Week 10 Notes for Comp 497 (454) Week 10 Today we look at the last two chapters in Part II. Cohen presents some results concerning the two categories of language we have seen so far: Regular languages (RL). Context-free

More information

Page 1. Compiler Lecture Note, (Regular Language) 컴파일러입문 제 3 장 정규언어. Database Lab. KHU

Page 1. Compiler Lecture Note, (Regular Language) 컴파일러입문 제 3 장 정규언어. Database Lab. KHU Page 1 컴파일러입문 제 3 장 정규언어 Page 2 목차 I. 정규문법과정규언어 II. 정규표현 III. 유한오토마타 VI. 정규언어의속성 Page 3 정규문법과정규언어 A study of the theory of regular languages is often justified by the fact that they model the lexical analysis

More information

Top-Down Parsing and Intro to Bottom-Up Parsing

Top-Down Parsing and Intro to Bottom-Up Parsing Predictive Parsers op-down Parsing and Intro to Bottom-Up Parsing Lecture 7 Like recursive-descent but parser can predict which production to use By looking at the next few tokens No backtracking Predictive

More information

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1) COSE212: Programming Languages Lecture 1 Inductive Definitions (1) Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 1 September 4, 2017 1 / 9 Inductive Definitions Inductive definition (induction)

More information