You are on page 1of 5

CONSTRUCTING SLR PARSING TABLE

Viable Prefix
In shift-reduce parsing , the stack always a viable prefix

that is, a prefix of some right-sentential form that ends no further right than the end of the handle of that rightsentential form E F * id (E) * id Valid Item An item is a production with dot somewhere in the body. An item is valid for valid prefix if the production of that item is used to generate the handle

Viable Prefix
Valid Items: an item A 1. 2 is valid for a viable prefix 1 if there is a derivation: S' =>*rm A =>*rm 1 2 Suppose A 1.2 is valid for 1, and B1 is on the parsing stack if 2 != , we should shift if 2 = , A 1 is the handle, and we should reduce by this production Note: two valid items may tell us to do different things for the same viable prefix. Some of these conflicts can be resolved using lookahead on the input string.

Constructing an SLR Parsing Table


Given a grammar G, construct the augmented grammar by

adding the production S' S. Construct C = {I0, I1, In}, the set of sets of LR items for G'. State I is constructed from Ii, with parsing action determined as follows: 1. [A .aB] is in Ii, where a is a terminal; goto(Ii,a) = Ij : set action[i,a] = "shift j" 2. [A .] is in Ii : set action[i,a] to "reduce A x" for all a in FOLLOW(A), where A != S' 3. [S' S.] is in Ii : set action[i,$] to "accept" 4. goto transitions constructed as follows: for all non-terminals: if goto(Ii, A) = Ij , then goto[i,A] = j . 5. All entries not defined by (3) & (4) are made "error". If there are any multiply defined entries, grammar is not SLR. 6. Initial state S0 of parser: that constructed from I0 or [S' .S]

SLR parse table

You might also like