LR(1) Parsing TopSLR(1) parsingComputing Follow(N)

Computing Follow(N)

  1. If a grammar contains the rule N 1 M R2, then Follow(M) must contain the first symbols that might appear in strings derived from R. In addition, if R derives the empty string, then the first symbols that might be derived from 2 will be in Follow(M).
  2. The computation of the Follow set will depend on several other sub-computations.
    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 }
  3. The set of nullable non-terminals can be computed by the following algorithm:
    1. Set "nullable" equal to the set of non-terminals appearing on the left side of productions of the form N .
    2. Until doing so adds no new non-terminals to "nullable", examine each production in the grammar adding to "nullable" all left-hand-sides of productions whose right-hand-side consist entirely of symbols in "nullable".
  4. 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:
  5. Consider how to determine nullable, First and Follow for the grammar:

    S A B C
    A a  |  CB
    B C  |  A d  | 
    C f  | 

  6. 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

LR(1) Parsing TopSLR(1) parsingComputing Follow(N)