La a a la a b la a ? the 3 rd symbol of the la

This preview shows page 6 - 11 out of 27 pages.

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. { a abd , b abd , a bd , c a bcd , c b bcd , cbcd } { a abd , b abd , a bd } { c a bcd , c b bcd , c bcd } { a abd , a bcd } { b abd , b bcd } { a bd , b cd } /* 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 } */
Image of page 6

Subscribe to view the full document.

7 Lookahead Strings and Lookahead Sets Example 19.1.2. Given the following grammar G 2 : S ABCabcd , A a | λ , B b | λ , C c | λ LA( S ) = { abc abcd , ab abcd , ac abcd , bc abcd , a abcd , b abcd , c abcd , abcd } No lookahead symbol is required in selecting the only S rule A a LA( A a ) = { abc a bcd , ab abcd , ac abcd , a abcd } A λ LA( A λ ) = { bc abcd , b abcd , c abcd , abcd } The 4 th lookahead symbol is required in selecting the A rule B b LA( B b ) = { bc abcd , b abcd } B λ LA( B λ ) = { c abcd , abcd } The 1 st lookahead symbol is required in selecting the B rule C c LA( C c ) = { c abcd } C λ LA( C λ ) = { abcd } The 1 st lookahead symbol is required in selecting the C rule
Image of page 7
8 FIRST, FOLLOW, and Lookahead Sets 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) 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. FIRST k ( A ) contains prefixes of length up to k of terminal symbols (directly ) derivable from A . FOLLOW k ( A ) contains prefixes of length up to k of terminal symbols that can follow the strings derivable from A . Defn. 19.2.1. 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 }
Image of page 8

Subscribe to view the full document.

9 FIRST, FOLLOW, and Lookahead Sets Defn. 19.2.3 . 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 ) } Example 19.2.1 . Given G 2 (in Example 19.1.2), 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 ) = Example 19.2.2 . FOLLOW 1 ( S ) = FOLLOW 1 ( A ) = FOLLOW 1 ( B ) = * { a , b , c , λ } { ab , ac , bc , a , b , c , λ } { abc , ab a , ac a , bc a , a ab , b ab , c ab } { λ } 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 }
Image of page 9
10 FIRST, FOLLOW and Lookahead Sets Lemma 19.2.2 . 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 ) Lemma 19.2.4 . 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 Example : Given S aSc | bSc | λ FIRST 1 ( S ) = FOLLOW ( S ) = { a , b , λ } FIRST 2 ( S ) = { aa ,
Image of page 10

Subscribe to view the full document.

Image of page 11
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