RIPAL: Responsive and Intuitive Parsing for the Analysis of Language

Pages

LR(0) ε closure calculation example

Background

We've seen a couple of LR(0) production closure examples.

In this section, we will illustrate a closure calculation that involves an ε production.

Motivating example

Example

Observe the following grammar:

S → A
A → ε

This grammar has the following augmented grammar:

S' → S $
S → A
A → ε

Recall that is has the following parse table:

$ S A S'
state1 reduce3 goto3 goto2
state2 reduce2
state3 accept

Recall the following parse of input string ε:

Input queue Parse stack Action
1 Apply action of reduce3 which corresponds to state1 and $ in our parse table
1 A Apply action of goto2 which corresponds to state1 and A in our parse table
1 A 2 Apply action of reduce2 which corresponds to state2 and $ in our parse table
1 S Apply action of goto3 which corresponds to state1 and S in our parse table
1 S 3 Accept, since this action corresponds to state3 and $ in our parse table

The fundamental question

Given a qualified production as follows:

N → sympre1 sympre2 ... symprenpre · Nnext sympost1 sympost2 ... sympostnpost

The fundamental question to assess in this context is:

How far can we progress through our productions without processing any input symbols OR with shifting and reducing input symbols such that we arrive back in the initial parse state?

The fundamental answer

Starting with the qualified production:

S' → · S $

we see the typical chaining of reduce and goto actions through the following sequence of parse stack states:

  1. 1
  2. 1 A
  3. 1 A 2
  4. 1 S
  5. 1 S 3

This chaining is fairly typical, with the exception of the transition from stack state 1 to state 1 A.

In this transition, we don't need to shift any symbol onto the parse stack before reducing according to production:

A → ε

Since ε is the empty string and not an input symbol, we can remain in state1 in the processing of its corresponding production. We don't need to shift into an additional state because there is no terminal to shift.

As a result, the closure of the qualified production set:

{S' → · S $}

is:

{S' → · S $, S → · A, A → · ε, A → ε ·}

Conclusion

We've seen many examples of qualified production closure calculations.

Next, we will introduce the formal definition of this closure calculation.


GitHub Repository: https://github.com/bprollinson/ripal

Copyright © 2017 Brendan Rollinson-Lorimer