Presentation is loading. Please wait.

Presentation is loading. Please wait.

9/30/2014IT 3271 How to construct an LL(1) parsing table ? 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table What is the.

Similar presentations


Presentation on theme: "9/30/2014IT 3271 How to construct an LL(1) parsing table ? 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table What is the."— Presentation transcript:

1 9/30/2014IT 3271 How to construct an LL(1) parsing table ? 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table What is the first terminal symbols you will see from deriving ASb, C, a, … and so on? The idea is easy:

2 9/30/2014IT 3272 N : the set of non-terminal symbols T : the set of terminal symbols Let ω  ( N  T )*. Definition: First set of ω, First(ω), is defined as follows: First(ω) = The set of every first (the left most) terminal symbol that appears in any possible string derived from ω. If ω derives, then let  First(ω); otherwise  First(ω);. First Sets First(ω) = { a | (a  T and ω a ω’ ) or ( ω a and a = )} **

3 9/30/2014IT 3273 First-set Examples 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  1.First( ) = { } 2.First(c C ) = {c} 3.First(a) = {a} 4.First(C)= First(c C )  First( ) = {c, } 5.First(ASb) = First(aSb) = {a} 6.First(S) = First(ASb)  First(C)= {a, c, } 7.First(Sb) = (First(S)- { })  First(b) = {a, b, c}

4 9/30/2014IT 3274 Algorithm to find First Sets 1.First( ) = { } 2.First(a ω) = {a} 3.If A  ω 1 | ω 2......... | ω n First(A) = First( ω 1 )  First( ω 2 ) .....  First( ω n ) 4.Assume ω  :  First(A), then First(Aω) = First(A)  First(A), then First(Aω) = (First(A) - { })  First( ω ) N : the set of non-terminal symbols T : the set of terminal symbols Let a  T, A  N, ω i  ( N  T )*

5 9/30/2014IT 3275 More (?) algorithmic procedure First(x): if (x = ) return { }; if (x = a ω and a  T) return {a}; if (x = A, A  N and A  ω 1 | ω 2......... | ω n ) return First( ω 1 )  First( ω 2 ) .....  First( ω n ); if (x = A ω and ω  ) if (  First(A)) return First(A); else return (First(A) - { })  First( ω ); N : the set of non-terminal symbols T : the set of terminal symbols

6 9/30/2014IT 3276 N : the set of non-terminal symbols T : the set of terminal symbols let A, S  N and S is the start symbol Definition: Follow(A), the follow set of A, is defined as follows: Follow (A) = The set of every terminal symbol (including $) that can appear to the right of A in any possible sentential form derived from the start symbol. Follow Sets Follow(A) = { a | S ωAa ω’ and a  T } *

7 9/30/2014IT 3277 Rules (recursive) for finding Follow Sets 1.If S is the start symbol, then let $  Follow (S). 2.If A   B, then let Follow (A)  Follow (B). 3.If A   B   First (  ), then let First (  )  Follow (B).  First (  ), then let (First (  ) - { })  Follow (A)  Follow (B). Let a  T, A, B  N, and ,   ( N  T )* S  …  …  …  …Aa... S$S$ …Ax…  …  Bx… …Ax…  …  B  x…

8 9/30/2014IT 3278 Follow-set Examples 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  1.Follow (S) = {b, $} S is the start symbol, $  Follow (S) S  A S b, b  Follow (S) 2.Follow (A ) = {a, b, c} S  A S b, First (S b)  Follow (A). First (S b) = (First (S)- { })  First (b) = {a, b, c} 3.Follow (C) = {b, $} S  C, Follow (S)  Follow (C) C  c C, Follow (C)  Follow (C)

9 9/30/2014IT 3279 Convert rules (definitions) of follow sets into an algorithm. How? 1.If S is the start symbol, then let $  Follow (S). 2.If A   B, then let Follow (A)  Follow (B). 3.If A   B   First (  ), then let First (  )  Follow (B).  First (  ), then let (First (  ) - { })  Follow (A)  Follow (B). Let a  T, A, B  N, and ,   ( N  T )* Check every rule and work on every Non- terminal symbols appearing in the right hand side of the production rules. S  …  …  …  …Aa...

10 9/30/2014IT 32710 Principle: repeatedly updating until nothing will further change. 1.Initialize Follow(X) =  for every X  N; 2.Let Follow (S) = {$}. 3.Repeat the following procedure until every follow-set stops changing: Check every rule and work on every non-terminal symbol appeared in the right hand side of the rule. i.If A   B, and B  N, then add every element in Follow (A) into Follow (B). ii.If A   B , and B  N, then if  First (  ), then add every element in First (  ) into Follow (B). if  First (  ), then add every terminal symbol in First (  ) and Follow(A) into Follow (B). (note:  T)

11 9/30/2014 IT 32711 Construction of LL(1) parsing talbes 1.For every a  First (ω) and a , put A  ω into T [A, a] 2.If  First (ω), then for every a  Follow (A), put A  ω into T [A, a] For ever production rule A  ω, do the following : 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S A C First (ASb) = {a}, First (C) = {c, }, Follow (S) = {b, $}, 12 First (a) = {a}, 22 First (cC) = {c}, First ( ) = { }, 3 Follow (C) = {b, $} 455

12 9/30/2014IT 32712 Condition for being an LL(1) grammar: 1.First (  )  First (  ) = . 2.If  First (  ), then First (  )  Follow (A) = . For every A  N with A  , A   and    Q: Can an LL(1) grammar be ambiguous?A: No.

13 9/30/2014IT 32713 Another Example: 1.E  E + T 2.E  T 3.T  T * F 4.T  F 5.F  ( E ) 6.F  id 1.E  T E’ 2.E’  + T E’ 3.E’  4.T  F T’ 5.T’  * F T’ 6.T’  7.F  ( E ) 8.F  id

14 9/30/2014IT 32714 First and Follow Sets 1.E  T E’ 2.E’  + T E’ 3.E’  4.T  F T’ 5.T’  * F T’ 6.T’  7.F  ( E ) 8.F  id First (F) = { (, id } First (T’) = { *, } First (T) = First (FT’) = { (, id } First (E’) = { +, } First (E) = First (TE’) = {(, id } { ), $ } { +, ), $ } { +} { +,* } Follow (E) Follow (E’) Follow (T) Follow (T’) Follow (F) check those caused by first sets E to E’ and T T to T’ and F { ), $ } { +, ), $ } {+,*, ), $ } {), $ } {} {+} {} {*} {$ } {} initialization check those caused by the follow sets from the previous stage

15 9/30/2014IT 32715 LL(1) Parsing Table 1.E  T E’ 2.E’  + T E’ 3.E’  4.T  F T’ 5.T’  * F T’ 6.T’  7.F  ( E ) 8.F  id { (, id } {+, } { (, id } {*, } {(, id } E E’ T T’ F { ), $ } { +, ), $ } {+,*, ), $ } First Follow id+*()$ E E’ T T’ F 11 233 44 5666 78

16 9/30/2014IT 32716 id*(id+id) E  T E’  F T’ E’  id T’ E’  id * F T’ E’  id * ( E ) T’ E’  id * ( T E’ ) T’ E’  id * ( F T’ E’ ) T’ E’  id * ( id T’ E’ ) T’ E’  id * ( id E’ ) T’ E’  id * ( id + TE’ ) T’ E’  id * ( id + F T’ E’ ) T’ E’  id * ( id + id T’ E’ ) T’ E’  id * ( id + id E’ ) T’ E’  id * ( id + id ) T’ E’  id * ( id + id ) E’  id * ( id + id ) 1.E  T E’ 2.E’  + T E’ 3.E’  4.T  F T’ 5.T’  * F T’ 6.T’  7.F  ( E ) 8.F  id The entry [E, id] in the Parsing table: LL(1)_PT[E, id] = 1 id+*()$ E11 E’233 T44 T’6566 F87 left-most derivation


Download ppt "9/30/2014IT 3271 How to construct an LL(1) parsing table ? 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table What is the."

Similar presentations


Ads by Google