Prev Up
Go backward to The Correctness of LR(0) parsing
Go up to Top

Some Preliminaries to LR(1) parsing

  1. Consider the grammar
    < S > a < S > b < S >  | 
  2. If we build the LR(0) machine for this grammar, we discover that it is not an LR(0) grammar because several states contain shift/reduce conflicts.

    We can use this machine anyway, if we are willing to look ahead a bit.

  3. In general, suppose that we find that after reading some prefix 1 of an input 1 x 2 we end up in a state that contains a reduce item [ N . ] which conflicts with some other item.
  4. 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 }
  5. 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".
  6. 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().

Computer Science 434
Department of Computer Science
Williams College

Prev Up