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
Definition 1. An LR(1) item of a CFG G is a string of the form A → [α • β,a], where A → αβ is a production in G, and a is a terminal of G or the special symbol ε. The items A → [α • β,a] and A → [α • β,ε] indicate the following position of the • in the parse.
The LR(1) parser is a deterministic automaton and as such its operation is based on static state transition tables. These codify the grammar of the language it recognizes and are typically called "parsing tables". The parsing tables of the LR(1) parser are parameterized with a lookahead terminal.
Such a lookahead is a symbol that is interpreted "command like" by some processors. It allows to peek ahead, so to read and evaluate a portion of the input stream without actually forwarding the location of the stream.
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With