1

Bottom-Up Parsing
· A bottom-up parser creates the parse tree oI the given input starting
Irom leaves towards the root.
· A bottom-up parser tries to Iind the right-most derivation oI the given
input in the reverse order.
S ... (the right-most derivation oI )
(the bottom-up parser Iinds the right-most derivation in the reverse order)
· Bottom-up parsing is also known as shift-reduce parsing because its
two main actions are shiIt and reduce.
At each shiIt action, the current symbol in the input string is pushed to a stack.
At each reduction step, the symbols at the top oI the stack (this symbol sequence is the right
side oI a production) will replaced by the non-terminal at the leIt side oI that production.
There are also two more actions: accept and error.
2
$hift-Reduce Parsing
· A shiIt-reduce parser tries to reduce the given input string into the starting symbol.
a string the starting symbol
reduced to
· At each reduction step, a substring oI the input matching to the right side oI a
production rule is replaced by the non-terminal at the leIt side oI that production rule.
· II the substring is chosen correctly, the right most derivation oI that string is created in
the reverse order.
Rightmost Derivation: S
ShiIt-Reduce Parser Iinds: ... S
*
rm
rm rm
3
$hift-Reduce Parsing -- Example
S F aABb input string: ,,,--
A F aA ' a ,,--
B F bB ' b ,-- reduction
,-
$
S aABb aAbb aaAbb aaabb
Right Sentential Forms
· How do we know which substring to be replaced at each reduction step?
rm rm rm rm
4
andle
· InIormally, a handle oI a string is a substring that matches the right side
oI a production rule.
But not every substring matches the right side oI a production rule is handle
· A handle oI a right sentential Iorm ( -.) is
a production rule A F . and a position oI
where the string . may be Iound and replaced by A to produce
the previous right-sentential Iorm in a rightmost derivation oI .
S -A -.
· II the grammar is unambiguous, then every right-sentential Iorm oI the
grammar has exactly one handle.
· We will see that is a string oI terminals.
rm rm
*
5
andle Pruning
· A right-most derivation in reverse can be obtained by handle-pruning.

0

1

2
...
n-1

n
÷
input string
· Start Irom
n
, Iind a handle A
n
F.
n
in
n
,
and replace .
n
in by A
n
to get
n-1
.
· Then Iind a handle A
n-1
F.
n-1
in
n-1
,
and replace .
n-1
in by A
n-1
to get
n-2
.
· Repeat this, until we reach S.
rm rm rm rm rm
6
$hift-Reduce Parser
E F E¹T ' T Right-Most Derivation oI ///
T F T*F ' F E E¹T E¹T*F E¹T*id E¹F*id
F F (E) ' id E¹id*id T¹id*id F¹id*id id¹id*id
Right-Most Sentential Form Reducing Production
/// F F id
// T F F
%// E F T
// F F id
/ T F F
%/ F F id
% T F T*F
% E F E¹T

,3/08 are red and underlined in the right-sentential Iorms.

$tack Implementation of $hift-Reduce Parser
· There are Iour possible actions oI a shiIt-parser action:
1. $hift : The next input symbol is shiIted onto the top oI the stack.
2. Reduce: Replace the handle on the top oI the stack by the non-
terminal.
3. ccept: SuccessIul completion oI parsing.
4. Error: Parser discovers a syntax error, and calls an error recovery
routine.
· Initial stack iust contains only the end-marker $.
· The end oI the input string is marked by the end-marker $.

$tack Implementation of $hift-Reduce Parser
$tack Input ction
$ id¹id*id$ shiIt
$id ¹id*id$ reduce by F F id Parse Tree
$F ¹id*id$ reduce by T F F
$T ¹id*id$ reduce by E F T E
$E ¹id*id$ shiIt
$E¹ id*id$ shiIt E 3 ¹ T
$E¹id *id$ reduce by F F id
$E¹F *id$ reduce by T F F T 2 T 5 * F 6
$E¹T *id$ shiIt
$E¹T* id$ shiIt F 1 F 4 id
$E¹T*id $ reduce by F F id
$E¹T*F $ reduce by T F T*F id id
$E¹T $ reduce by E F E¹T
$E $ accept

onflicts During $hift-Reduce Parsing
· There are context-Iree grammars Ior which shiIt-reduce parsers cannot
be used.
· Stack contents and the next input symbol may not decide action:
shift/reduce conflict: Whether make a shiIt operation or a reduction.
reduce/reduce conflict: The parser cannot decide which oI several
reductions to make.
· II a shiIt-reduce parser cannot be used Ior a grammar, that grammar is
called as non-LR(k) grammar.
leIt to right right-most k lookhead
scanning derivation
· An ambiguous grammar can never be a LR grammar.
10
$hift-Reduce Parsers
· There are two main categories oI shiIt-reduce parsers
1. Operator-Precedence Parser
simple, but only a small class oI grammars.
2. LR-Parsers
covers wide range oI grammars.
· SLR simple LR parser
· LR most general LR parser
· LALR intermediate LR parser (lookhead LR parser)
SLR, LR and LALR work same, only their parsing tables are diIIerent.
SLR
CFG
LR
LALR
Operator-Precedence Parser
· Operator grammar
small, but an important class oI grammars
we may have an eIIicient operator precedence parser
(a shiIt-reduce parser) Ior an operator grammar.
· In an operator grammar, no production rule can have:
1 at the right side
two adiacent non-terminals at the right side.
· Ex:
EFAB EFEOE EFE¹E '
AFa EFid E*E '
BFb OF¹'*'/ E/E ' id
not operator grammar not operator grammar operator grammar
Precedence Relations
· In operator-precedence parsing, we deIine three disioint precedence
relations between certain pairs oI terminals.
a ·
.
b b has higher precedence than a
a ÷· b b has same precedence as a
a
.
~ b b has lower precedence than a
· The determination oI correct precedence relations between terminals
are based on the traditional notions oI associativity and precedence oI
operators. (Unary minus causes a problem).
Using Operator-Precedence Relations
· The intention oI the precedence relations is to Iind the handle oI a right-
sentential Iorm,
·
.
with marking the leIt end,
÷· appearing in the interior oI the handle, and
.
~ marking the right hand.
· In our input string $a
1
a
2
...a
n
$, we insert the precedence relation
between the pairs oI terminals (the precedence relation holds between
the terminals in that pair).
Using Operator -Precedence Relations
E F E¹E ' E-E ' E*E ' E/E ' E`E ' (E) ' -E ' id
The partial operator-precedence
table Ior this grammar
· Then the input string id¹id*id with the precedence relations
inserted will be:
$ ·
.
id
.
~ ¹ ·
.
id
.
~ * ·
.
id
.
~ $
id id ¹¹ * * $ $
id id
..
~~
..
~~
..
~~
¹¹ ··
.. ..
~~ ··
.. ..
~~
* * ··
.. ..
~~
..
~~
..
~~
$ $ ··
..
··
..
··
..
To Find The andles
1. Scan the string Irom leIt end until the Iirst
.
~ is encountered.
2. Then scan backwards (to the leIt) over any ÷· until a ·
.
is encountered.
3. The handle contains everything to leIt oI the Iirst
.
~ and to the right oI
the ·
.
is encountered.
$ ·
.
id
.
~ ¹ ·
.
id
.
~ * ·
.
id
.
~ $ E F id $ id ¹ id * id $
$ ·
.
¹ ·
.
id
.
~ * ·
.
id
.
~ $ E F id $ E ¹ id * id $
$ ·
.
¹ ·
.
* ·
.
id
.
~ $ E F id $ E ¹ E * id $
$ ·
.
¹ ·
.
*
.
~ $ E F E*E $ E ¹ E *
.
E $
$ ·
.
¹
.
~ $ E F E¹E $ E ¹ E $
$ $ $ E $
Operator-Precedence Parsing lgorithm
The input string is w$. the initial stack is $ and a table holds precedence relations
between certain terminals
Algorithm:
set p to point to the Iirst symbol oI w$ ;
repeat forever
if ( $ is on top oI the stack and p points to $ ) then return
else ¦
let a be the topmost terminal symbol on the stack and let b be the symbol pointed to
by p;
if ( a ·
.
b or a ÷· b ) then ¦ /* SHIFT */
push b onto the stack;
advance p to the next input symbol;
}
else if ( a
.
~ b ) then /* REDUCE */
repeat pop stack
until ( the top oI stack terminal is related by ·
.
to the terminal most
recently popped );
else error();
}
Operator-Precedence Parsing lgorithm -- Example
8tack input action
$ id¹id*id$ $ ·
.
id shiIt
$id ¹id*id$ id
.
~ ¹ reduce E F id
$ ¹id*id$ shiIt
$¹ id*id$ shiIt
$¹id *id$ id
.
~ * reduce E F id
$¹ *id$ shiIt
$¹* id$ shiIt
$¹*id $ id
.
~ $ reduce E F id
$¹* $ *
.
~ $ reduce E F E*E
$¹ $ ¹
.
~ $ reduce E F E¹E
$ $ accept
id id ¹¹ ** $$
id id
..
~~
..
~~
..
~~
¹¹ ··
.. ..
~~ ··
.. ..
~~
** ··
.. ..
~~
..
~~
..
~~
$$ ··
..
··
..
··
..
ow to reate Operator-Precedence Relations
· We use associativity and precedence relations among operators.
1. II operator 7
1
has higher precedence than operator 7
2
,
7
1
.
~ 7
2
and 7
2
·
.
7
1
2. II operator 7
1
and operator 7
2
have equal precedence,
they are leIt-associative 7
1
.
~ 7
2
and 7
2
.
~ 7
1
they are right-associative 7
1
·
.
7
2
and 7
2
·
.
7
1
3. For all operators 7, 7 ·
.
id, id
.
~ 7, 7 ·
.
(, (·
.
7, 7
.
~ ), )
.
~ 7, 7
.
~ $, and $ ·
.
7
4. Also, let
(÷·) $ ·
.
( id
.
~ ) )
.
~ $
( ·
.
( $ ·
.
id id
.
~ $ )
.
~ )
( ·
.
id
Operator-Precedence Relations
¹¹ - - ** // ` ` id id ( ( ) ) $$
¹¹
..
~~
..
~~ ··
..
··
..
··
..
··
..
··
.. ..
~~
..
~~
- -
..
~~
..
~~ ··
..
··
..
··
..
··
..
··
.. ..
~~
..
~~
**
..
~~
..
~~
..
~~
..
~~ ··
..
··
..
··
.. ..
~~
..
~~
//
..
~~
..
~~
..
~~
..
~~ ··
..
··
..
··
.. ..
~~
..
~~
` `
..
~~
..
~~
..
~~
..
~~ ··
..
··
..
··
.. ..
~~
..
~~
id id
..
~~
..
~~
..
~~
..
~~
..
~~
..
~~
..
~~
( ( ··
..
··
..
··
..
··
..
··
..
··
..
··
..
÷÷··
) )
..
~~
..
~~
..
~~
..
~~
..
~~
..
~~
..
~~
$$ ··
..
··
..
··
..
··
..
··
..
··
..
··
..
ow to reate Operator-Precedence Relations
$tep1: Check whether Grammar is Operator Grammar or not
$tep2 : Calculate the leading and Trailing Ior every non terminal involved in given
grammar.
1. For any production like X F aZ
Leading(X)÷¦a} iI is c or a single non terminal
2. For any production like X F Bu , Leading(X)÷ Leading(B)
3. For any production like X F aZ
Trailing(X)÷¦a} iI Z is c or a single non terminal
4. For any production like X F uB, Trailing(X)÷Trailing(B)
5. For any production like A F B
Leading(A)÷ Leading(B), Trailing(A)÷Trailing(B)
6. For any production like A F a, Leading(A)÷ ¦a} Trailing(A)÷¦a}
E F E¹T L(E)÷ ¹(rule-1) T(E)÷ ¹ ( rule-3)
'E F T L(E)÷L(T), T(E)÷T(T) (rule-5)
T F T*F L(T)÷ *(rule-1) T(T)÷ * ( rule-3)
'T F F L(T)÷L(F), T(T)÷T(F) (rule-5)
F F (E) L(F)÷ ( (rule-1) T(F)÷ ) ( rule-3)
F F id L(F)÷T(F)÷id (Rule-6)
on-
Terminal
E T F
Leading(L) ¹,*, ( , id *, ( , id ( , id
Trailing(T) ¹,*, ) , id *, ) , id ) , id
Operator-Precedence Relations
$tep3: ow establish the precedence relation between to terminals as Iollows
1. a ÷· b : iI in RHS oI any production X F ua×bo , where × is nothing or single non
terminal
2. a <
.
b: iI b is in Lead(B) where B is immediate right non terminal oI a in any
production , AF u aB × a(row) ·
.
¦all lead oI B}(column)
3. a
.
> b: iI a is in Last(B) where B is immediate leIt non terminal oI a in any
production , AF u Bb × ¦all Trailing oI B}
.
~ b
4. $ ·
.
¦all lead oI S}
5. ¦all Trailing oI S }
.
~ $
Operator-Precedence Relations
++ `` ( ( ) ) id id $$
++
..
~~ ··
..
··
.. ..
~~ ··
.. ..
~~
``
..
~~
..
~~ ··
.. ..
~~ ··
.. ..
~~
( ( ··
..
··
..
··
..
÷÷· ·
··
..
) )
..
~~
..
~~
..
~~
..
~~
id id
..
~~
..
~~
..
~~
..
~~
$$ ··
..
··
..
··
..
··
..
on-Terminal E T F
Leading(L) ¹,*, ( , id *, ( , id ( , id
Trailing(T) ¹,*, ) , id *, ) , id ) , id
E F E¹T rule-2
'E FT rule-3
T F T*F rule-2
'T F F rule-3
F F (E) rule-1 &2
F F id rule-3
Rule-4
Rule-5
andling Unary Minus
· Operator-Precedence parsing cannot handle the unary minus when we
also have the binary minus in our grammar.
· The best approach to solve this problem, let the lexical analyzer handle
this problem.
The lexical analyzer will return two diIIerent tokens Ior
the unary minus and the binary minus.
The lexical analyzer will need a lookhead to distinguish
the binary minus Irom the unary minus.
· Then, we make
7 ·
.
unary-minus Ior any operator
unary-minus
.
~ 7 iI unary-minus has higher precedence than 7
unary-minus ·
.
7 iI unary-minus has lower (or equal) precedence than 7
Precedence Functions
· Compilers using operator precedence parsers do not need to store the
table oI precedence relations.
· The table can be encoded by two precedence Iunctions I and g that map
terminal symbols to integers.
· For symbols a and b.
I(a) · g(b) whenever a ·
.
b
I(a) ÷ g(b) whenever a ÷· b
I(a) ~ g(b) whenever a
.
~ b
lgorithm 4.6 onstructing precedence functions
onstructing precedence functions
Method:
1. Create symbols f
a
and g
b
Ior each a that is a terminal or $.
2. Partition the created symbols into as many groups as possible, in such a way
that iI a ÷. b, then f
a
and g
b
are in the same group.
3. Create a directed graph whose nodes are the groups Iound in (2). For any a
and b, iI a ·.b , place an edge Irom the group oI g
b
to the group oI f
a
. OI a
.~ b, place an edge Irom the group oI f
a
to that oI g
b.
4. II the graph constructed in (3) has a cycle, then no precedence Iunctions
exist. II there are no cycle, let I(a) be the length oI the longest path
beginning at the group oI f
a
; let g(a) be the length oI the longest path
beginning at the group oI g
a.
Example
I
*
I
$
I
id
I
¹
g
id
g
¹
g
*
g
$
+ ` Id $
f 2 2 4 4 4 4 0 0
g 1 1 3 3 5 5 0 0
Disadvantages of Operator Precedence Parsing
· Disadvantages:
It is diIIicult to handle operators (like - unary minus)which have diIIerent
precedence (the lexical analyzer should handle the unary minus).
Small class oI grammars.
DiIIicult to decide which language is recognized by the grammar.
· dvantages:
Simple and Easy to implement
powerIul enough Ior expressions in programming languages
Can be constructed by hand aIter understanding the grammar.
Simple to debug
Error Recovery in Operator-Precedence Parsing
Error ases:
1. o relation holds between the terminal on the top oI stack and the next
input symbol.
2. A handle is Iound (reduction step), but there is no production with this
handle as a right side
Error Recovery:
1. Each empty entry is Iilled with a pointer to an error routine.
2. Decides the popped handle 'looks like¨ which right hand side. And
tries to recover Irom that situation.
andling $hift/Reduce Errors
When consulting the precedence matrix to decide whether to shiIt or
reduce, we may Iind that no relation holds between the top stack and
the Iirst input symbol.
To recover, we must modiIy (insert/change)
1. Stack or
2. Input or
3. Both.
We must be careful that we don`t get into an infinite loop.
Example
id ( ) $
id id e3 e3 e3 e3
..
~~
..
~~
( ( ··
..
··
..
÷. ÷. e4 e4
) ) e3 e3 e3 e3
..
~~
..
~~
$$ ··
..
··
..
e2 e2 e1 e1
e1: Called when : whole expression is missing
insert id onto the input
issue diagnostic: missing operand`
e2: Called when : expression begins with a right parenthesis
delete ) Irom the input
issue diagnostic: unbalanced right parenthesis`
Example
id ( ) $
id id e3 e3 e3 e3
..
~~
..
~~
( ( ··
..
··
..
÷. ÷. e4 e4
) ) e3 e3 e3 e3
..
~~
..
~~
$$ ··
..
··
..
e2 e2 e1 e1
e3: Called when : id or ) is Iollowed by id or (
insert + onto the input
issue diagnostic: missing operator`
e4: Called when : expression ends with a leIt parenthesis
pop ( Irom the stack
issue diagnostic: missing right parenthesis`
32
LR Parsers
· The most powerIul shiIt-reduce parsing (yet eIIicient) is:
LR(k) parsing.
leIt to right right-most k lookhead
scanning derivation (k is omitted it is 1)
· LR parsing is attractive because:
LR parsing is most general non-backtracking shiIt-reduce parsing, yet it is still eIIicient.
The class oI grammars that can be parsed using LR methods is a proper superset oI the class
oI grammars that can be parsed with predictive parsers.
LL(1)-Grammars LR(1)-Grammars
An LR-parser can detect a syntactic error as soon as it is possible to do so a leIt-to-right
scan oI the input.
33
LR Parsers
· LR-Parsers
covers wide range oI grammars.
SLR simple LR parser
LR most general LR parser
LALR intermediate LR parser (look-head LR parser)
SLR, LR and LALR work same (they used the same algorithm),
only their parsing tables are diIIerent.
34
LR Parsing lgorithm
S
m
X
m
S
m-1
X
m-1
.
.
S
1
X
1
S
0
a
1
... a
i
... a
n
$
Action Table
terminals and $
s
t Iour diIIerent
a actions
t
e
s
Goto Table
non-terminal
s
t each item is
a a state number
t
e
s
LR Parsing lgorithm
stack
input
output
35
onfiguration of LR Parsing lgorithm
· A conIiguration oI a LR parsing is:
( S
o
X
1
S
1
... X
m
S
m
, a
i
a
i¹1
... a
n
$ )
Stack Rest oI Input
· S
m
and a
i
decides the parser action by consulting the parsing action
table. (3tal Stack contains iust S
o
)
· A conIiguration oI a LR parsing represents the right sentential Iorm:
X
1
... X
m
a
i
a
i¹1
... a
n
$
36
ctions of LR-Parser
1. shift s -- shiIts the next input symbol and the state s onto the stack
( S
o
X
1
S
1
... X
m
S
m
, a
i
a
i¹1
... a
n
$ ) ( S
o
X
1
S
1
... X
m
S
m
a
i
s, a
i¹1
... a
n
$ )
2. reduce F. (or 73 where n is a production number)
pop 2'.' (÷r) items Irom the stack;
then push and s where s÷goto[s
m-r
.(
( S
o
X
1
S
1
... X
m
S
m
, a
i
a
i¹1
... a
n
$ ) ( S
o
X
1
S
1
... X
m-r
S
m-r
A s, a
i
... a
n
$ )
Output is the reducing production reduce AF.
3. ccept Parsing successIully completed
4. Error -- Parser detected an error (an empty entry in the action table)
3
Reduce ction
· pop 2'.' (÷r) items Irom the stack; let us assume that . ÷
1

2
...
r
· then push and s where s÷goto[s
m-r
.(
( S
o
X
1
S
1
... X
m-r
S
m-r

1
S
m-r
...
r
S
m
, a
i
a
i¹1
... a
n
$ )
( S
o
X
1
S
1
... X
m-r
S
m-r
A s, a
i
... a
n
$ )
· In Iact,
1

2
...
r
is a handle.
X
1
... X
m-r
A a
i
... a
n
$ X
1
... X
m

1
...
r
a
i
a
i¹1
... a
n
$
3
($LR) Parsing Tables for Expression Grammar
state id + ` ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s r2 r2
3 r4 r4 r4 r4
4 s5 s4 2 3
5 r6 r6 r6 r6
6 s5 s4 3
s5 s4 10
s6 s11
r1 s r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
Action Table Goto Table
1) E F E¹T
2) E F T
3) T F T*F
4) T F F
5) F F (E)
6) F F id
3
ctions of ($)LR-Parser -- Example
stack input action output
0 id*id¹id$ shiIt 5
0id5 *id¹id$ reduce by FFid FFid
0F3 *id¹id$ reduce by TFF TFF
0T2 *id¹id$ shiIt
0T2* id¹id$ shiIt 5
0T2*id5 ¹id$ reduce by FFid FFid
0T2*F10 ¹id$ reduce by TFT*F TFT*F
0T2 ¹id$ reduce by EFT EFT
0E1 ¹id$ shiIt 6
0E1¹6 id$ shiIt 5
0E1¹6id5 $ reduce by FFid FFid
0E1¹6F3 $ reduce by TFF TFF
0E1¹6T $ reduce by EFE¹T EFE¹T
0E1 $ accept
40
onstructing $LR Parsing Tables
II Given a Grammar G, then Following steps are carried out to construct the parse Table:
· $tep1:umbring oI all Grammar productions.
· $tep2: Calculate FIRST and FOLLOW Ior on-terminals
· $tep3: Augment the Given Grammar G:
· $tep4: Construction oI LR(0) Collection oI Items:
(a) Closure Operation
(b) The Goto Operation
(c) The set oI Items Construction
· $tep5: Construction oI SLR parser Table
(a) Filling oI ShiIt Entries
(b) Filling oI reduce Entries
(c) Filling oI Goto Entries
(d) Accept Entry
41
onstructing $LR Parsing Tables
II Given a Grammar G, then Following steps are carried out to construct the parse Table:
· $tep1:umbring oI all Grammar productions.
· $tep2: Calculate FIR$T and FOLLOW Ior on-terminals
· $tep3: Augment the Given Grammar G:
· $tep4: Construction oI LR(0) Collection oI Items:
(a) Closure Operation
(b) The Goto Operation
(c) The set oI Items Construction
· $tep5: Construction oI SLR parser Table
(a) Filling oI ShiIt Entries
(b) Filling oI reduce Entries
(c) Filling oI Goto Entries
(d) Accept Entry
1) E F E¹T 2) E F T 3) T F T*F
4) T F F 5) F F (E) 6) F F id
E` F E E F E¹T E F T T F T*F T F F F F (E) F F id
42
onstructing $LR Parsing Tables - LR(0) Item
· An LR(0) item oI a grammar G is a production oI G a dot at the some
position oI the right side.
· Ex: A F aBb Possible LR(0) Items: A F.aBb
(Iour diIIerent possibility) A F a.Bb
A F aB.b
A F aBb.
· Sets oI LR(0) items will be the states oI action and goto table oI the
SLR parser.
· A collection oI sets oI LR(0) items (the canonical LR(0) collection) is
the basis Ior constructing SLR parsers.
· :gme3ted Grammar:
G` is G with a new production rule S`FS where S` is the new starting
symbol.
43
The losure Operation
· II is a set oI LR(0) items Ior a grammar G, then clo8ure( is the
set oI LR(0) items constructed Irom I by the two rules:
1. Initially, every LR(0) item in I is added to closure(I).
2. II A F -.B. is in closure(I) and BF is a production rule oI G;
then BF. will be in the closure(I).
We will apply this rule until no more new LR(0) items can be added
to closure(I).
44
The losure Operation -- Example
E` F E closure(¦E` F.E}) ÷
E F E¹T ¦ E` F.E kernel items
E F T E F.E¹T
T F T*F E F.T
T F F T F.T*F
F F (E) T F.F
F F id F F.(E)
F F.id }
45
Goto Operation
· II I is a set oI LR(0) items and X is a grammar symbol (terminal or non-
terminal), then goto(I,X) is deIined as Iollows:
II A F -.X. in I
then every item in closure(] F -X..<) will be in goto(I,X).
Example:
I ÷¦ E` F.E, E F.E¹T, E F.T,
T F.T*F, T F.F,
F F.(E), F F.id }
goto(I,E) ÷ ¦ E` F E., E F E.¹T }
goto(I,T) ÷ ¦ E F T., T F T.*F }
goto(I,F) ÷ ¦T F F.}
goto(I,() ÷ ¦ F F (.E), E F.E¹T, E F.T, T F.T*F, T F.F,
F F.(E), F F.id }
goto(I,id) ÷ ¦ F F id.}
46
onstruction of The anonical LR(0) ollection
· To create the SLR parsing tables Ior a grammar G, we will create the
canonical LR(0) collection oI the grammar G`.
· Algorithm:
C is ¦ closure(¦S`F.S}) }
repeat the Iollowings until no more set oI LR(0) items can be added to C.
for each I in C and each grammar symbol X
if goto(I,X) is not empty and not in C
add goto(I,X) to C
· goto Iunction is a DFA on the sets in C.
4
The anonical LR(0) ollection -- Example
I
0
: E` F .E I
1
: E` F E. I
6
: E F E¹.T I

: E F E¹T.
E F .E¹T E F E.¹T T F .T*F T F T.*F
E F .T T F .F
T F .T*F I
2
: E F T. F F .(E) I
10
: T F T*F.
T F .F T F T.*F F F .id
F F .(E)
F F .id I
3
: T F F. I

: T F T*.F I
11
: F F (E).
F F .(E)
I
4
: F F (.E) F F .id
E F .E¹T
E F .T I

: F F (E.)
T F .T*F E F E.¹T
T F .F
F F .(E)
F F .id
I
5
: F F id.
4
Transition Diagram (DF) of Goto Function
I
0
I
1
I
2
I
3
I
4
I
5
I
6
I

I

to I
2
to I
3
to I
4
I

to I
3
to I
4
to I
5
I
10
to I
4
to I
5
I
11
to I
6
to I

id
(
F
*
E
E
¹
T
T
T
)
F
F
F
(
id
id
(
*
(
id
¹
4
onstructing $LR Parsing Table
(of an augumented grammar G`)
1. Construct the canonical collection oI sets oI LR(0) items Ior G`.
C¦I
0
,...,I
n
}
2. Create the parsing action table as Iollows
· II a is a terminal, AF-.a. in I
i
and goto(I
i
,a)÷I
i
then action|i,a| is 8hift j.
· II AF-. is in I
i
, then action|i,a| is reduce AF- Ior all a in FOLLOW(A)
where AS`.
· II S`FS. is in I
i
, then action|i,$| is accept.
· II any conIlicting actions generated by these rules, the grammar is not SLR(1).
3. Create the parsing goto table
· Ior all non-terminals A, iI goto(I
i
,A)÷I
i
then goto|i,A|÷i
4. All entries not deIined by (2) and (3) are errors.
5. Initial state oI the parser contains S`F.S
50
Parsing Tables of Expression Grammar
state id + ` ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s r2 r2
3 r4 r4 r4 r4
4 s5 s4 2 3
5 r6 r6 r6 r6
6 s5 s4 3
s5 s4 10
s6 s11
r1 s r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
Action Table Goto Table
51
$LR(1) Grammar
· An LR parser using SLR(1) parsing tables Ior a grammar G is called as
the SLR(1) parser Ior G.
· II a grammar G has an SLR(1) parsing table, it is called SLR(1)
grammar (or SLR grammar in short).
· Every SLR grammar is unambiguous, but every unambiguous grammar
is not a SLR grammar.
52
shift/reduce and reduce/reduce conflicts
· II a state does not know whether it will make a shiIt operation or
reduction Ior a terminal, we say that there is a shift/reduce conflict.
· II a state does not know whether it will make a reduction operation
using the production rule or Ior a terminal, we say that there is a
reduce/reduce conflict.
· II the SLR parsing table oI a grammar G has a conIlict, we say that that
grammar is not SLR grammar.
53
onflict Example
S F L÷R I
0
: S` F .S I
1
: S` F S. I
6
: S F L÷.R I

: S F L÷R.
S F R S F .L÷R R F .L
LF*R S F .R I
2
: S F L.÷R LF.*R
L F id L F .*R R F L. L F .id
R F L L F .id
R F .L I
3
: S F R.
I
4
: L F *.R I

: L F *R.
Problem R F .L
FOLLOW(R)÷¦÷,$} LF.*R I

: R F L.
÷ shiIt 6 L F .id
reduce by R F L
shiIt/reduce conIlict I
5
: L F id.
54
onflict Example2
S F AaAb I
0
: S` F .S
S F BbBa S F .AaAb
A F 1 S F .BbBa
B F 1 A F .
B F .
Problem
FOLLOW(A)÷¦a,b}
FOLLOW(B)÷¦a,b}
a reduce by A F 1 b reduce by A F 1
reduce by B F 1 reduce by B F 1
reduce/reduce conIlict reduce/reduce conIlict
55
onstructing anonical LR(1) Parsing Tables
· In SLR method, the state i makes a reduction by AF- when the current
token is a:
iI the AF-. in the I
i
and a is FOLLOW(A)
· In some situations, .A cannot be Iollowed by the terminal a in
a right-sentential Iorm when .- and the state i are on the top stack.
This means that making reduction in this case is not correct.
S F AaAb SAaAbAabab SBbBaBbaba
S F BbBa
A F 1 Aab 1 ab Bba 1 ba
B F 1 AaAb Aa 1 b BbBa Bb 1 a
56
LR(1) Item
· To avoid some oI invalid reductions, the states need to carry more
inIormation.
· Extra inIormation is put into a state by including a terminal symbol as a
second component in an item.
· A LR(1) item is:
A F -..,a where a is the look-head oI the LR(1) item
(a is a terminal or end-marker.)
5
LR(1) Item (cont.)
· When . ( in the LR(1) item A F -..,a ) is not empty, the look-head
does not have any aIIect.
· When . is empty (A F -.,a ), we do the reduction by AF- only iI
the next input symbol is a (not Ior any terminal in FOLLOW(A)).
· A state will contain A F -.,a
1
where ¦a
1
,...,a
n
} FOLLOW(A)
...
A F -.,a
n
5
anonical ollection of $ets of LR(1) Items
· The construction oI the canonical collection oI the sets oI LR(1) items
are similar to the construction oI the canonical collection oI the sets oI
LR(0) items, except that clos:re and goto operations work a little bit
diIIerent.
closure(I) is: ( where I is a set oI LR(1) items)
every LR(1) item in I is in closure(I)
iI AF-.B.,a in closure(I) and BF is a production rule oI G;
then BF.,b will be in the closure(I) Ior each terminal b in
FIRST(.a) .
5
goto operation
· II I is a set oI LR(1) items and X is a grammar symbol
(terminal or non-terminal), then goto(I,X) is deIined as
Iollows:
II A F -.X.,a in I
then every item in closure(] F -X...a<) will be in
goto(I,X).
60
onstruction of The anonical LR(1) ollection
· Algorithm:
C is ¦ closure(¦S`F.S,$}) }
repeat the Iollowings until no more set oI LR(1) items can be added to C.
for each I in C and each grammar symbol X
if goto(I,X) is not empty and not in C
add goto(I,X) to C
· goto Iunction is a DFA on the sets in C.
61
$hort otation for The $ets of LR(1) Items
· A set oI LR(1) items containing the Iollowing items
A F -..,a
1
...
A F -..,a
n
can be written as
A F -..,a
1
/a
2
/.../a
n
62
anonical LR(1) ollection -- Example
S F AaAb I
0
: S` F .S ,$ I
1
: S` F S. ,$
S F BbBa S F .AaAb ,$
A F 1 S F .BbBa ,$ I
2
: S F A.aAb ,$
B F 1 A F . ,a
B F . ,b I
3
: S F B.bBa ,$
I
4
: S F Aa.Ab ,$ I
6
: S F AaA.b ,$ I

: S F AaAb. ,$
A F . ,b
I
5
: S F Bb.Ba ,$ I

: S F BbB.a ,$ I

: S F BbBa. ,$
B F . ,a
S
A
B
a
b
A
B
a
b
to I
4
to I
5
63
anonical LR(1) ollection - Example2
S` F S
1) S F L÷R
2) S F R
3) LF *R
4) L F id
5) R F L
I
0
:S` F .S,$
S F .L÷R,$
S F .R,$
L F .*R,$/÷
L F .id,$/÷
R F .L,$
I
1
:S` F S.,$
I
2
:S F L.÷R,$
R F L.,$
I
3
:S F R.,$
I
4
:L F *.R,$/÷
R F .L,$/÷
LF .*R,$/÷
L F .id,$/÷
I
5
:L F id.,$/÷
I
6
:S F L÷.R,$
R F .L,$
L F .*R,$
L F .id,$
I

:L F *R.,$/÷
I

: R F L.,$/÷
I

:S F L÷R.,$
I
10
:R F L.,$
I
11
:L F *.R,$
R F .L,$
LF .*R,$
L F .id,$
I
12
:L F id.,$
I
13
:L F *R.,$
to I
6
to I

to I

to I
4
to I
5
to I
10
to I
11
to I
12
to I

to I
10
to I
11
to I
12
to I
13
id
S
L
L
L
R
R
R
id
id
id
R
L
*
*
*
*
I
4
and I
11
I
5
and I
12
I

and I
13
I

and I
10
64
onstruction of LR(1) Parsing Tables
1. Construct the canonical collection oI sets oI LR(1) items Ior G`.
C¦I
0
,...,I
n
}
2. Create the parsing action table as Iollows
· II a is a terminal, AF-.a.,b in I
i
and goto(I
i
,a)÷I
i
then action|i,a| is 8hift j.
· II AF-.,a is in I
i
, then action|i,a| is reduce AF- where AS`.
· II S`FS.,$ is in I
i
, then action|i,$| is accept.
· II any conIlicting actions generated by these rules, the grammar is not LR(1).
3. Create the parsing goto table
· Ior all non-terminals A, iI goto(I
i
,A)÷I
i
then goto|i,A|÷i
4. All entries not deIined by (2) and (3) are errors.
5. Initial state oI the parser contains S`F.S,$
65
LR(1) Parsing Tables - (for Example2)
id ` ÷ $ $ L R
0 s5 s4 1 2 3
1 acc
2 s6 r5
3 r2
4 s5 s4
5 r4 r4
6 s12 s11 10
7 r3 r3
8 r5 r5
9 r1
10 r5
11 s12 s11 10 13
12 r4
13 r3
no shiIt/reduce or
no reduce/reduce conIlict

so, it is a LR(1) grammar
66
LLR Parsing Tables
· LLR stands Ior Lookhead LR.
· LALR parsers are oIten used in practice because LALR parsing tables
are smaller than LR(1) parsing tables.
· The number oI states in SLR and LALR parsing tables Ior a grammar G
are equal.
· But LALR parsers recognize more grammars than SLR parsers.
· acc creates a LALR parser Ior the given grammar.
· A state oI LALR parser will be again a set oI LR(1) items.
6
reating LLR Parsing Tables
Canonical LR(1) Parser LALR Parser
shrink # oI states
· This shrink process may introduce a reduce/reduce conIlict in the
resulting LALR parser (so the grammar is OT LALR)
· But, this shrink process does not produce a shift/reduce conIlict.
6
The ore of $et of LR(1) Items
· The core oI a set oI LR(1) items is the set oI its Iirst component.
Ex: S F L.÷R,$ S F L.÷R Core
R F L.,$ R F L.
· We will Iind the states (sets oI LR(1) items) in a canonical LR(1) parser with same
cores. Then we will merge them as a single state.
I
1
:L F id.,÷ A new state: I
12
: L F id.,÷
L F id.,$
I
2
:L F id.,$ have same core, merge them
· We will do this Ior all states oI a canonical LR(1) parser to get the states oI the LALR
parser.
· In Iact, the number oI the states oI the LALR parser Ior a grammar will be equal to the
number oI states oI the SLR parser Ior that grammar.
6
reation of LLR Parsing Tables
· Create the canonical LR(1) collection oI the sets oI LR(1) items Ior
the given grammar.
· Find each core; Iind all sets having that same core; replace those sets
having same cores with a single set which is their union.
C÷¦I
0
,...,I
n
} C`÷¦J
1
,...,J
m
} where m A n
· Create the parsing tables (action and goto tables) same as the
construction oI the parsing tables oI LR(1) parser.
ote that: II J÷I
1
... I
k
since I
1
,...,I
k
have same cores
cores oI goto(I
1
,X),...,goto(I
2
,X) must be same.
So, goto(J,X)÷K where K is the union oI all sets oI items having same cores as goto(I
1
,X).
· II no conIlict is introduced, the grammar is LALR(1) grammar.
(We may only introduce reduce/reduce conIlicts; we cannot introduce
a shiIt/reduce conIlict)
0
$hift/Reduce onflict
· We say that we cannot introduce a shiIt/reduce conIlict during the
shrink process Ior the creation oI the states oI a LALR parser.
· Assume that we can introduce a shiIt/reduce conIlict. In this case, a state
oI LALR parser must have:
A F -.,a and B F ..a,b
· This means that a state oI the canonical LR(1) parser must have:
A F -.,a and B F ..a,c
But, this state has also a shiIt/reduce conIlict. i.e. The original canonical
LR(1) parser has a conIlict.
(Reason Ior this, the shiIt operation does not depend on lookaheads)
1
Reduce/Reduce onflict
· But, we may introduce a reduce/reduce conIlict during the shrink
process Ior the creation oI the states oI a LALR parser.
I
1
: A F -.,a I
2
: A F -.,b
B F ..,b B F ..,c

I
12
: A F -.,a/b reduce/reduce conIlict
B F ..,b/c
2
anonical LLR(1) ollection - Example2
S` F S
1) S F L÷R
2) S F R
3) LF *R
4) L F id
5) R F L
I
0
:S` F.S,$
S F.L÷R,$
S F.R,$
L F.*R,$/÷
L F.id,$/÷
R F.L,$
I
1
:S` F S.,$
I
2
:S F L.÷R,$
R F L.,$
I
3
:S F R.,$
I
411
:L F *.R,$/÷
R F.L,$/÷
LF.*R,$/÷
L F.id,$/÷
I
512
:L F id.,$/÷
I
6
:S F L÷.R,$
R F.L,$
L F.*R,$
L F.id,$
I
13
:L F *R.,$/÷
I
10
: R F L.,$/÷
I

:S F L÷R.,$
to I
6
to I
13
to I
10
to I
411
to I
512
to I
10
to I
411
to I
512
to I

S
L
L
L
R
R
id
id
id
R
*
*
*
Same Cores
I
4
and I
11
I
5
and I
12
I

and I
13
I

and I
10
3
LLR(1) Parsing Tables - (for Example2)
id ` ÷ $ $ L R
0 s5 s4 1 2 3
1 acc
2 s6 r5
3 r2
4 s5 s4
5 r4 r4
6 s12 s11 10
7 r3 r3
8 r5 r5
9 r1
no shiIt/reduce or
no reduce/reduce conIlict

so, it is a LALR(1) grammar
4
Using mbiguous Grammars
· All grammars used in the construction oI LR-parsing tables must be
un-ambiguous.
· Can we create LR-parsing tables Ior ambiguous grammars ?
es, but they will have conIlicts.
We can resolve these conIlicts in Iavor oI one oI them to disambiguate the grammar.
At the end, we will have again an unambiguous grammar.
· Why we want to use an ambiguous grammar?
Some oI the ambiguous grammars are much natural, and a corresponding unambiguous
grammar can be very complex.
Usage oI an ambiguous grammar may eliminate unnecessary reductions.
· Ex.
E F E¹T ' T
E F E¹E ' E*E ' (E) ' id T F T*F ' F
F F (E) ' id
5
$ets of LR(0) Items for mbiguous Grammar
I
0
: E` F.E
E F.E¹E
E F.E*E
E F.(E)
E F.id
I
1
: E` F E.
E F E .¹E
E F E .*E
I
2
: E F (.E)
E F.E¹E
E F.E*E
E F.(E)
E F.id
I
3
: E F id.
I
4
: E F E ¹.E
E F.E¹E
E F.E*E
E F.(E)
E F.id
I
5
: E F E *.E
E F.E¹E
E F.E*E
E F.(E)
E F.id
I
6
: E F (E.)
E F E.¹E
E F E.*E
I

: E F E¹E.
E F E.¹E
E F E.*E
I

: E F E*E.
E F E.¹E
E F E.*E
I

: E F (E).
I
5
)
E
E
E
E
*
¹
¹
¹
¹
*
*
*
(
(
(
(
id
id
id
id
I
4
I
2
I
2
I
3
I
3
I
4
I
4
I
5
I
5
6
$LR-Parsing Tables for mbiguous Grammar
FOLLOW(E) ÷ ¦ }
State I

has shiIt/reduce conIlicts Ior symbols and .
I
0
I
1
I

I
4
E ¹ E
when current token is ¹
shiIt ¹ is right-associative
reduce ¹ is leIt-associative
when current token is *
shiIt * has higher precedence than ¹
reduce ¹ has higher precedence than *

$LR-Parsing Tables for mbiguous Grammar
FOLLOW(E) ÷ ¦ }
State I

has shiIt/reduce conIlicts Ior symbols and .
I
0
I
1
I

I
5
E * E
when current token is *
shiIt * is right-associative
reduce * is leIt-associative
when current token is ¹
shiIt ¹ has higher precedence than *
reduce * has higher precedence than ¹

$LR-Parsing Tables for mbiguous Grammar
id + ` ( ) $ E
0 s3 s2 1
1 s4 s5 acc
2 s3 s2 6
3 r4 r4 r4 r4
4 s3 s2
5 s3 s2
6 s4 s5 s
r1 s5 r1 r1
r2 r2 r2 r2
r3 r3 r3 r3
ction Goto

$LR-Parsing Tables for mbiguous Grammar
id + ` ( ) $ E
0 s3 e1 e1 s2 e2 e1 1
1 e3 s4 s5 e3 e2 acc
2 s3 e1 e1 s2 e2 e1 6
3 r4 r4 r4 r4 r4 r4
4 s3 e1 e1 s2 e2 e1
5 s3 e1 e1 s2 e2 e1
6 e3 s4 s5 e3 s e4
r1 r1 s5 r1 r1 r1
r2 r2 r2 r2 r2 r2
r3 r3 r3 r3 r3 r3
ction Goto
0
Using mbiguous Grammars
· All grammars used in the construction oI LR-parsing tables must be
un-ambiguous.
· Can we create LR-parsing tables Ior ambiguous grammars ?
es, but they will have conIlicts.
We can resolve these conIlicts in Iavor oI one oI them to disambiguate the grammar.
At the end, we will have again an unambiguous grammar.
· Why we want to use an ambiguous grammar?
Some oI the ambiguous grammars are much natural, and a corresponding unambiguous
grammar can be very complex.
Usage oI an ambiguous grammar may eliminate unnecessary reductions.
· Ex.
E F E¹T ' T
E F E¹E ' E*E ' id T F T*F ' F
F F id
1
$ets of LR(0) Items for mbiguous Grammar
I
0
: E` F.E
E F.E¹E
E F.E*E
E F.id
I
1
: E` F E.
E F E .¹E
E F E .*E
I
2
: E F id.
I
3
: E F E ¹.E
E F.E¹E
E F.E*E
E F.id
I
4
: E F E *.E
E F.E¹E
E F.E*E
E F.id
I
5
: E F E¹E.
E F E.¹E
E F E.*E
I
6
: E F E*E.
E F E.¹E
E F E.*E
I
5
E
E E
*
¹
¹
¹
*
*
id
id
id
I
3
I
3
I
4
I
4
I
5
2
$LR-Parsing Tables for mbiguous Grammar
FOLLOW(E) ÷ ¦ }
State I

has shiIt/reduce conIlicts Ior symbols and .
I
0
I
1
I

I
4
E ¹ E
when current token is ¹
shiIt ¹ is right-associative
reduce ¹ is leIt-associative
when current token is *
shiIt * has higher precedence than ¹
reduce ¹ has higher precedence than *
3
$LR-Parsing Tables for mbiguous Grammar
FOLLOW(E) ÷ ¦ }
State I

has shiIt/reduce conIlicts Ior symbols and .
I
0
I
1
I

I
5
E * E
when current token is *
shiIt * is right-associative
reduce * is leIt-associative
when current token is ¹
shiIt ¹ has higher precedence than *
reduce * has higher precedence than ¹
4
$LR-Parsing Tables for mbiguous Grammar
id + ` $ E
0 s2 e1 e1 e1 1
1 e2 s3 s4 acc
2 e2 r3 r3 r3
3 s2 e1 e1 e1 5
4 s2 e1 e1 e1 6
5 e2 r1 s4 r1
6 e2 r2 r2 r2
ction Goto
5
$LR-Parsing Tables for mbiguous Grammar
Stack I/P Error message and action
0 Id¹*$ shiIt
0id2 ¹*$ Reduce E÷ id
0E1 ¹*$ shiIt
0E1¹3 *$ 'id expected¨ pushed and 2 to stack
0E1¹3id2 *$ Reduce E÷ id
0E1¹3E5 $ shiIt
0E1¹3E5*4 $ 'id expected¨ pushed and 2 to stack
0E1¹3E5*4id2 $ Reduce E÷ id
0E1¹3E5*4E6 $ Reduce E÷ E*E
0E1¹3E5 $ Reduce E÷ E¹E
0E1 $ accept
6
Error Recovery in LR Parsing
· An LR parser will detect an error when it consults the parsing action
table and Iinds an error entry. All empty entries in the action table are
error entries.
· Errors are never detected by consulting the goto table.
· An LR parser will announce error as soon as there is no valid
continuation Ior the scanned portion oI the input.
· A canonical LR parser (LR(1) parser) will never make even a single
reduction beIore announcing an error.
· The SLR and LALR parsers may make several reductions beIore
announcing an error.
· But, all LR parsers (LR(1), LALR and SLR parsers) will never shiIt an
erroneous input symbol onto the stack.

Panic Mode Error Recovery in LR Parsing
· Scan down the stack until a state s with a goto on a particular
nonterminal is Iound. (Get rid oI everything Irom the stack beIore this
state s).
· Discard zero or more input symbols until a symbol a is Iound that can
legitimately Iollow A.
The symbol a is simply in FOLLOW(A), but this may not work Ior all situations.
· The parser stacks the nonterminal and the state goto[s.(, and it
resumes the normal parsing.
· This nonterminal A is normally is a basic programming block (there can
be more than one choice Ior A).
stmt, expr, block, ...

Phrase-Level Error Recovery in LR Parsing
· Each empty entry in the action table is marked with a speciIic error
routine.
· An error routine reIlects the error that the user most likely will make in
that case.
· An error routine inserts the symbols into the stack or the input (or it
deletes the symbols Irom the stack and the input, or it can do both
insertion and deletion).
missing operand
unbalanced right parenthesis