0

I'm currently looking at two closure calculation examples using the tool at http://jsmachines.sourceforge.net/machines/lr1.html

Example 1

S -> A c
A -> b B
B -> A b

Here, in the initial state ends up with a closure of:

[S -> .A c, $]; [A -> .b B, c]}

Example 2

S -> A B
A -> a
B -> b
B -> ''

The calculated first step closure is:

{[S -> .A B, $]; [A -> .a, b/$]}

In example 1, why is the follow of b from rule 3 not included in the lookahead? In case 2, we follow B to figure out that $ is part of the lookahead so is there some special reason to not consider all rules in case 1?

1 Answers1

1

When doing a closure with ". A α" we use FIRST(α) as the lookahead, and only include the containing (parent) lookahead if ε ∈ FIRST(α). In example 1, ε ∉ FIRST(c), so the lookahead is just c. In example 2, ε ∈ FIRST(B), so we add the containing lookahead ($ in this case) to the lookahead.

FOLLOW is never relevant.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Thanks, this makes a lot of sense and explains how context is relevant to the parsing setup. This corresponds with a good definition I found here: http://www.cs.ecu.edu/karl/5220/spr16/Notes/Bottom-up/lr1.html. – Brendan Rollinson Aug 19 '19 at 01:43