TopLR(1) Parsing Building an LR(1) Machine

Building an LR(1) Machine

  1. First, we need to extend the definitions which we used to define the transition function for an LR(0) machine to account for the lookaheads we have added to LR(1) items.
    goto
    Given a set of LR(1) items for a grammar G, we define
    goto( , x ) = { [ N 1 x . 2 , a ]  | [ N 1 . x 2 , a] }
    closure
    Given a set of LR(1) items for a grammar G with productions P, we define closure() to be the smallest set of LR(1) items such that:
    1. closure()
    2. if [N1 1 . N2 2,a] closure() and N2 3 P then, for each b First( 2 a ), [N2 . 3, b] closure()
  2. With these definitions, it should be obvious, that the next step is to define the LR(1) finite automaton for a grammar G consisting of:
  3. The notions of a kernel item and a reduce item transfer naturally from LR(0) items to LR(1) items.
  4. Note that the language accepted by the LR(1) FSM is the same as that accepted by the LR(0) machine (i.e. the set of viable prefixes). The extra states in the machine, however, include information that can be used to make a better parser.
  5. Consider what happens when we build the LR(1) machine for the non-SLR(1) grammar considered earlier.

    E ( L , E )
    E S
    L L , E
    L E
    S ident
    S ( S )

  6. A set of LR(1) items contains a conflict if it contains a reduce items of the form [ N 1 . , x ] and either another reduce item of the form [ M 2 . , x ] or a shift item of the form [ M alpha. x 2 , y ].
  7. We say that a grammar is LR(1) if the reachable states in its LR(1) machine are conflict free.
  8. Given an LR(1) grammar, its LR(1) parser, shifts in state with input x if state contains a shift item of the form [ N alpha. x , a], reduces using production N if state contains a reduce item of the form [ N . , x ] and reports error otherwise.

Computer Science 434
Department of Computer Science
Williams College

TopLR(1) Parsing Building an LR(1) Machine