Ab ac ba bb bc ? c ? follow 2 s c cc ? 11 ll k

This preview shows page 10 - 18 out of 27 pages.

ab , ac , ba , bb , bc , λ } { c , λ } FOLLOW 2 ( S ) = { c , cc , λ }
Image of page 10

Subscribe to view the full document.

11 LL( K ) Grammars Theorem 19.2.5. 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 ))
Image of page 11
12 19.4. Construction of FIRST k Sets Algorithm 19.4.1 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
Image of page 12

Subscribe to view the full document.

13 19.4. Construction of FIRST k Sets Example 19.4.1 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 } { ad , bc , aa , ab , ac , bb } { λ , bc , bb } { ad , ac } { ad , bc , aa , ab , ac , bb } { ad , bc , aa , ab , ac , bb } { λ , bc , bb } { ad , ac } F ( S ) F ( A ) F ( B ) F ( C ) 0 1 2 3 4 5 φ φ φ φ φ { λ } φ F ’( a ) = a F ’( b ) = b F ’( c ) = c F ’( d ) = d F ’(#) = # F ( S ) = φ F ( A ) = φ F ( B ) = { λ } F ( C ) = φ
Image of page 13
14 19.4. Construction of FOLLOW k Sets Algorithm 19.5.1 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 3.2.1. L := FL’( A ) 3.2.2. if u n V , then FL( u n ) := FL( u n ) L 3.2.3. for i := n – 1 to 1 do 3.2.3.1. L := trunc k (FIRST k ( u i +1 ) L ) 3.2.3.2. 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 )
Image of page 14

Subscribe to view the full document.

15 19.5. Construction of FOLLOW k Sets Example 19.5.1 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 ##, d #, dd ad , ac , ca, cc ##, d #, dd ad , ac , ca, cc ##, d #, dd FL ( S ) FL ( A ) FL ( B ) FL ( C ) 0 1 { } { } 2 { } { } { } { } 3 { } { } { } { } 4 { } { } { } { } 5 { } { } { } { } { λ } φ φ φ φ φ 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 * *
Image of page 15
16 19.5 Construction of LA k Sets Example 19.5.2 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 } { 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 }
Image of page 16

Subscribe to view the full document.

17 19.3 Strong LL( K ) Grammars In strong LL( k ) grammars 2200 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 19.3.1 Let G = ( V , , P , S ) be a CFG w/ endmarker # k . G is strong
Image of page 17
Image of page 18
You've reached the end of this preview.
  • Winter '12
  • DennisNg
  • Grammar, Context-free grammar, Bottom-up parsing, lookahead sets

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern