Prev Up Next
Go backward to Announcements
Go up to Top
Go forward to SLR(1) Parsing

Some Preliminaries to LR(1) parsing (cont.)

  1. Preliminaries often involve definitions:
    nullable
    Given a grammar G, we say that a non-terminal N is nullable if N .
    first set
    Given a grammar G and (Vn U Vt)* we define First() to be the set of terminals that might appear as the first symbol in a string derived from . First() will include if . Thus,
    First() = { a Vt  |  a , for some ( Vn U Vt )* } U { if }
  2. Given that we have computed the set of nullable non-terminal, we can compute First for each terminal and non-terminal using a "run until nothing changes" algorithm: to compute First().
  3. Consider how to construct these sets for the grammar:
    S A B C
    A a  |  CB
    B C  |  A d  | 
    C f  | 
    • All the non-terminals are nullable:
      • B and C are directly nullable.
      • The production A B C, then implies A is nullable.
      • Then, the production S A B C implies S is nullable.
    • The productions break down as:
      S A B C
      A a
      A CB
      B C
      B A d
      B
      C f
      C
    • The FirstSet values start out as shown in the table below and can be refined by iterating over the production table.
      S A B C a d f
      { } { , a } { } { , f } { a } { d } { f }
      { } { } { } { } { a } { d } { f }

      { }

      { } { } { } { a } { d } { f }

      { }

      { } { } { } { a } { d } { f }
  4. The final preliminary is the definition of Follow:
    Follow( N ) = { x Vt  | A N x } U { if S N }
  5. The computation of Follow(N) depends of the First sets defined earlier. If
    M N
    then Follow(N) must contains First( ) . If is nullable, then Follow(N) must also contain Follow(M). These observations are enough to give us an approximation algorithm for computing Follow. See the books on reserve for details.

Computer Science 434
Department of Computer Science
Williams College

Prev Up Next