LR(1) Item DFA - Computing Lookaheads

17,841

Solution 1

The lookaheads used in an LR(1) parser are computed as follows. First, the start state has an item of the form

S -> .w  ($)

for every production S -> w, where S is the start symbol. Here, the $ marker denotes the end of the input.

Next, for any state that contains an item of the form A -> x.By (t), where x is an arbitrary string of terminals and nonterminals and B is a nonterminal, you add an item of the form B -> .w (s) for every production B -> w and for every terminal in the set FIRST(yt). (Here, FIRST refers to FIRST sets, which are usually introduced when talking about LL parsers. If you haven't seen them before, I would take a few minutes to look over those lecture notes).

Let's try this out on your grammar. We start off by creating an item set containing

S -> .AB ($)

Next, using our second rule, for every production of A, we add in a new item corresponding to that production and with lookaheads of every terminal in FIRST(B$). Since B always produces the string d, FIRST(B$) = d, so all of the productions we introduce will have lookahead d. This gives

S -> .AB ($)
A -> .aAb (d)
A -> .a (d)

Now, let's build the state corresponding to seeing an 'a' in this initial state. We start by moving the dot over one step for each production that starts with a:

A -> a.Ab (d)
A -> a. (d)

Now, since the first item has a dot before a nonterminal, we use our rule to add one item for each production of A, giving those items lookahead FIRST(bd) = b. This gives

A -> a.Ab (d)
A -> a. (d)
A -> .aAb (b)
A -> .a (b)

Continuing this process will ultimately construct all the LR(1) states for this LR(1) parser. This is shown here:

[0]
S -> .AB  ($)
A -> .aAb (d)
A -> .a   (d)

[1]
A -> a.Ab (d)
A -> a.   (d)
A -> .aAb (b)
A -> .a   (b)

[2]
A -> a.Ab (b)
A -> a.   (b)
A -> .aAb (b)
A -> .a   (b)

[3]
A -> aA.b (d)

[4]
A -> aAb. (d)

[5]
S -> A.B  ($)
B -> .d   ($)

[6]
B -> d.   ($)

[7]
S -> AB.  ($)

[8]
A -> aA.b (b)

[9]
A -> aAb. (b)

In case it helps, I taught a compilers course last summer and have all the lecture slides available online. The slides on bottom-up parsing should cover all of the details of LR parsing and parse table construction, and I hope that you find them useful!

Hope this helps!

Solution 2

here is the LR(1) automaton for the grammar as the follow has been done above I think it's better for the understanding to trying draw the automaton and the flow will make the idea of the lookaheads clearer

here is the automaton for the grammar

Share:
17,841
mrjasmin
Author by

mrjasmin

Updated on June 06, 2022

Comments

  • mrjasmin
    mrjasmin almost 2 years

    I have trouble understanding how to compute the lookaheads for the LR(1)-items.

    Lets say that I have this grammar:

    S -> AB
    A -> aAb | a
    B -> d
    

    A LR(1)-item is an LR(0) item with a lookahead. So we will get the following LR(0)-item for state 0:

    S -> .AB , {lookahead} 
    A -> .aAb,  {lookahead} 
    A -> .a,  {lookahead}
    

    State: 1

    A ->  a.Ab, {lookahead} 
    A ->  a. ,{lookahead} 
    A -> .aAb ,{lookahead} 
    A ->.a ,{lookahead}
    

    Can somebody explain how to compute the lookaheads ? What is the general approach ?

    Thank you in advance

  • mrjasmin
    mrjasmin over 11 years
    Thank you. Can you explain why $ is included in the lookahead sets in the following grammar, but it's not in FIRST($A) ? S → •A {$} A → • AA {$, b} A → • bc {$ ,b }
  • templatetypedef
    templatetypedef over 11 years
    @mrjasmin- I need to see more of the grammar to know what the FIRST and lookahead sets should be; can you post more? Also, note that you shouldn't be computing FIRST($A) anywhere. If you have A -> .AA ($), the lookaheads for the resulting items would be the terminals in FIRST(A$), not FIRST($A). Does that help at all?
  • mrjasmin
    mrjasmin over 11 years
    Hi ! This question is from an exam and all that is given is: S -> .A {$} A -> .AA { } A -> .bc { } The student is supposed to find the lookahead set- And the answer is the post above. I don't understand how $ is a lookahead
  • templatetypedef
    templatetypedef over 11 years
    @mrjasmin- The initial $ probably comes from the fact that S is the start symbol, so its production is always marked with a $ after the fact. The production A -> .AA would therefore initially have $ as a lookahead, as would A -> bc. Next, since A -> .AA ($) is an item, you'd add in new items for each production of A, with lookaheads FIRST(A$). Since A -> bc is a production of A, the only element of FIRST(A$) is b. Thus you'd add A -> .AA (b) and A -> .bc (b) to the item set. Merging these with A -> .AA ($) and A -> .bc ($) gives A -> .AA ($, b) and A -> .bc ($, b). Does that make sense?
  • mrjasmin
    mrjasmin over 11 years
    Why would the production A -> .AA initially have $ as a lookahead ? Shouldn't A -> aAb | a also have $ initially then ? Thanks
  • templatetypedef
    templatetypedef over 11 years
    @mrjasmin- No, those two shouldn't be followed by $. The production S -> AB means that the items for the A productions start with FIRST(B$), which is just d. The reason for the $ lookaheads in this second case is that the production S -> A has nothing after the A, so the lookahead is FIRST($), which is just $.
  • Afonso Tsukamoto
    Afonso Tsukamoto almost 11 years
    This was really helpful! Thanks @templatetypedef.
  • tgoneil
    tgoneil over 8 years
    3 more states are needed, as noted below.
  • templatetypedef
    templatetypedef over 8 years
    @tgoneil I missed two of those states the first time around - thanks for spotting that! As for the third - it looks like you've augmented the grammar by adding in a production S' -> S while I haven't done that, so I think that third state depends on whether you do that or not.
  • tgoneil
    tgoneil over 8 years
    Yes it is augmented, so yes, would be one less state. :-)
  • Krupip
    Krupip over 6 years
    Your link, and every other LALR parsing site on the web appears to mention the use of follow sets not first sets in creation of the item set, are you mistaken in your usage of first sets? web.stanford.edu/class/archive/cs/cs143/cs143.1128/handouts/‌​… and web.cs.dal.ca/~sjackson/lalr1.html
  • templatetypedef
    templatetypedef over 6 years
    @snb Generating a LALR(1) parser typically does use FOLLOW sets. However, this question is about LR(1) parsing, and typically LR(1) parsers are created using FIRST rather than FOLLOW sets.
  • Krupip
    Krupip over 6 years
    @templatetypedef Ah ok, I went backwards in learning about LR parsing.
  • templatetypedef
    templatetypedef over 6 years
    @snb No worries! LR parsing is often taught in the order of LR(0), then SLR(1), then LR(1), then LALR(1), though you sometimes see LALR(1) and LR(1) interchanged with one another. It can take some practice to get the hang of them!
  • alhelal
    alhelal about 6 years
    @templatetypedef parasol.tamu.edu/~rwerger/Courses/434/lec10.pdf for this tell why E->.T+E,$