0% found this document useful (0 votes)
2 views

Syntax Analysis II 2024 Student Copy

The document discusses bottom-up parsing techniques, specifically focusing on shift-reduce parsing and LR parsing methods. It explains the process of constructing parse trees, reducing strings to grammar symbols, and handling conflicts during parsing. Additionally, it covers the construction of LR(0) automata and SLR parsing tables, providing examples and algorithms for implementation.

Uploaded by

gnananya2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Syntax Analysis II 2024 Student Copy

The document discusses bottom-up parsing techniques, specifically focusing on shift-reduce parsing and LR parsing methods. It explains the process of constructing parse trees, reducing strings to grammar symbols, and handling conflicts during parsing. Additionally, it covers the construction of LR(0) automata and SLR parsing tables, providing examples and algorithms for implementation.

Uploaded by

gnananya2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

SYNTAX ANALYSIS

BOTTOM-UP PARSING

Dr. R Guru
Associate Professor
Dept. of Computer Science and Engineering,
SJCE, JSSSTU, Mysuru
BOTTOM-UP PARSING
A bottom-up parse corresponds to the construction of
a parse tree for an input string beginning at the leaves
(bottom) and working up towards the root ( top).
Consider the grammar
E→E+T|T
T→T*F|F
F →( E )| id
The sequence of tree snapshots for a bottom-up parse
tree for the input string id*id
Reductions
Bottom-up parsing is the process of "reducing" a string w
to the start symbol of the grammar.
At each reduction step, a specific substring matching the
body of a production is replaced by the nonterminal at the
head of that production.
The key decisions during bottom-up parsing are about
when to reduce and about what production to apply, as the
parse proceeds
Consider the sequence of strings
id * id, F * id, T * id, T * F, T, E
Consider the bottom up parse for id * id
id * id, F * id, T * id, T * F, T, E

Derivation for the corresponding to the parse


E → T → T * F → T * id →F * id → id * id

Bottom up parse is like right most derivation in reverse


order.
Handle Pruning
Bottom-up parsing during a left-to-right scan of the input
constructs a rightmost derivation in reverse.
 A "handle" is a substring that matches the body of a
production, and whose reduction represents one step along
the reverse of a rightmost derivation.
Handles during a parse of id1 * id2
Right Sentential Handle Reducing Production
form

id1 * id2 id1 F → id


F * id2 F T→ F
id2 F → id
T * id2 T→T*F
T*F T*F
E→T
T T
Shift-Reduce Parsing
Shift-reduce parsing is a form of bottom-up parsing in
which a stack holds grammar symbols and an input buffer
holds the rest of the string to be parsed.
The handle always appears at the top of the stack just
before it is identified as the handle.
$ is used to mark the bottom of the stack and also the
right end of the input.
Initially, the stack is empty, and the string w is on the
input, as follows:
STACK INPUT
$ w$
Shift-Reduce Parsing
During a left-to-right scan of the input string, the parser
shifts zero or more input symbols onto the stack, until it is
ready to reduce a string β of grammar
symbols on top of the stack.
It then reduces β to the head of the appropriate
production.
The parser repeats this cycle until it has detected an error
or until the stack contains the start symbol and the input is
empty:
STACK INPUT
$S $
Shift-Reduce Parsing
Upon entering this configuration, the parser halts and
announces successful completion of parsing.
There are four possible actions in a shift-reduce parser:
(1) Shift,
(2) Reduce,
(3) Accept,
(4) Error.
Shift-Reduce Parsing
1. Shift. Shift the next input symbol onto the top of the
stack.
2. Reduce. The right end of the string to be reduced must be
at the top of the stack. Locate the left end of the string
within the stack and decide with what nonterminal to
replace the string.
3. Accept. Announce successful completion of parsing.
4. Error. Discover a syntax error and call an error recovery
routine.
Shift-Reduce Parsing
STACK INPUT ACTION
$ id1 * id2 $ shift
$id1 * id2 $ reduce by F→id
$F * id2 $ reduce by T →F
$T * id2 $ shift
$T* id2 $ shift
$ T * id2 $ reduce by F →id
$T*F $ reduce by T→ T * F
$T $ reduce by E→T
$E $ accept
Conflicts During Shift-Reduce Parsing
Every shift-reduce parser for such a grammar can reach a
configuration in which the parser, knowing the entire
stack contents and the next input symbol, cannot decide
whether to shift or to reduce (a shift/reduce conflict), or
cannot decide which of several reductions to make (a
reduce/reduce conflict).
Shift/Reduce conflict
Reduce/Reduce conflict
Shift/Reduce Conflict
STACK INPUT ACTION
$ id1 * id2 $ shift
$id1 * id2 $ reduce by F→id
$F * id2 $ reduce by T →F
$T * id2 $ shift / Reduce
$T* id2 $ shift
$ T * id2 $ reduce by F →id
$T*F $ reduce by T→ T * F
$T $ reduce by E→T
$E $ accept
Reduce/Reduce Conflict
Consider the grammar
S→Aa|bAc|Bc|bBa
A→d
B→d
and string to be parse is “ dbc”

STACK INPUT ACTION


$ dbc $ shift
$d bc Reduce / Reduce
conflict by
A→d
B→d
Introduction to LR Parsing:
The most of the bottom-up parser is based on a concept
called LR(K) parsing:
"L" is for left-to-right scanning of the input,
"R" for constructing a rightmost derivation in reverse, and
the k for the number of input symbols of lookahead that
are used in making parsing decisions.
When (k) is omitted, k is assumed to be 1.
Items and the LR(0) Automaton
How does a shift-reduce parser know when to shift and
when to reduce?
An LR parser makes shift-reduce decisions by
maintaining states to keep track of where we are in a
parse. States represent sets of "items."
An LR(0) item of a grammar G is a production of G with
a dot at some position of the body. Consider the
Production A →XYZ yields the four items
A →.XYZ
A →X.YZ
A →XY.Z
A →XYZ.
Items and the LR(0) Automaton
The production A → ɛ generates only one item, A → •
The item A →.XYZ indicates that we hope to see a string
derivable from XYZ next on the input.
Item A → X.YZ indicates that we have just seen on the
input a string derivable from X and that we hope next to
see a string derivable from YZ.
Item A → XY.Z indicates that we have just seen on the
input a string derivable from XY and that we hope next to
see a string derivable from Z.
Item A → XYZ. Indicates that we have seen the body
XYZ and that it may be time to reduce XYZ to A.
Items and the LR(0) Automaton
LR(0) automaton:
The Collection of sets of LR(0) items, called the
canonical LR(0) collection, provides the basis for
constructing a deterministic finite automaton that is used
to make parsing decisions.
To construct the canonical LR(0) collection for a
grammar:
we have to define an Augmented Grammar and two
function CLOSURE and GOTO.
Items and the LR(0) Automaton
Augmented Grammar:
If G is a grammar with start symbol S, then G', the
augmented grammar for G, is G with a new start symbol
S` and production S` → S.
The purpose of this new starting production is to indicate
to the parser when it should stop parsing and announce
acceptance of the input.
That is, acceptance occurs when and only when the parser
is about to reduce by S` → S.
Items and the LR(0) Automaton
1. Consider the grammar
E→E+T|T
T→T*F|F
F → ( E ) | id
Then an augmented grammar is
E` → E
E →E+T|T
T →T*F|F
F → ( E )| id
2. Consider the grammar
A → (A)
A→ a
Then an augmented grammar is
A` → A
A → (A)
A→ a
Computation of the canonical collection
of sets of LR(0) items
void items(G')
{
C = CLOSURE({[S` → •S]});
repeat
for ( each set of items I in C )
for ( each grammar symbol X )
if ( GOTO (I, X) is not empty and not in C )
add GOTO(I, X) to C;
until no new sets of items are added to C on a round;
}
.
Closure of Item Sets
If I is a set of items for a grammar G, then CLOSURE(I)
is the set of items constructed from I by the two rules:

1. Initially, add every item in I to CLOSURE(I).


2. If A→α.Bβ is in CLOSURE(I) and B → γ is a production,
then add the item B →.γ to CLOSURE(I), if it is not
already there.
Apply this rule until no more new items can be added to
CLOSURE(I).
Computation of CLOSURE
SetOfItems CLOSURE (I) {
J = I;
repeat
for ( each item A → α.Bβ in J )
for ( each production B → γ of G )
if ( B →. γ is not in J )
add B →. γ to J;
until no more items are added to J on one round;
return J;
}
The Function GOTO
The second useful function is GOTO (I, X) where I is a
set of items and X is a grammar symbol.
 GOTO ( I , X) is defined to be the closure of the set of all
items [A →αX.β] such that [A →α.Xβ] is in I.
The GOTO function is used to define the transitions in the
LR(0) automaton for a grammar.
 The states of the automaton correspond to sets of items,
and GOTO(I,X) specifies the transition from the state for I
under input X.
Constructing SLR-Parsing Tables
INPUT: An augmented grammar G'.
OUTPUT: The SLR-parsing table functions ACTION and GOTO
for G'.
METHOD:
1. Construct C = {I0, I1,... ,In}, the collection of sets of LR(0) items
for G'.
2. State i is constructed from Ii The parsing actions for state i are
determined as follows:
(a) If [A → α.aβ] is in Ii and GOTO(Ii , a) = Ij, then set
ACTION[i, a] to "shift j . " Here a must be a terminal.
(b) If [A → α.] is in Ii , then set ACTION[i, a] to "reduce
A → α " for all a in FOLLOW(A); here A may not be S'.
(c) If [S' → S.] is in Ii , then set ACTION[ i , $] to "accept."
If any conflicting actions result from the above rules, we say
the grammar is not SLR(l). The algorithm fails to produce
a parser in this case.
3. The GOTO transitions for state i are constructed for all
nonterminals A using the rule: If GOTO(Ii ,A) = Ij , then
GOTO[ i , A] = j.
4. All entries not defined by rules (2) and (3) are made
"error."
5. The initial state of the parser is the one constructed from
the set of items containing [S' → .S]
Example -1.
Construct the SLR set of items for the grammar
E→E+T|T
T→T*F|F
F → ( E ) | id
Solution:
An augmented grammar is
E` → E
E →E+T|T
T →T*F|F
F → ( E )| id
LR(0) Items
I0: I 2 :GOTO(I0 ,T) I5 :GOTO(I0 , id)
E`→ .E E→ T. F→ id.
E → .E + T T → T. * F I6 :GOTO(I1 , +)
E → .T I3 :GOTO(I0 , F) E → E+. T
T → .T * F T→ F. T → .T * F
T→ .F I4 :GOTO(I0 , ( ) T → .F
F → .( E ) F →(. E ) F →.( E )
F → .id E → .E + T F →.id
I1 : GOTO(I0 , E) E → .T I7 :GOTO(I2 , *)
E`→ E. T → .T * F T → T *. F
E → E.+ T T → .F F → .( E )
F → .( E ) F → .id
F → .id
I8 :GOTO(I4 , E)
F → ( E. )
E → E.+ T
I9 :GOTO(I6 , T)
E → E+ T.
T → T .* F
I10 :GOTO(I7 , F)
T → T * F.
I11 :GOTO(I8 , ) )
F →( E ).
The corresponding LR(0) automaton is
Example 2.
Construct the SLR set of items for the grammar and GOTO
function
S → L= R | R
L → * R | id
R→L
LR(0) Automaton
The central idea behind "Simple LR," or SLR, parsing is
the construction from the grammar of the LR(0)
automaton.
The states of this automaton are the sets of items from the
canonical LR(0) collection, and the transitions are given
by the GOTO function.
The start state of the LR(0) automaton is
CLOSURE({[ S` → S]}), where S` is the start symbol of
the augmented grammar.
The parse of id * id
LI NE STACK SYMBOL INPUT ACTION

(1) 0 $ id * id $ Shift to 5
(2) 05 $ id * id $ Reduce by F→ id
(3) 03 $F * id $ Reduce by T → F
(4) 02 $T * id $ Shift to 7
(5) 027 $T* id $ Shift to 5
(6) 0275 $ T * id $ Reduce by F→ id
(7) 0 2 7 10 $T*F $ Reduce by T→ T * F
(8) 02 $T $ Reduce by E → T
(9) 01 $E $ accept
The LR-Parsing Algorithm
LR parser consists of an input, an output, a stack, a driver
program, and a parsing table that has two parts (ACTION
and GOTO).
The LR-Parsing Algorithm
The driver program is the same for all LR parsers; only
the parsing table changes from one parser to another.
The parsing program reads characters from an input
buffer one at a time. Where a shift-reduce parser would
shift a symbol, an LR parser shifts a state. Each state
summarizes the Information contained in the stack below
it.
The stack holds a sequence of states, s0s1 • • • sm , where
sm is on top.
 In the SLR method, the stack holds states from the LR(0)
automaton; the canonical- LR and LALR methods are
similar.
By construction, each state has a corresponding grammar
symbol.
The states correspond to sets of items, and that there is a
transition from state i to state j if GOTO(Ii, X) = Ij.
All transitions to state j must be for the same grammar
symbol X. Thus, each state, except the start state 0, has a
unique grammar symbol associated with it .
Structure of the LR Parsing Table
The parsing table consists of two parts: a parsing-action
function ACTION and a goto function GOTO.
1. The ACTION function takes as arguments a state i and a
terminal a or $. The value of ACTION [ i, a] can have one
of four forms:
(a) Shift j, where j is a state. The action taken by the
parser effectively shifts input a to the stack, but uses state j
to represent a.
(b) Reduce A → β. The action of the parser effectively
reduces β on the top of the stack to head A.
(c) Accept. The parser accepts the input and finishes parsing.
(d) Error. The parser discovers an error in its input and takes
some corrective action.

2. We extend the GOTO function, defined on sets of items,


to states:
If GOTO(Ii, X) = I j , then GOTO also maps a state i and a
nonterminal A to state j .
LR-Parser Configurations
A configuration of a LR parser is a pair:
(s0s1 • • Sm, aiai+1 • • an$)
where the first component is the stack contents and the
second component is the remaining input. This
configuration represents the right-sentential form
X1X2 • • • Xm aiai+1 • • an
in essentially the same way as a shift-reduce parser
would; the only difference is that instead of grammar
symbols, the stack holds states from which grammar
symbols can be recovered. That is, Xi is the grammar
symbol represented by state Si.
Behavior of the LR Parser
The next move of the parser from the configuration is
determined by reading a, the current input symbol, and
Sm , the state on top of the stack, and then consulting the
entry ACTION[sm , ai] in the parsing action table. The
configurations resulting after each of the four types of
move are as follows
1. If ACTION[sm , ai] = shift s, the parser executes a shift
move; it shifts the next state s onto the stack, entering the
configuration
(s0s1 • • sm s, ai+1 • • an$)
The current input symbol is now ai+1.
2. If ACTION [sm , ai] = reduce A → β, then the parser
executes a reduce move, entering the configuration
(s0s1 • • sm – r s, aiai+1 • • an$)
where r is the length of β, and s = GOTO[sm - r , A]. Here the
parser first popped r state symbols off the stack, exposing
state sm – r. The parser then pushed s, the entry for
GOTO[sm – r ,A], onto the stack. The current input symbol
is not changed in a reduce move.
3. If ACTION [sm , ai] = accept, parsing is completed.
4. If ACTION [sm , ai]= error, the parser has discovered an
error and calls an error recovery routine.
LR-parsing algorithm.
INPUT: An input string w and an LR-parsing table with
functions ACTION and GOTO for a grammar G.
OUTPUT: If w is in L(G), the reduction steps of a bottom-
up parse for w; otherwise, an error indication.
METHOD: Initially, the parser has s0 on its stack, where s0
is the initial state, and w$ in the input buffer. The parser
then executes the program.
let a be the first symbol of w$;
while(1)
{ /* repeat forever */
let s be the state on top of the stack;
if ( ACTION[s, a] = shift t ) {
push t onto the stack;
let a be the next input symbol;
} else if ( ACTION [s, a] = reduce A → β {
pop β symbols off the stack;
let state t now be on top of the stack;
push GOTO[t, A] onto the stack;
output the production A → β;
} else if ( ACTION[s, a] = accept ) break;
else call error-recovery routine;
}
Parsing table for Expression grammar
STATE ACTION GOTO
id + * ( ) $ E T F
0 S5 s4 1 2 3
1 s6 acc
r2 s7 r2 r2
2 r4 r4 r4 r4
3 S5 s4 8 2 3
4 r6 r6 r6 r6
5 S5 s4 9 3
6 S5 s4 10
7 s6 s11
8 r1 s7 r1 r1
9 r3 r3 r3 r3
10 r5 r5 r5 r5
11
Moves of an LR parser on id * id + id
STACK SYMBOLS INPUT ACTION
(1) 0 id * id + id $ Shift
(2) 05 id * id + id $ Reduce by F → id
(3) 03 F * id + id $ Reduce by T → F
(4) 02 T * id + id $ Shift
(5) 027 T* * id + id $ Shift
(6) 0275 T * id id + id $ Reduce by F → id
(7) 0 2 7 10 T*F + id $ Reduce by T → T * F
(8) 02 T + id $ Reduce by E → T
(9) 01 E + id $ Shift
(10) 016 E+ id $ Shift
(11) 0165 E + id $ Reduce by F → id
(12) 0163 E+F $ Reduce by T → F
(13) 0169 E+T $ Reduce by E → E + T
(14) 01 E $ Accept
Example 2.
Construct the SLR Parsing table for the
Grammar.
S → L= R | R
L → * R | id
R→L
Canonical LR(1) Items
In the SLR method, state i calls for reduction by A → α if
the set of items Ii contains item [A → α.] and a is in
FOLLOW(A).
In some situations, however, when state i appears on top of
the stack, the viable prefix βα on the stack is such that βA
cannot be followed by a in any right-sentential form.
Thus, the reduction by A → α should be invalid on input
a.
Viable prefixes
The stack contents must be a prefix of a right-sentential form.
If the stack holds α and the rest of the input is x, then a sequence of
reductions will take αx to S.
 S →*rm αx
Not all prefixes of right-sentential forms can appear on the stack,
however, since the parser must not shift past the handle.
For example, suppose
E →*rm F * id →*rm (E) * id
at various times during the parse, the stack will hold
(, (E, and (E), but it must not hold (E)*, since (E) is a handle,
which the parser must reduce to F before shifting *.
Definition of viable Prefixes
The prefixes of right sentential forms that can appear on the top
of the stack of a shift reduce parser are called viable prefixes
A viable prefix is a prefix of a right-sentential form that does not
continue past the right end of the rightmost handle of that
sentential form.
SLR parsing is based on the fact that LR(0) automata recognize
viable prefixes.
Consider the item A →β1. β2
Let S → αAβ → α β1β2 β
Since dot is in between the β1and β2 , so αβ1 will be the top of
the stack, so that αβ1 valid for a viable prefix.
We say that A →β1. β2 valid item for the viable prefix αβ1
Example
Let A →β1. β2 valid item for the viable prefix αβ1
Let w = α β1β2β
Let current state {A →β1. β2 , A →β1. }
Consider the configuration
stack input
$ α β1 β2x $
Case1: β2 not equal to Є,
Shift, since dot is in the middle, we must shift.
Case2: β2 equal to Є
Reduce A →β1
Construct the SLR Parsing table for the
Grammar.
S → L= R | R
L → * R | id
R→L
S` → S I 1:GOTO(I0 , S) I5:GOTO(I0 , id)
S → L= R | R S` →S. L → id.
L → * R | id I2 :GOTO(I0 , L) I6:GOTO(I1 , =)
R→L S →L.= R S → L= .R
I0 : R → L. R → .L
S` → .S I3 :GOTO(I0 , R) L→.*R
S → .L= R S → R. L → .id
S → .R I4:GOTO(I0 , *) I7:GOTO(I4 ,R)
L→.*R L → *. R L → * R.
L → .id R → .L I8:GOTO(I6 ,L)
R → .L L→.*R R → L.
L → .id I9:GOTO(I4 ,R)
S → L= R.
Parsing Table
STATE ACTION GOTO
= * id $ S L R
0 s4 s5 1 2 3
1 acc
2 s6/ r5 r5
3 r2
4 s4 s5 8 7
5 r4 r4
6 s4 s5 8 9
7 r3 r3
8 r5 r5
9 r1
Where in state 2we had item R → L. and = sign,
which is in FOLLOW(R)
SLR parser calls for reduction by R → L the states 2 with =
as next input ( the shift action is also called for because of
item S → L. = R in state 2 )
Here there is no right sentential form of the grammar that
begins R = …..
Thus state 2 which is the state corresponding to viable prefix
L only, should not really call for reduction of that L to R
We can carry more information in the state that will allow us
to rule out some of the these invalid reductions by A → α
Canonical LR(1) Items
The extra information is incorporated into the state by
redefining items to include a terminal symbol as a second
component.
The general form of an item becomes [A → α.β,a], where
A →αβ is a production and a is a terminal or the right
end marker $. We call such an object an LR(1) item.
The 1 refers to the length of the second component, called
the lookahead of the item.
The lookahead has no effect in an item of the form
[A → α.β, a], where β is not Є,
but an item of the form [A → α.,a] calls for a reduction by
A → α only if the next input symbol is a.
Set of LR(1) items construction for
grammar G`
void items(G')
{
initialize C to CLOSURE({[S` → .S,$]});
repeat
for ( each set of items I in C )
for ( each grammar symbol X )
if ( GOTO(I,X) is not empty and not in C )
add GOTO(I, X) to C;
until no new sets of items are added to C;
}
Set of LR(1) items construction for
grammar G`
SetOfltems CLOSURE(I)
{
repeat
for ( each item [A → α.Bβ, a ] in I )
for ( each production B → γ in G' )
for ( each terminal b in FIRST(βa ) )
add [B → .γ, b] to set I;
until no more items are added to I;
return I;
}
Set of LR(1) items construction for
Grammar G`
SetOfltems GOTO(I, X)
{
initialize J to be the empty set;
for ( each item [A → α.Xβ, a ] in I )
add item [A → αX.β, a] to set J;
return CLOSURE(J);
}
Example -1
Consider the grammar
S → CC
C → cC | d
The augmented grammar is
S` → S
S → CC
C → cC | d
The LR(1) items are:
I0: S` → .S ,$
S → .CC , $
C → .c C , c | d
C → .d ,c | d
I1 : GOTO(I0, S) I4: GOTO(I0, c) I8 : GOTO(I2, d)
S` → S. ,$ C → d. , c |d C → c C. , c | d
I2 : GOTO(I0, C) I5: GOTO(I2, C) I9 : GOTO(I6, C)
S → C.C , $ S → C C. , $ C → cC. , $
C → .c C , $ I6 : GOTO(I2, c)
C → .d , $ C → c. C , $
I 3: GOTO(I0, c) C → .c C , $
C → c. C , c |d C → .d , $
C → .c C , c |d I7 : GOTO(I2, d)
C → .d , c |d C → d. , $
Construction of canonical-LR parsing
tables.
INPUT: An augmented grammar G'.
OUTPUT: The canonical-LR parsing table functions ACTION
and GOTO for G'.
METHOD:
1. Construct C = {I0, I1,... ,In}, the collection of sets of LR(1)
items for G'.
2. State i is constructed from Ii The parsing actions for state i are
determined as follows:
(a) If [A → α.aβ , b] is in Ii and GOTO(Ii , a) = Ij, then set
ACTION[i, a] to "shift j . " Here a must be a terminal.
(b) If [A → α., a] is in Ii , A ≠ S`then set ACTION[i, a] to "reduce
A → α.”
Construction of canonical-LR parsing
tables.
(c) If [S' → S.,$] is in I , then set ACTION[ i , $] to
i
"accept."
If any conflicting actions result from the above rules, we say
the grammar is not LR(l). The algorithm fails to produce a
parser in this case.
3. The goto transitions for state i are constructed for all
nonterminals A using the rule: If GOTO(Ii ,A) = Ij , then
GOTO[ i , A] = j.
4. All entries not defined by rules (2) and (3) are made
"error."
5. The initial state of the parser is the one constructed from
the set of items containing [S' → .S , $]
LR(1) Parsing Table
STATE ACTION GOTO
c d $ S C
0 s3 s4 1 2
1 acc
2 s7 5
3 s3 s4 8
4 r3 r3
5 r1
6 s6 s7 9
7 r3
8 r2 r2
9 r2
LALR Parsing table
INPUT: An augmented grammar G'.
OUTPUT: The LALR parsing-table functions ACTION
and GOTO for G'.
METHOD:
1. Construct C = {I1, I2,. . . , In} , the collection of sets of
LR(1) items.
2. For each core present among the set of LR(1) items, find
all sets having that core, and replace these sets by their
union.
3. Let C = {J1, J2,. . . , Jm} be the resulting sets of LR(1)
items. The parsing actions for state i are constructed from
Ji.
If there is a parsing action conflict, the algorithm fails
to produce a parser, and the grammar is said not to be
LALR(l).
4.The GOTO table is constructed as follows. If J is the
union of one or more sets of LR(1) items, that is, J =
I1,∩ I2 ∩ • • • ∩ Ik, then the cores of GOTO(I1 , X),
GOTO(I2 , X),... , GOTO(Ik , X) are the same, since
I1, I2,. . . , Ik all have the same core. Let K be the
union of all sets of items having the same core as
GOTO(I1 ,X). Then GOTO(J,X) = K.
There are three pairs of sets of items that can be merged.
I3 and I6 are replaced by their union:
I36 : C → c. C , c| d| $
C → .c C , c| d| $
C → .d , c| d| $
I4 and I7 are replaced by their union:
I47 : C → d., c| d| $
I8 and I9 are replaced by their union:
I89 : C → c C., c| d| $
LALR Parsing Table

STATE ACTION GOTO


c d $ S C
0 s36 s47 1 2
1 acc
2 s36 s47 5
36 s36 s47 89
47 r3 r3 r3
5 r1
89 r2 r2 r2

You might also like