Dealing with infinite loops when constructing states for LR(1) parsing

Posted by Bruce on Stack Overflow See other posts from Stack Overflow or by Bruce
Published on 2010-04-28T12:57:32Z Indexed on 2010/04/28 13:03 UTC
Read the original article Hit count: 237

Filed under:
|
|
|

I'm currently constructing LR(1) states from the following grammar.

S->AS
S->c
A->aA
A->b

where A,S are nonterminals and a,b,c are terminals.

This is the construction of I0

I0: S' -> .S, epsilon
    ---------------
    S -> .AS, epsilon
    S -> .c, epsilon
    ---------------
    S -> .AS, a
    S -> .c, c
    A -> .aA, a
    A -> .b, b

And I1.

From S, I1: S' -> S., epsilon  //DONE

And so on. But when I get to constructing I4...

From a, I4: A -> a.A, a
        -----------
        A -> .aA, a
        A -> .b, b

The problem is A -> .aA

When I attempt to construct the next state from a, I'm going to once again get the exact same content of I4, and this continues infinitely. A similar loop occurs with

S -> .AS

So, what am I doing wrong? There has to be some detail that I'm missing, but I've browsed my notes and my book and either can't find or just don't understand what's wrong here. Any help?

© Stack Overflow or respective owner

Related posts about parsing

Related posts about lr1