You are on page 1of 23

Compiler Construction(CS-462)

(Lecture 16)

Instructor:
Gul Sher Ali

______________________________________________________
University of Gujrat
1
Non- Recursive predictive
parsing

 Asper discussed in class /


notes

2
Syntax Error

 As per discussed in class / notes

3
4
5
Outline
 Top-down v.s. Bottom-up
 Top-down parsing  Bottom-up parsing
 Shift-reduce parsers
 Recursive-descent
parsing  LR(0) parsing
 LR(0) items
 LL(1) parsing
 Finite automata of items
 LL(1) parsing
 LR(0) parsing algorithm
algorithm
 LR(0) grammar
 First and follow sets
 SLR(1) parsing
 Constructing LL(1)  SLR(1) parsing algorithm
parsing table
 SLR(1) grammar
 Error recovery  Parsing conflict
 LALR(1)
Chapter 4 Parsing 2301373 6
Parsing Techniques
Bottom-up parsers
 Start in a state valid for
legal first tokens
 Bottom-up parsers handle a
large class of grammars
7
Bottom-up Parsing

 Use explicit stack to perform a parse


 Simulate rightmost derivation (R) from left (L) to right,
thus called LR parsing
 More powerful than top-down parsing
 Left recursion does not cause problem
 Two actions
 Shift: take next input token into the stack
 Reduce: replace a string B on top of stack by a
nonterminal A, given a production A  B

8
Terminologies
 Shifting :
 Movement of input symbol to stack is called shifting.

 Bottom-up parsing is call LR(0) parsing.

 LR-Parsing is performed by using Shift-Reduce


parsing technique.

 Reverse of derivation is called reduction.


Example of Shift-reduce Parsing
 Grammar
E  E+T|T
T  T*F|F
F  ( E ) | id
 Parsing actions
Stack Input Action
$ id * id $ shift
$ id * id $
$F * id $ reduce F  id
$T * id $ reduce T  F
$ T* id $ shift
$ T* id $ shift
$ T* F $ reduce F  id
$T $ reduce T  T *F
$E $ reduce E  T
$E $ accept
10
2nd Example of Shift-reduce
Parsing
 Grammar
S’  S
S  (S)S | 
 Reverse of
 rightmost derivation
 Parsing actions
 from left to right
Stack Input Action
1 (())
$ ( ( ) ) $ shift
$( ( ) ) $ shift 2 (())
$(( ) ) $ reduce S   3 (())
$((S ) ) $ shift 4 ((S))
$((S) ) $ reduce S   5 ((S))
$((S)S ) $ reduce S  ( S ) S 6 ((S)S)
$(S ) $ shift 7 (S)
$(S) $ reduce S   8 (S)
$(S)S $ reduce S  ( S ) S 9 (S)S
$S $ accept 10 S’ S
2301373 11
Terminologies
 Right sentential form  Right sentential form
 (S)S
 sentential form in a rightmost  ((S)S)
derivation
 Viable prefix
 Viable prefix  ( S ) S, ( S ), ( S, (
 sequence of symbols on the  ( ( S ) S, ( ( S ), ( ( S , ( (, (
parsing stack  Handle
 ( S ) S. with S  
 Handle  ( S ) S . with S  
 right sentential form +  ( ( S ) S . ) with S  ( S ) S
position where reduction can
be performed + production  LR(0) item
used for reduction  S ( S ) S.
 S (S).S
 LR(0) item  S (S.)S
 S (.S)S
 production with distinguished  S .(S)S
position in its RHS 12
Shift-reduce parsers

 There are two possible actions:


 shift and reduce
 Parsing is completed when
 the input stream is empty and
 the stack contains only the start symbol
 The grammar must be augmented
 a new start symbol S’ is added
 a production S’  S is added
 To make sure that parsing is finished when S’ is on top of stack
because S’ never appears on the RHS of any production.

2301373 13
Conflicts

 Shift-reduceand reduce-
reduce conflicts are caused by
 The limitations of the LR parsing method (even when the
grammar is unambiguous)
 Ambiguity of the grammar

14
LR parsing

The LR parser is a non-recursive, shift-reduce, bottom-up


parser. It uses a wide class of context-free grammar which makes
it the most efficient syntax analysis technique.
LR parsers are also known as LR(k) parsers, where L stands
for left-to-right scanning of the input stream; R stands for the
construction of right-most derivation in reverse, and k denotes
the number of look ahead symbols to make decisions.
LR parsers do not need left-factored grammars. LR parsers can
handle left-recursive grammars.

15
The image given below depicts the bottom-up parsers
available.

16
There are three widely used algorithms available for constructing an
LR parser:

SLR(1) – Simple LR Parser:


Works on smallest class of grammar
Few number of states, hence very small table
Simple and fast construction
LR(1) – LR Parser:
Works on complete set of LR(1) Grammar
Generates large table and large number of states
Slow construction
LALR(1) – Look-Ahead LR Parser:
Works on intermediate size of grammar
Number of states are same as in SLR(1)
17
LR(0) parsing
 Keep track of what is left to be done in the
parsing process by using finite automata of
items
 An item A  w . B y means:
 A  w B y might be used for the reduction in the
future,
 at the time, we know we already construct w in the
parsing process,
 if B is constructed next, we get the new item
AwB.Y

2301373 18
LR(0) Automation Rules
 The grammar must be augmented
 a new start symbol S’ is added
 a production S’  S is added
 It-self to closure.
 When the next symbol is NT, then add its’
productions
 If the added symbol order 2 again have NT,
then add it’s production.

2301373 19
Finite automata of items
Grammar:
S’  .S S S’  S.
S’  S S  .(S)S
S  (S)S S.
S  (S.)S
S  ( S
Items: )
S  (.S)S
S’  .S S  .(S)S
S.
S’  S.
( ( S  (S).S
S  .(S)S S  .(S)S
S  (.S)S S.

S  (S.)S
S
S  (S).S
S  (S)S. S  (S)S.
S. 20
Example for LR(0)

Grammar:
S  AA
S b c
A b a A
A c

Finite automata of items ?

21
Example for LR(0)

Grammar:
E  E +T | T
T  T *F | F
F  ( E ) | id

Finite automata of items ?

22
Non-LR(0)Grammar

 Conflict
S’  .S S S’  S. 1
 Shift-reduce conflict S  .(S)S
A state contains a S.
0 S  (S.)S 3
complete item A  x. (
and a shift item A  x.By S
S  (.S)S )
 Reduce-reduce conflict
S  .(S)S
A state contains more S .
2
than one complete items.
( ( S  (S).S
S  .(S)S
 A grammar is a LR(0) S .
4
grammar if there is no
conflict in the S
grammar. S  (S)S. 5
2301373 23

You might also like