Skip to main content
SYNTAX ANALYSIS
OR
PARSING
Lecture 09
1
ASSIGNMENT 01 (DUE ON 20TH)
1. Derive the (i) LL(1) parse table, (ii) LR(0)
automation, (iii) LR(0) parse table of following
grammar:
S-> SS+
S-> SS*
S-> a
2. Parse the following string using (i) LL (1) parse
table, (ii) LR (0) parse table:
“aa+a*a+”
2
LR(1) ITEM
 To avoid some of invalid reductions, the states need to carry more
information.
 Extra information is put into a state by including a terminal symbol
as a second component in an item.
 A LR(1) item is:
A  .,a where a is the look-head of the LR(1) item
(a is a terminal or end-marker.)
3
LR (1) ITEMS
4
INTUITION BEHIND LR (1) ITEMS
5
INTUITION BEHIND LR (1) ITEMS
6
THE CLOSURE FUNCTION
7
THE CLOSURE FUNCTION
8
LR (1) EXAMPLE
 S -> CC
 C -> cC
 C -> d
9
10
THE GOTO FUNCTION
11
CONSTRUCTION OF LR(1) PARSING TABLES
1. Construct the canonical collection of sets of LR(1) items for G’.
C{I0,...,In}
2. Create the parsing action table as follows
• If a is a terminal, A.a,b in Ii and goto(Ii,a)=Ij then action[i,a] is
shift j.
• If A.,a is in Ii , then action[i,a] is reduce A where AS’.
• If S’S.,$ is in Ii , then action[i,$] is accept.
• If any conflicting actions generated by these rules, the grammar is not
LR(1).
3. Create the parsing goto table
• for all non-terminals A, if goto(Ii,A)=Ij then goto[i,A]=j
4. All entries not defined by (2) and (3) are errors.
5. Initial state of the parser contains S’.S,$
12
LALR Parsing Tables
• LALR stands for LookAhead LR.
• LALR parsers are often used in practice because
LALR parsing tables are smaller than LR(1) parsing
tables.
• The number of states in SLR and LALR parsing tables
for a grammar G are equal.
• But LALR parsers recognize more grammars than
SLR parsers.
• A state of LALR parser will be again a set of LR(1)
items.
CREATING LALR PARSING TABLES
Canonical LR(1) Parser € LALR Parser
shrink # of states
• This shrink process may introduce a reduce/reduce
conflict in the resulting LALR parser (so the grammar
is NOT LALR)
• But, this shrink process does not produce a
shift/reduce conflict.
The Core of A Set of LR(1) Items
• The core of a set of LR(1) items is the set of its
first component.
Ex: S  L =R,$. S  L.=R
R  L.,$ R  L.
• We will find the states (sets of LR(1) items) in a canonical LR(1)
parser with same cores. Then we will merge them as a single
state.
L  id.,$
I1:L  id.,= A new state:I12: L  id.,=
I2:L  id.,$
• We will do this for all states of a canonical LR(1) parser to get
the states of the LALR parser.
• In fact, the number of the states of the LALR parser for a
grammar will be equal to the number of states of the SLR
Core
Same Core
Merge Them
CREATION OF LALR PARSING TABLES
• Create the canonical LR(1) collection of the sets of LR(1) items
for the given grammar.
• Find each core; find all sets having that same core; replace
those sets having same cores with a single set which is their
union.
C={I0,...,In} € C’={J1,...,Jm} where m  n
• Create the parsing tables (action and goto tables) same as the
construction of the parsing tables of LR(1) parser.
– Note that: If J=I1  ...  Iksince I1,...,Ik have same cores
€ cores of goto(I1,X),...,goto(I2,X) must be same.
– So, goto(J,X)=K where K is the union of all sets of items
having same cores as goto(I1,X).
• If no conflict is introduced, the grammar is LALR(1) grammar.
(We may only introduce reduce/reduce conflicts; we cannot
introduce a shift/reduce conflict)
SHIFT/REDUCE CONFLICT
• We say that we cannot introduce a shift/reduce conflict during
the shrink process for the creation of the states of a LALR
parser.
• Assume that we can introduce a shift/reduce conflict. In this
case, a state of LALR parser must have:
A  .,a and B  .a,b
• This means that a state of the canonical LR(1) parser must
have:
A  .,a and B  .a,c
But, this state has also a shift/reduce conflict. i.e. The original
canonical LR(1) parser has a conflict.
(Reason for this, the shift operation does not depend on
lookaheads)
Reduce/Reduce Conflict
• But, we may introduce a reduce/reduce conflict during the
shrink process for the creation of the states of a LALR parser.
I1 : A  .,a
B  .,b

I2: A  .,b
B  .,c
€ reduce/reduce conflictI12: A  .,a/b
B  .,b/c
Any Questions ?
19