SlideShare une entreprise Scribd logo
1  sur  56
SYNTAX ANALYSIS
OR
PARSING
Lecture 07-08
BOTTOM-UP PARSING
• A bottom-up parser creates the parse tree of the
given input starting from leaves towards the root
• A bottom-up parser tries to find the right-most
derivation of the given input in the reverse order.
S  ...  (the right-most derivation of )
 (the bottom-up parser finds the right-most
derivation in the reverse order)
IffatAnjum,Lecturer,BRACUniversity
RIGHTMOST DERIVATION
IffatAnjum,Lecturer,BRACUniversity
RIGHTMOST DERIVATION IN REVERSE
IffatAnjum,Lecturer,BRACUniversity
RIGHTMOST DERIVATION IN REVERSE
LR parsing corresponds to rightmost derivation in reverse
IffatAnjum,Lecturer,BRACUniversity
• A reduction step replaces a specific substring
(matching the body of a production)
• Reduction is the opposite of derivation
• Bottom up parsing is a process of reducing a string 
to the start symbol S of the grammar
REDUCTION
IffatAnjum,Lecturer,BRACUniversity
HANDLE
• Informally, a handle is a substring (in the parsing string)
that matches the right side of a production rule.
– But not every substring matches the right side of a
production rule is handle
• A handle of a right sentential form  ( ) is a
production rule A   and a position of 
where the string  may be found and replaced by A to
produce the previous right-sentential form in a rightmost
derivation  .
S  A  
IffatAnjum,Lecturer,BRACUniversity
HANDLE PRUNING
• A right-most derivation in reverse can be obtained by
handle-pruning.
rm rm rm rm rm
S=0  1  2  ...  n-1  n= 
input string
• Start from n, find a handle Ann in n,
and replace n in by An to getn-1.
• Then find a handle An-1n-1 in n-1,
and replace n-1 in by An-1 to getn-2.
• Repeat this, until we reach S.
n-th right-sentential form
IffatAnjum,Lecturer,BRACUniversity
SHIFT-REDUCE PARSING
• Bottom-up parsing is also known as shift-reduce
parsing because its two main actions are shift and
reduce.
• data structures: input-string and stack
• Operations
– At each shift action, the current symbol in the input
string is pushed to a stack.
– At each reduction step, the symbols at the top of the
stack (this symbol sequence is the right side of a
production) will replaced by the non-terminal at the left
side of that production.
– Accept: Announce successful completion of parsing
– Error: Discover a syntax error and call error recovery
IffatAnjum,Lecturer,BRACUniversity
SHIFT REDUCE PARSING
S  a T R e
T  T b c | b
R  d
Remaining input: abbcde
Rightmost derivation:
S  a T R e
 a T d e
 a T b c d e
 a b b c d e
IffatAnjum,Lecturer,BRACUniversity
SHIFT REDUCE PARSING
S  a T R e
T  T b c | b
R  d
a b
Shift a, Shift b
Remaining input: bcde
Rightmost derivation:
S  a T R e
 a T d e
 a T b c d e
 a b b c d e
IffatAnjum,Lecturer,BRACUniversity
SHIFT REDUCE PARSING
S  a T R e
T  T b c | b
R  d
a b
Shift a, Shift b
Reduce T  b


T
Remaining input: bcde
Rightmost derivation:
S  a T R e
 a T d e
 a T b c d e
 a b b c d e
IffatAnjum,Lecturer,BRACUniversity
SHIFT REDUCE PARSING
S  a T R e
T  T b c | b
R  d
a b
Shift a, Shift b
Reduce T  b
Shift b, Shift c


 T
b c
Remaining input: de
Rightmost derivation:
S  a T R e
 a T d e
 a T b c d e
 a b b c d e
IffatAnjum,Lecturer,BRACUniversity
SHIFT REDUCE PARSING
S  a T R e
T  T b c | b
R  d
a b
Shift a, Shift b
Reduce T  b
Shift b, Shift c
Reduce T  T b c




T
b c
T
Remaining input: de
Rightmost derivation:
S  a T R e
 a T d e
 a T b c d e
 a b b c d e
IffatAnjum,Lecturer,BRACUniversity
SHIFT REDUCE PARSING
S  a T R e
T  T b c | b
R  d
a b
Shift a, Shift b
Reduce T  b
Shift b, Shift c
Reduce T  T b c
Shift d





T
b c
T
d
Remaining input: e
Rightmost derivation:
S  a T R e
 a T d e
 a T b c d e
 a b b c d e
IffatAnjum,Lecturer,BRACUniversity
SHIFT REDUCE PARSING
S  a T R e
T  T b c | b
R  d
a b
Shift a, Shift b
Reduce T  b
Shift b, Shift c
Reduce T  T b c
Shift d
Reduce R  d






T
b c
T
d
R
Remaining input: e
Rightmost derivation:
S  a T R e
 a T d e
 a T b c d e
 a b b c d e
IffatAnjum,Lecturer,BRACUniversity
SHIFT REDUCE PARSING
S  a T R e
T  T b c | b
R  d
a b
Shift a, Shift b
Reduce T  b
Shift b, Shift c
Reduce T  T b c
Shift d
Reduce R  d
Shift e







T
b c
T
d
R
e
Remaining input:
Rightmost derivation:
S  a T R e
 a T d e
 a T b c d e
 a b b c d e
IffatAnjum,Lecturer,BRACUniversity
SHIFT REDUCE PARSING
S  a T R e
T  T b c | b
R  d
a b
Shift a, Shift b
Reduce T  b
Shift b, Shift c
Reduce T  T b c
Shift d
Reduce R  d
Shift e
Reduce S  a T R e








T
b c
T
d
R
e
S
Remaining input:
Rightmost derivation:
S  a T R e
 a T d e
 a T b c d e
 a b b c d e
IffatAnjum,Lecturer,BRACUniversity
EXAMPLE SHIFT-REDUCE PARSING
Consider the grammar:
Stack Input Action
$ id1 + id2$ shift
$id1 + id2$ reduce 6
$F + id2$ reduce 4
$T + id2$ reduce 2
$E + id2$ shift
$E + id2$ shift
$E + id2 reduce 6
$E + F reduce 4
$E + T reduce 1
$E accept
IffatAnjum,Lecturer,BRACUniversity
SHIFT-REDUCE PARSING
• Handle will always appear on Top of stack, never
inside
• Possible forms of two successive steps in any
rightmost derivation
• CASE 1:
S
A
B
   y z
S * Az  Byz 
yz
rm rm rm
STACK
$
INPUT
yz$
After Reducing the handle
$B yz$
Shifting from Input
$By z$
Reduce the handle
$A z$
IffatAnjum,Lecturer,BRACUniversity
SHIFT-REDUCE PARSING
• Case 2:
S
A
y z
B
  x
S * BxAz  Bxyz 
xyz
rm rm rm
STACK
$
$B
$Bxy
$BxA
INPUT
xyz$
After Reducing the handle
xyz$
Shifting from Input
z$
Reducing the handle
z$
IffatAnjum,Lecturer,BRACUniversity
CONFLICTS DURING SHIFT-REDUCE PARSING
• There are context-free grammars for which shift-reduce parsers
cannot be used.
• Stack contents and the next input symbol may not decide action:
– shift/reduce conflict: Whether make a shift
operation or a reduction.
– reduce/reduce conflict: The parser cannot
decide which of several reductions to make.
• If a shift-reduce parser cannot be used for a grammar, that grammar is
called as non-LR(k) grammar.
k lookheadleft to right
scanning
right-most
derivation
• An ambiguous grammar can never be a LR grammar.
IffatAnjum,Lecturer,BRACUniversity
SHIFT-REDUCE CONFLICT IN AMBIGUOUS
GRAMMAR
stmt  if expr then stmt
| if expr then stmt else stmt
| other
STACK INPUT
….if expr then stmt else….$
 We can’t decide whether to shift or reduce?
IffatAnjum,Lecturer,BRACUniversity
REDUCE-REDUCE CONFLICT IN AMBIGUOUS
GRAMMAR
1. stmt  id(parameter_list)
2. stmt  expr:=expr
3. parameter_list  parameter_list, parameter
4. parameter_list  parameter
5. parameter_list  id
6. expr  id(expr_list)
7. expr  id
8. expr_list  expr_list, expr
9. expr_list  expr
STACK INPUT
….id ( id , id ) …$
 We can’t decide which production will be used to
reduce id?
IffatAnjum,Lecturer,BRACUniversity
SHIFT-REDUCE PARSERS
There are two main categories of shift-reduce parsers
1. Operator-Precedence Parser
– simple, but only a small class of grammars.
CFG
LR
LALR
SLR
2. LR-Parsers
– covers wide range of 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 different.
IffatAnjum,Lecturer,BRACUniversity
LR PARSERS
LR parsing is attractive because:
– LR parsing is most general non-backtracking shift-reduce
parsing,
yet it is still efficient.
– The class of grammars that can be parsed using LR methods is
a proper superset of the class of 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 left-to-right scan of the input.
– LR parsers can be constructed to recognize virtually all
programming language constructs for which CFG grammars
canbe written
Drawback of LR method:
– Too much work to construct LR parser by hand
• Fortunately tools (LR parsers generators) are available
IffatAnjum,Lecturer,BRACUniversity
LL VS. LR
 LR (shift reduce) is more powerful than LL
(predictive parsing)
 Can detect a syntactic error as soon as possible.
 LR is difficult to do by hand (unlike LL)
IffatAnjum,Lecturer,BRACUniversity
LR PARSING ALGORITHM
Sm
Xm
Sm-1
Xm-1
.
.
S1
X1
S0
a1 ... ai ... an $
Action Table
terminals and $
Goto Table
non-terminal
s
t
a
t
e
s
four different
actions
s
t each item is
a a state number
t
e
s
LR Parsing Algorithm
input
stack
output
IffatAnjum,Lecturer,BRACUniversity
A CONFIGURATION OF LR PARSING
ALGORITHM
• A configuration of a LR parsing is:
( So X1 S1 ... Xm Sm, ai ai+1 ... an $ )
Stack Rest of Input
• Sm and ai decides the parser action by consulting the
parsing action table. (Initial Stack contains just So
)
• A configuration of a LR parsing represents the right sentential
form:
X1 ... Xm ai ai+1 ... an $
IffatAnjum,Lecturer,BRACUniversity
ACTIONS OF A LR-PARSER
1. shift s -- shifts the next input symbol and the state s onto the stack
( So X1 S1 ... Xm Sm, ai ai+1 ... an $ )€ ( So X1 S1 ... Xm Sm ai s, ai+1 ... an $ )
2. reduce A (or rN where N is a production number)
– pop 2|| (=r) items from the stack;
– then push Aand s where s=goto[sm-r,A]
( So X1 S1 ... Xm Sm, ai ai+1 ... an $ )€ ( So X1 S1 ... Xm-r Sm-r A s, ai ... an $ )
– Output is the reducing production reduce A
2. Accept – Parsing successfully completed
3. Error -- Parser detected an error (an empty entry in the action table)
IffatAnjum,Lecturer,BRACUniversity
Reduce Action
• pop 2||(=r) items fromthe stack; let us assume that
 = Y1Y2...Yr
• then push Aand s where s=goto[sm-r,A]
( So X1 S1 ... Xm-r Sm-r Y1 Sm-r ...Yr Sm, ai ai+1 ... an $ )
€ ( So X1 S1 ... Xm-r Sm-r A s, ai ... an $ )
• In fact, Y1Y2...Yris a handle.
X1 ... Xm-r A ai ... an $ c X1 ... Xm Y1...Yr ai ai+1 ... an $
IffatAnjum,Lecturer,BRACUniversity
LR PARSER STACK(S)
IffatAnjum,Lecturer,BRACUniversity
LR PARSER STACK(S)
IffatAnjum,Lecturer,BRACUniversity
CONSTRUCTING SLR PARSING TABLES –
LR(0) ITEM
• An item indicates how much of a production we have seen at a
given point in the parsing process
• For Example the item A  X  YZ
– We have already seen on the input a string derivable from X
– We hope to see a string derivable from YZ
• For Example the item A   XYZ
– We hope to see a string derivable from XYZ
• For Example the item A  XYZ 
– We have already seen on the input a string derivable from XYZ
– It is possibly time to reduce XYZ to A
• Special Case:
Rule: A   yields only one item
A  
IffatAnjum,Lecturer,BRACUniversity
CONSTRUCTING SLR PARSING TABLES
• A collection of sets of LR(0) items (the canonical
LR(0) collection) is the basisfor constructing
SLR parsers.
• Canonical LR(0) collection provides the basis of
constructing a DFA called LR(0) automaton
– This DFA is used to make parsing decisions
• Each state of LR(0) automaton represents a set of
items in the canonical LR(0) collection
• To construct the canonical LR(0) collection for a
grammar
– Augmented Grammar
– CLOSURE function
– GOTO function
IffatAnjum,Lecturer,BRACUniversity
CONSTRUCTING SLR PARSING TABLES –
LR(0) ITEM
• An LR(0) item of a grammar G is a production of G a dot at the
some position of the right side.
• Ex: A  aBb Possible LR(0) Items: A   aBb
(four different possibility) A  a  Bb
A  aB  b
A  aBb 
• Sets of LR(0) items will be the states of action and goto table of
the SLR parser.
– States represent sets of “items”
• LR parser makes shift-reduce decision by maintaining states to
keep track of where we are in a parsing process
IffatAnjum,Lecturer,BRACUniversity
GRAMMAR AUGMENTATION
IffatAnjum,Lecturer,BRACUniversity
THE CLOSURE OPERATION
• If I is a set of LR(0) items for a grammar G, then
closure(I) is the set of LR(0) items constructed from I
by the two rules:
1. Initially, every LR(0) item in I is added to closure(I).
2. If A  .B is in closure(I) and
B is a production rule of G;
then B. will be in the closure(I).
We will apply this rule until no more new LR(0)
items can be added to closure(I).
IffatAnjum,Lecturer,BRACUniversity
THE CLOSURE OPERATION -- EXAMPLE
E'  E closure({E'  .E}) =
E  E+T { E'  E kernel
items
E  T E  E+T
T  T*F E  T
T  F T  T*F
F  (E) T  F
F  id F  (E)
F  id }
IffatAnjum,Lecturer,BRACUniversity
GOTO OPERATION
• If I is a set of LR(0) items and X is a grammar symbol (terminal
or non-terminal), then GOTO(I,X) is defined as follows:
– If A   X in I
then every item in closure({A  X  }) willbe in
GOTO(I,X).
Example:
E   T,I ={ E’   E, E   E+T,
T   T*F,T   F,
F   (E), F   id }
 T, T   T*F, T   F,
GOTO(I,E) = { E’  E  , E  E  +T }
GOTO(I,T) = { E  T  , T  T  *F }
GOTO(I,F) = {T  F  }
GOTO(I,() = { F  ( E), E   E+T, E 
F   (E), F   id }
GOTO(I,id) = { F  id  }
IffatAnjum,Lecturer,BRACUniversity
CONSTRUCTION OF THE CANONICAL LR(0)
COLLECTION (CC)
• To create the SLR parsing tables for a grammar G, we will
create the canonical LR(0) collection of the grammar G’.
• Algorithm:
C is { closure({S'  S}) }
repeat the followings until no more set of 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 function is a DFA on the sets in C.
IffatAnjum,Lecturer,BRACUniversity
THE CANONICAL LR(0) COLLECTION --
EXAMPLE
I0: E’  .E
E  .E+T
I1: E’  E.
E  E.+T
I6: E  E+.T
T  .T*F
I9: E  E+T.
T  T.*F
E  .T T  .F
T  .T*F
T  .F
I2: E  T.
T  T.*F
F  .(E)
F  .id
I10: T  T*F.
F  .(E)
F  .id I3: T  F. I7: T  T*.F
F  .(E)
I11: F  (E).
I4: F  (.E)
E  .E+T
F  .id
E  .T
T  .T*F
I8: F  (E.)
E  E.+T
T  .F
F  .(E)
F  .id
I5: F  id.
IffatAnjum,Lecturer,BRACUniversity
TRANSITION DIAGRAM (DFA) OF GOTO
FUNCTION
I0 I1
I2
I3
I6
I7
I8
to I2
to I3
to I4
I9
to I3
I10
to I4
to I5
I11
to I6
to I7
id to I4
to I5
(
F
*
E
E T
T
)
+
F
T
F
(
I4
id
I5
id
(
*
F
(
id
+
IffatAnjum,Lecturer,BRACUniversity
CONSTRUCTING SLR PARSING TABLE
(OF AN AUGUMENTED GRAMMAR G’)
1. Construct the canonical collection of sets of LR(0)
items for G’. C{I0,...,In}
2. Create the parsing action table as follows:
 If a is a terminal, A.a in Ii and goto(Ii,a)=Ij then
action[i,a] is shift j.
 If A. is in Ii , then action[i,a] is reduce A
for all a in FOLLOW(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 SLR(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 containsS’.S
IffatAnjum,Lecturer,BRACUniversity
PARSING TABLES OF EXPRESSION GRAMMAR
state id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
Action Table Goto Table
IffatAnjum,Lecturer,BRACUniversity
(SLR) PARSING TABLES FOR EXPRESSION
GRAMMAR
state id + * ( ) $ E T F
0 s5 s4 1 2 3
1 s6 acc
2 r2 s7 r2 r2
3 r4 r4 r4 r4
4 s5 s4 8 2 3
5 r6 r6 r6 r6
6 s5 s4 9 3
7 s5 s4 10
8 s6 s11
9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
Action Table Goto Table
Key to Notation
S4=“Shift input symbol
and push state 4”
R5= “Reduce by rule 5”
Acc=Accept
(blank)=Syntax Error
IffatAnjum,Lecturer,BRACUniversity
EXAMPLE LR PARSE: (ID+ID)*ID
IffatAnjum,Lecturer,BRACUniversity
EXAMPLE LR PARSE: (ID+ID)*ID
IffatAnjum,Lecturer,BRACUniversity
EXAMPLE LR PARSE: (ID+ID)*ID
IffatAnjum,Lecturer,BRACUniversity
ACTIONS OF A (S)LR-PARSER -- EXAMPLE
stack
0
input
id*id+id$
action
shift 5
output
0id5 *id+id$ reduce by Fid Fid
0F3
0T2
0T2*7
*id+id$
*id+id
$
id+id
$
reduce by TF
shift 7
shift 5
TF
0T2*7id5 +id$ reduce by Fid Fid
0T2*7F10 +id$ reduce by TT*F TT*F
0T2
0E1
0E1+6
0E1+6id5
+id$
+i
d$
id$
$
reduce by ET
shift 6
shift 5
reduce by Fid
ET
Fid
0E1+6F3 $ reduce by TF TF
0E1+6T9
0E1
$
$
reduce by EE+T
accept
EE+
T
IffatAnjum,Lecturer,BRACUniversity
LR PARSING ALGORITHM
IffatAnjum,Lecturer,BRACUniversity
SLR GRAMMAR: REVIEW
 An LR parser using SLR parsing tables for a
grammar G is called as the SLR parser for G.
 If a grammar G has an SLR parsing table, it is
called SLR grammar (or SLR grammar in short).
 Every SLR grammar is unambiguous, but every
unambiguous grammar is not a SLR grammar.
 If the SLR parsing table of a grammar G has a
conflict, we say that that grammar is not SLR
grammar.
IffatAnjum,Lecturer,BRACUniversity
CONFLICT EXAMPLE
IffatAnjum,Lecturer,BRACUniversity
CONFLICT EXAMPLE2
IffatAnjum,Lecturer,BRACUniversity
CONFLICT
IffatAnjum,Lecturer,BRACUniversity
Any Questions ?
IffatAnjum,Lecturer,BRACUniversity

Contenu connexe

Tendances (20)

Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Module 11
Module 11Module 11
Module 11
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Compiler: Syntax Analysis
Compiler: Syntax AnalysisCompiler: Syntax Analysis
Compiler: Syntax Analysis
 
Left factor put
Left factor putLeft factor put
Left factor put
 
Ch4a
Ch4aCh4a
Ch4a
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Nonrecursive predictive parsing
Nonrecursive predictive parsingNonrecursive predictive parsing
Nonrecursive predictive parsing
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
Parsing
ParsingParsing
Parsing
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Parsing
ParsingParsing
Parsing
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 

En vedette

Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Iffat Anjum
 
Fog computing ( foggy cloud)
Fog computing  ( foggy cloud)Fog computing  ( foggy cloud)
Fog computing ( foggy cloud)Iffat Anjum
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxIffat Anjum
 
Cognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationCognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationIffat Anjum
 
Distributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioDistributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioIffat Anjum
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Iffat Anjum
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysisIffat Anjum
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environmentIffat Anjum
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysisIffat Anjum
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generationIffat Anjum
 
code optimization
code optimization code optimization
code optimization Sanjeev Raaz
 
Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerIffat Anjum
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 

En vedette (20)

Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Fog computing ( foggy cloud)
Fog computing  ( foggy cloud)Fog computing  ( foggy cloud)
Fog computing ( foggy cloud)
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptx
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Cognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentationCognitive radio network_MS_defense_presentation
Cognitive radio network_MS_defense_presentation
 
Distributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radioDistributed contention based mac protocol for cognitive radio
Distributed contention based mac protocol for cognitive radio
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
code optimization
code optimization code optimization
code optimization
 
Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to Compiler
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 

Similaire à Bottom-Up Parsing (20)

Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptx
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptx
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Ch06
Ch06Ch06
Ch06
 
Assignment7
Assignment7Assignment7
Assignment7
 
Lecture9 syntax analysis_5
Lecture9 syntax analysis_5Lecture9 syntax analysis_5
Lecture9 syntax analysis_5
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Parsing
ParsingParsing
Parsing
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
Compiler Design Material 2
Compiler Design Material 2Compiler Design Material 2
Compiler Design Material 2
 
PCD ?s(MCA)
PCD ?s(MCA)PCD ?s(MCA)
PCD ?s(MCA)
 
Principles of Compiler Design
Principles of Compiler DesignPrinciples of Compiler Design
Principles of Compiler Design
 
Pcd(Mca)
Pcd(Mca)Pcd(Mca)
Pcd(Mca)
 
Assignment10
Assignment10Assignment10
Assignment10
 
COMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax AnalysisCOMPILER DESIGN- Syntax Analysis
COMPILER DESIGN- Syntax Analysis
 

Plus de Iffat Anjum

Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Iffat Anjum
 
Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Iffat Anjum
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Iffat Anjum
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysisIffat Anjum
 
On qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareOn qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareIffat Anjum
 
Data link control
Data link controlData link control
Data link controlIffat Anjum
 
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...Iffat Anjum
 
Qo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networksQo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networksIffat Anjum
 
A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...Iffat Anjum
 
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...Iffat Anjum
 
Quality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networksQuality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networksIffat Anjum
 
Multicastingand multicast routing protocols
Multicastingand multicast routing protocolsMulticastingand multicast routing protocols
Multicastingand multicast routing protocolsIffat Anjum
 
Fpga(field programmable gate array)
Fpga(field programmable gate array) Fpga(field programmable gate array)
Fpga(field programmable gate array) Iffat Anjum
 
Multicastingand multicast routing protocols
Multicastingand multicast routing protocolsMulticastingand multicast routing protocols
Multicastingand multicast routing protocolsIffat Anjum
 
Motorola microprocessor
Motorola microprocessorMotorola microprocessor
Motorola microprocessorIffat Anjum
 

Plus de Iffat Anjum (16)

Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05Lecture 09 syntax analysis 05
Lecture 09 syntax analysis 05
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
On qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcareOn qo s provisioning in context aware wireless sensor networks for healthcare
On qo s provisioning in context aware wireless sensor networks for healthcare
 
Data link control
Data link controlData link control
Data link control
 
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
 
Qo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networksQo s based mac protocol for medical wireless body area sensor networks
Qo s based mac protocol for medical wireless body area sensor networks
 
A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...A reinforcement learning based routing protocol with qo s support for biomedi...
A reinforcement learning based routing protocol with qo s support for biomedi...
 
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
Data centric multiobjective qo s-aware routing protocol (dm-qos) for body are...
 
Quality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networksQuality of service aware mac protocol for body sensor networks
Quality of service aware mac protocol for body sensor networks
 
Library system
Library systemLibrary system
Library system
 
Multicastingand multicast routing protocols
Multicastingand multicast routing protocolsMulticastingand multicast routing protocols
Multicastingand multicast routing protocols
 
Fpga(field programmable gate array)
Fpga(field programmable gate array) Fpga(field programmable gate array)
Fpga(field programmable gate array)
 
Multicastingand multicast routing protocols
Multicastingand multicast routing protocolsMulticastingand multicast routing protocols
Multicastingand multicast routing protocols
 
Motorola microprocessor
Motorola microprocessorMotorola microprocessor
Motorola microprocessor
 

Dernier

Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 

Dernier (20)

Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 

Bottom-Up Parsing

  • 2. BOTTOM-UP PARSING • A bottom-up parser creates the parse tree of the given input starting from leaves towards the root • A bottom-up parser tries to find the right-most derivation of the given input in the reverse order. S  ...  (the right-most derivation of )  (the bottom-up parser finds the right-most derivation in the reverse order) IffatAnjum,Lecturer,BRACUniversity
  • 4. RIGHTMOST DERIVATION IN REVERSE IffatAnjum,Lecturer,BRACUniversity
  • 5. RIGHTMOST DERIVATION IN REVERSE LR parsing corresponds to rightmost derivation in reverse IffatAnjum,Lecturer,BRACUniversity
  • 6. • A reduction step replaces a specific substring (matching the body of a production) • Reduction is the opposite of derivation • Bottom up parsing is a process of reducing a string  to the start symbol S of the grammar REDUCTION IffatAnjum,Lecturer,BRACUniversity
  • 7. HANDLE • Informally, a handle is a substring (in the parsing string) that matches the right side of a production rule. – But not every substring matches the right side of a production rule is handle • A handle of a right sentential form  ( ) is a production rule A   and a position of  where the string  may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation  . S  A   IffatAnjum,Lecturer,BRACUniversity
  • 8. HANDLE PRUNING • A right-most derivation in reverse can be obtained by handle-pruning. rm rm rm rm rm S=0  1  2  ...  n-1  n=  input string • Start from n, find a handle Ann in n, and replace n in by An to getn-1. • Then find a handle An-1n-1 in n-1, and replace n-1 in by An-1 to getn-2. • Repeat this, until we reach S. n-th right-sentential form IffatAnjum,Lecturer,BRACUniversity
  • 9. SHIFT-REDUCE PARSING • Bottom-up parsing is also known as shift-reduce parsing because its two main actions are shift and reduce. • data structures: input-string and stack • Operations – At each shift action, the current symbol in the input string is pushed to a stack. – At each reduction step, the symbols at the top of the stack (this symbol sequence is the right side of a production) will replaced by the non-terminal at the left side of that production. – Accept: Announce successful completion of parsing – Error: Discover a syntax error and call error recovery IffatAnjum,Lecturer,BRACUniversity
  • 10. SHIFT REDUCE PARSING S  a T R e T  T b c | b R  d Remaining input: abbcde Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e IffatAnjum,Lecturer,BRACUniversity
  • 11. SHIFT REDUCE PARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Remaining input: bcde Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e IffatAnjum,Lecturer,BRACUniversity
  • 12. SHIFT REDUCE PARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b   T Remaining input: bcde Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e IffatAnjum,Lecturer,BRACUniversity
  • 13. SHIFT REDUCE PARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c    T b c Remaining input: de Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e IffatAnjum,Lecturer,BRACUniversity
  • 14. SHIFT REDUCE PARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c     T b c T Remaining input: de Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e IffatAnjum,Lecturer,BRACUniversity
  • 15. SHIFT REDUCE PARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c Shift d      T b c T d Remaining input: e Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e IffatAnjum,Lecturer,BRACUniversity
  • 16. SHIFT REDUCE PARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c Shift d Reduce R  d       T b c T d R Remaining input: e Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e IffatAnjum,Lecturer,BRACUniversity
  • 17. SHIFT REDUCE PARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c Shift d Reduce R  d Shift e        T b c T d R e Remaining input: Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e IffatAnjum,Lecturer,BRACUniversity
  • 18. SHIFT REDUCE PARSING S  a T R e T  T b c | b R  d a b Shift a, Shift b Reduce T  b Shift b, Shift c Reduce T  T b c Shift d Reduce R  d Shift e Reduce S  a T R e         T b c T d R e S Remaining input: Rightmost derivation: S  a T R e  a T d e  a T b c d e  a b b c d e IffatAnjum,Lecturer,BRACUniversity
  • 19. EXAMPLE SHIFT-REDUCE PARSING Consider the grammar: Stack Input Action $ id1 + id2$ shift $id1 + id2$ reduce 6 $F + id2$ reduce 4 $T + id2$ reduce 2 $E + id2$ shift $E + id2$ shift $E + id2 reduce 6 $E + F reduce 4 $E + T reduce 1 $E accept IffatAnjum,Lecturer,BRACUniversity
  • 20. SHIFT-REDUCE PARSING • Handle will always appear on Top of stack, never inside • Possible forms of two successive steps in any rightmost derivation • CASE 1: S A B    y z S * Az  Byz  yz rm rm rm STACK $ INPUT yz$ After Reducing the handle $B yz$ Shifting from Input $By z$ Reduce the handle $A z$ IffatAnjum,Lecturer,BRACUniversity
  • 21. SHIFT-REDUCE PARSING • Case 2: S A y z B   x S * BxAz  Bxyz  xyz rm rm rm STACK $ $B $Bxy $BxA INPUT xyz$ After Reducing the handle xyz$ Shifting from Input z$ Reducing the handle z$ IffatAnjum,Lecturer,BRACUniversity
  • 22. CONFLICTS DURING SHIFT-REDUCE PARSING • There are context-free grammars for which shift-reduce parsers cannot be used. • Stack contents and the next input symbol may not decide action: – shift/reduce conflict: Whether make a shift operation or a reduction. – reduce/reduce conflict: The parser cannot decide which of several reductions to make. • If a shift-reduce parser cannot be used for a grammar, that grammar is called as non-LR(k) grammar. k lookheadleft to right scanning right-most derivation • An ambiguous grammar can never be a LR grammar. IffatAnjum,Lecturer,BRACUniversity
  • 23. SHIFT-REDUCE CONFLICT IN AMBIGUOUS GRAMMAR stmt  if expr then stmt | if expr then stmt else stmt | other STACK INPUT ….if expr then stmt else….$  We can’t decide whether to shift or reduce? IffatAnjum,Lecturer,BRACUniversity
  • 24. REDUCE-REDUCE CONFLICT IN AMBIGUOUS GRAMMAR 1. stmt  id(parameter_list) 2. stmt  expr:=expr 3. parameter_list  parameter_list, parameter 4. parameter_list  parameter 5. parameter_list  id 6. expr  id(expr_list) 7. expr  id 8. expr_list  expr_list, expr 9. expr_list  expr STACK INPUT ….id ( id , id ) …$  We can’t decide which production will be used to reduce id? IffatAnjum,Lecturer,BRACUniversity
  • 25. SHIFT-REDUCE PARSERS There are two main categories of shift-reduce parsers 1. Operator-Precedence Parser – simple, but only a small class of grammars. CFG LR LALR SLR 2. LR-Parsers – covers wide range of 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 different. IffatAnjum,Lecturer,BRACUniversity
  • 26. LR PARSERS LR parsing is attractive because: – LR parsing is most general non-backtracking shift-reduce parsing, yet it is still efficient. – The class of grammars that can be parsed using LR methods is a proper superset of the class of 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 left-to-right scan of the input. – LR parsers can be constructed to recognize virtually all programming language constructs for which CFG grammars canbe written Drawback of LR method: – Too much work to construct LR parser by hand • Fortunately tools (LR parsers generators) are available IffatAnjum,Lecturer,BRACUniversity
  • 27. LL VS. LR  LR (shift reduce) is more powerful than LL (predictive parsing)  Can detect a syntactic error as soon as possible.  LR is difficult to do by hand (unlike LL) IffatAnjum,Lecturer,BRACUniversity
  • 28. LR PARSING ALGORITHM Sm Xm Sm-1 Xm-1 . . S1 X1 S0 a1 ... ai ... an $ Action Table terminals and $ Goto Table non-terminal s t a t e s four different actions s t each item is a a state number t e s LR Parsing Algorithm input stack output IffatAnjum,Lecturer,BRACUniversity
  • 29. A CONFIGURATION OF LR PARSING ALGORITHM • A configuration of a LR parsing is: ( So X1 S1 ... Xm Sm, ai ai+1 ... an $ ) Stack Rest of Input • Sm and ai decides the parser action by consulting the parsing action table. (Initial Stack contains just So ) • A configuration of a LR parsing represents the right sentential form: X1 ... Xm ai ai+1 ... an $ IffatAnjum,Lecturer,BRACUniversity
  • 30. ACTIONS OF A LR-PARSER 1. shift s -- shifts the next input symbol and the state s onto the stack ( So X1 S1 ... Xm Sm, ai ai+1 ... an $ )€ ( So X1 S1 ... Xm Sm ai s, ai+1 ... an $ ) 2. reduce A (or rN where N is a production number) – pop 2|| (=r) items from the stack; – then push Aand s where s=goto[sm-r,A] ( So X1 S1 ... Xm Sm, ai ai+1 ... an $ )€ ( So X1 S1 ... Xm-r Sm-r A s, ai ... an $ ) – Output is the reducing production reduce A 2. Accept – Parsing successfully completed 3. Error -- Parser detected an error (an empty entry in the action table) IffatAnjum,Lecturer,BRACUniversity
  • 31. Reduce Action • pop 2||(=r) items fromthe stack; let us assume that  = Y1Y2...Yr • then push Aand s where s=goto[sm-r,A] ( So X1 S1 ... Xm-r Sm-r Y1 Sm-r ...Yr Sm, ai ai+1 ... an $ ) € ( So X1 S1 ... Xm-r Sm-r A s, ai ... an $ ) • In fact, Y1Y2...Yris a handle. X1 ... Xm-r A ai ... an $ c X1 ... Xm Y1...Yr ai ai+1 ... an $ IffatAnjum,Lecturer,BRACUniversity
  • 34. CONSTRUCTING SLR PARSING TABLES – LR(0) ITEM • An item indicates how much of a production we have seen at a given point in the parsing process • For Example the item A  X  YZ – We have already seen on the input a string derivable from X – We hope to see a string derivable from YZ • For Example the item A   XYZ – We hope to see a string derivable from XYZ • For Example the item A  XYZ  – We have already seen on the input a string derivable from XYZ – It is possibly time to reduce XYZ to A • Special Case: Rule: A   yields only one item A   IffatAnjum,Lecturer,BRACUniversity
  • 35. CONSTRUCTING SLR PARSING TABLES • A collection of sets of LR(0) items (the canonical LR(0) collection) is the basisfor constructing SLR parsers. • Canonical LR(0) collection provides the basis of constructing a DFA called LR(0) automaton – This DFA is used to make parsing decisions • Each state of LR(0) automaton represents a set of items in the canonical LR(0) collection • To construct the canonical LR(0) collection for a grammar – Augmented Grammar – CLOSURE function – GOTO function IffatAnjum,Lecturer,BRACUniversity
  • 36. CONSTRUCTING SLR PARSING TABLES – LR(0) ITEM • An LR(0) item of a grammar G is a production of G a dot at the some position of the right side. • Ex: A  aBb Possible LR(0) Items: A   aBb (four different possibility) A  a  Bb A  aB  b A  aBb  • Sets of LR(0) items will be the states of action and goto table of the SLR parser. – States represent sets of “items” • LR parser makes shift-reduce decision by maintaining states to keep track of where we are in a parsing process IffatAnjum,Lecturer,BRACUniversity
  • 38. THE CLOSURE OPERATION • If I is a set of LR(0) items for a grammar G, then closure(I) is the set of LR(0) items constructed from I by the two rules: 1. Initially, every LR(0) item in I is added to closure(I). 2. If A  .B is in closure(I) and B is a production rule of G; then B. will be in the closure(I). We will apply this rule until no more new LR(0) items can be added to closure(I). IffatAnjum,Lecturer,BRACUniversity
  • 39. THE CLOSURE OPERATION -- EXAMPLE E'  E closure({E'  .E}) = E  E+T { E'  E kernel items E  T E  E+T T  T*F E  T T  F T  T*F F  (E) T  F F  id F  (E) F  id } IffatAnjum,Lecturer,BRACUniversity
  • 40. GOTO OPERATION • If I is a set of LR(0) items and X is a grammar symbol (terminal or non-terminal), then GOTO(I,X) is defined as follows: – If A   X in I then every item in closure({A  X  }) willbe in GOTO(I,X). Example: E   T,I ={ E’   E, E   E+T, T   T*F,T   F, F   (E), F   id }  T, T   T*F, T   F, GOTO(I,E) = { E’  E  , E  E  +T } GOTO(I,T) = { E  T  , T  T  *F } GOTO(I,F) = {T  F  } GOTO(I,() = { F  ( E), E   E+T, E  F   (E), F   id } GOTO(I,id) = { F  id  } IffatAnjum,Lecturer,BRACUniversity
  • 41. CONSTRUCTION OF THE CANONICAL LR(0) COLLECTION (CC) • To create the SLR parsing tables for a grammar G, we will create the canonical LR(0) collection of the grammar G’. • Algorithm: C is { closure({S'  S}) } repeat the followings until no more set of 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 function is a DFA on the sets in C. IffatAnjum,Lecturer,BRACUniversity
  • 42. THE CANONICAL LR(0) COLLECTION -- EXAMPLE I0: E’  .E E  .E+T I1: E’  E. E  E.+T I6: E  E+.T T  .T*F I9: E  E+T. T  T.*F E  .T T  .F T  .T*F T  .F I2: E  T. T  T.*F F  .(E) F  .id I10: T  T*F. F  .(E) F  .id I3: T  F. I7: T  T*.F F  .(E) I11: F  (E). I4: F  (.E) E  .E+T F  .id E  .T T  .T*F I8: F  (E.) E  E.+T T  .F F  .(E) F  .id I5: F  id. IffatAnjum,Lecturer,BRACUniversity
  • 43. TRANSITION DIAGRAM (DFA) OF GOTO FUNCTION I0 I1 I2 I3 I6 I7 I8 to I2 to I3 to I4 I9 to I3 I10 to I4 to I5 I11 to I6 to I7 id to I4 to I5 ( F * E E T T ) + F T F ( I4 id I5 id ( * F ( id + IffatAnjum,Lecturer,BRACUniversity
  • 44. CONSTRUCTING SLR PARSING TABLE (OF AN AUGUMENTED GRAMMAR G’) 1. Construct the canonical collection of sets of LR(0) items for G’. C{I0,...,In} 2. Create the parsing action table as follows:  If a is a terminal, A.a in Ii and goto(Ii,a)=Ij then action[i,a] is shift j.  If A. is in Ii , then action[i,a] is reduce A for all a in FOLLOW(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 SLR(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 containsS’.S IffatAnjum,Lecturer,BRACUniversity
  • 45. PARSING TABLES OF EXPRESSION GRAMMAR state id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 Action Table Goto Table IffatAnjum,Lecturer,BRACUniversity
  • 46. (SLR) PARSING TABLES FOR EXPRESSION GRAMMAR state id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5 Action Table Goto Table Key to Notation S4=“Shift input symbol and push state 4” R5= “Reduce by rule 5” Acc=Accept (blank)=Syntax Error IffatAnjum,Lecturer,BRACUniversity
  • 47. EXAMPLE LR PARSE: (ID+ID)*ID IffatAnjum,Lecturer,BRACUniversity
  • 48. EXAMPLE LR PARSE: (ID+ID)*ID IffatAnjum,Lecturer,BRACUniversity
  • 49. EXAMPLE LR PARSE: (ID+ID)*ID IffatAnjum,Lecturer,BRACUniversity
  • 50. ACTIONS OF A (S)LR-PARSER -- EXAMPLE stack 0 input id*id+id$ action shift 5 output 0id5 *id+id$ reduce by Fid Fid 0F3 0T2 0T2*7 *id+id$ *id+id $ id+id $ reduce by TF shift 7 shift 5 TF 0T2*7id5 +id$ reduce by Fid Fid 0T2*7F10 +id$ reduce by TT*F TT*F 0T2 0E1 0E1+6 0E1+6id5 +id$ +i d$ id$ $ reduce by ET shift 6 shift 5 reduce by Fid ET Fid 0E1+6F3 $ reduce by TF TF 0E1+6T9 0E1 $ $ reduce by EE+T accept EE+ T IffatAnjum,Lecturer,BRACUniversity
  • 52. SLR GRAMMAR: REVIEW  An LR parser using SLR parsing tables for a grammar G is called as the SLR parser for G.  If a grammar G has an SLR parsing table, it is called SLR grammar (or SLR grammar in short).  Every SLR grammar is unambiguous, but every unambiguous grammar is not a SLR grammar.  If the SLR parsing table of a grammar G has a conflict, we say that that grammar is not SLR grammar. IffatAnjum,Lecturer,BRACUniversity