Notatypewriter's Blog

Umm… what?

Archive for September 2012

An algorithm for doing closure in LR(1)

leave a comment »

At least, it’s one that is simple enough for me to remember. The ones typically presented in lectures and books were confusing to me.

Mine seems to work on examples from lecture, in books, and on Wikipedia.

Define closure( item=[A→β•Bδ,a] ), where:

  • A is a non-terminal
  • β is a sentence of terminals and non-terminals
  • • is the position in the string being parsed
  • B is a non-terminal
  • δ is a sentence of terminals and non-terminals
  • , is the lookahead position
  • a is the lookahead terminal

If the item does not match this pattern, then closure returns empty.

Else, for each production B→γ and each terminal b in FIRST(δ), do closure( [B→•γ, b] ).

From Wikipedia:

FIRST(A) is the set of terminals which can appear as the first element of any chain of rules matching non-terminal A.

Read the rest of this entry »


Written by notatypewriter

2012 September 22 at 3:37 pm

Posted in Nerding out

Tagged with ,

%d bloggers like this: