SlideShare a Scribd company logo
1 of 62
Download to read offline
Bottom up parsing
Bottom-Up Parsing
 Bottom-up parsing is more general than top- down.
 A bottom-up parser builds a derivation by working
from the input sentence back toward the start symbol S
 Preferred method in practice
 Also called LR parsing
L means that tokens are read left to right
R means that it constructs a rightmost derivation
Bottom-up Parsing
• Two types of bottom-up parsing:
• Operator-Precedence parsing
• LR parsing
• covers wide range of grammars.
• SLR – simple LR parser
• LR – most general LR parser
• LALR – intermediate LR parser (Lookahead LR
parser)
Bottom-Up Parsing
LR parsing reduces a string to the start
symbol by inverting productions:
Str input string of terminals
repeat
Identify β in str such that A →β is a
production
Replace β by A in str
until str = S (the start symbol) OR all
possibilities are exhausted
Bottom-Up Parsing
 int + (int) + (int)
 E + (int) + (int)
 E + (E) + (int)
 E + (int)
 E + (E)
 E
E → E + ( E ) | int
A rightmost derivation
in reverse
Reductions
Bottom-up parsing as the process of
"reducing" a string w to the start symbol
of the grammar.
At each reduction step, a substring that
matches the body of a production is
replaced by the non-terminal at the head
of that production.
Handle
Handle of a string: Substring that matches the
RHS of some production AND whose reduction
to the non-terminal on the LHS is a step along
the reverse of some rightmost derivation.
Handles
 Formally:
Handle of a right sentential form  is <A  ,
location of  in >
i.e. A   is a handle of  at the location
immediately after the end of , if:S => A => 
 A certain sentential form may have many different
handles.
 Right sentential forms of a non-ambiguous grammar
have one unique handle
Example
S  aABe  aAde  aAbcde  abbcde
S  aABe
A  Abc | b
B  d
S  aABe is a handle of aABe in location 1.
B  d is a handle of aAde in location 3.
A  Abc is a handle of aAbcde in location 2.
A  b is a handle of abbcde in location 2.
Handle Pruning
 A rightmost derivation in reverse can be obtained by “handle-pruning.”
abbcde
Find the handle = b at loc. 2
aAbcde
b at loc. 3 is not a handle:
aAAcde
... blocked.
Handle-pruning
 The process of discovering a handle & reducing it to the appropriate left-hand side is
called handle pruning.
 Handle pruning forms the basis for a bottom-up parsing method.
To construct a rightmost derivation
S = 0  1  2  …  n-1  n = w
Apply the following simple algorithm
for i  n to 1 by -1
Find the handle Ai i in i
Replace i with Ai to generate i-1
1. S -> 0 S1|01 indicate the handle in each of the following right-sentential forms:
1. 000111
2. 00S11
2. For the grammar S -> S S + | S S * | a indicate the handle in each of the following
right-sentential forms:
1. SSS + a * +
2. SS + a * a+
3. aaa * a + +.
3. Give bottom-up parses for the following input strings
1. The input 000111 according to the grammar of
Exercise 1
2. The input aaa * a + + according to the grammar of 2
Shift Reduce Parsing with a
Stack
Two problems:
 locate a handle
 decide which production to use (if there are
more than two candidate productions).
Shift-reduce Parsing
A shift-reduce parser is a stack automaton with four actions
 Shift — next word is shifted onto the stack
 Reduce — right end of handle is at top of stack
Locate left end of handle within the stack
Pop handle off stack & push appropriate lhs
 Accept — stop parsing & report success
 Error — call an error reporting/recovery routine
Accept & Error are simple
Shift is just a push and a call to the scanner
Reduce takes |rhs| pops & 1 push
x - 2 * y
Stack Input Handle Action
$ id - num * id None shift
$ id - num * id
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
0 Goal  Expr
1 Expr  Expr + Term
2 | Expr - Term
3 | Term
4 Term  Term * Factor
5 | Term / Factor
6 | Factor
7 Factor  number
8 | id
9 | ( Expr )
Back to x - 2 * y
Stack Input Handle Action
$ id - num * id none shift
$ id - num * id 8,1 reduce 8
$ Factor - num * id 6,1 reduce 6
$ Term - num * id 3,1 reduce 4
$ Expr - num * id
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
0 Goal  Expr
1 Expr  Expr + Term
2 | Expr - Term
3 | Term
4 Term  Term * Factor
5 | Term / Factor
6 | Factor
7 Factor  number
8 | id
9 | ( Expr )
Back to x - 2 * y
Stack Input Handle Action
$ id - num * id none shift
$ id - num * id 8,1 reduce 8
$ Factor - num * id 6,1 reduce 6
$ Term - num * id 3,1 reduce 4
$ Expr - num * id
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
0 Goal  Expr
1 Expr  Expr + Term
2 | Expr - Term
3 | Term
4 Term  Term * Factor
5 | Term / Factor
6 | Factor
7 Factor  number
8 | id
9 | ( Expr )
Expr is not a handle at this point because it does not
occur at this point in the derivation.
Back to x - 2 * y
Stack Input Handle Action
$ id - num * id none shift
$ id - num * id 8,1 reduce 8
$ Factor - num * id 6,1 reduce 6
$ Term - num * id 3,1 reduce 3
$ Expr - num * id none shift
$ Expr - num * id none shift
$ Expr - num * id
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
0 Goal  Expr
1 Expr  Expr + Term
2 | Expr - Term
3 | Term
4 Term  Term * Factor
5 | Term / Factor
6 | Factor
7 Factor  number
8 | id
9 | ( Expr )
Back to x - 2 * y
Stack Input Handle Action
$ id - num * id none shift
$ id - num * id 8,1 reduce 8
$ Factor - num * id 6,1 reduce 6
$ Term - num * id 3,1 reduce 3
$ Expr - num * id none shift
$ Expr - num * id none shift
$ Expr - num * id 7,3 reduce 7
$ Expr - Factor * id 6,3 reduce 6
$ Expr - Term * id
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
0 Goal  Expr
1 Expr  Expr + Term
2 | Expr - Term
3 | Term
4 Term  Term * Factor
5 | Term / Factor
6 | Factor
7 Factor  number
8 | id
9 | ( Expr )
Back to x - 2 * y
Stack Input Handle Action
$ id - num * id none shift
$ id - num * id 8,1 reduce 8
$ Factor - num * id 6,1 reduce 6
$ Term - num * id 3,1 reduce 3
$ Expr - num * id none shift
$ Expr - num * id none shift
$ Expr - num * id 7,3 reduce 7
$ Expr - Factor * id 6,3 reduce 6
$ Expr - Term * id none shift
$ Expr - Term * id none shift
$ Expr - Term * id
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
0 Goal  Expr
1 Expr  Expr + Term
2 | Expr - Term
3 | Term
4 Term  Term * Factor
5 | Term / Factor
6 | Factor
7 Factor  number
8 | id
9 | ( Expr )
Back to x - 2 * y
5 shifts +
9 reduces +
1 accept
Stack Input Handle Action
$ id - num * id none shift
$ id - num * id 8,1 reduce 8
$ Factor - num * id 6,1 reduce 6
$ Term - num * id 3,1 reduce 3
$ Expr - num * id none shift
$ Expr - num * id none shift
$ Expr - num * id 7,3 reduce 7
$ Expr - Factor * id 6,3 reduce 6
$ Expr - Term * id none shift
$ Expr - Term * id none shift
$ Expr - Term * id 8,5 reduce 8
$ Expr - Term * Factor 4,5 reduce 4
$ Expr - Term 2,3 reduce 2
$ Expr 0,1 reduce 0
$ Goal none accept
1. Shift until the top of the stack is the right end of a handle
2. Find the left end of the handle and reduce
0 Goal  Expr
1 Expr  Expr + Term
2 | Expr - Term
3 | Term
4 Term  Term * Factor
5 | Term / Factor
6 | Factor
7 Factor  number
8 | id
9 | ( Expr )
Goal
<id,x>
Term
Fact.
Expr –
Expr
<id,y>
<num,2>
Fact.
Fact.Term
Term
*
Stack Input Action
$ id - num * id shift
$ id - num * id reduce 8
$ Factor - num * id reduce 6
$ Term - num * id reduce 3
$ Expr - num * id shift
$ Expr - num * id shift
$ Expr - num * id reduce 7
$ Expr - Factor * id reduce 6
$ Expr - Term * id shift
$ Expr - Term * id shift
$ Expr - Term * id reduce 8
$ Expr - Term * Factor reduce 4
$ Expr - Term reduce 2
$ Expr reduce 0
$ Goal accept
Back to x - 2 * y
Corresponding Parse Tree
Conflicts During Shift-Reduce Parsing
Conflicts
“shift/reduce” or “reduce/reduce”
Example:
stmt  if expr then stmt
| if expr then stmt else stmt
| other (any other statement)
Stack Input
if … then stmt else … Shift/ Reduce Conflict
We can’t tell
whether it is a
handle
LR Parsing
Bottom-up parser based on a concept called
LR(k) parsing
"L" is for left-to-right scanning of the input.
"R" for constructing a rightmost derivation in
reverse,
“k” for the number of input symbols of
lookahead that are used in making parsing
decisions.
Why LR Parsers?
For a grammar to be LR it is sufficient that a
left-to-right shift-reduce parser be able to
recognize handles of right-sentential forms
when they appear on top of the stack.
Why LR Parsers?
 LR parsers can be constructed to recognize all
programming language constructs for which
context-free grammars can be written.
 The LR-parsing method is the most general non-
back-tracking shift-reduce parsing method.
 An LR parser can detect a syntactic errors.
 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 or LL
methods.
Components of LR Parser
X
Y
Z
$
A + B $
Parsing Program
Parse Table
Action Goto
output
stack
Input buffer
Techniques for Creating Parse Table
 SLR: Construct parsing table for small set of grammars called
SLR(1).
 Easy to develop.
 CLR(CANONICAL LR) : Most powerful.
Generates a large parse table.
More difficult develop.
Works for all types of CFG
May detect position of error.
 LALR(LOOK AHEAD LR) :Widely used method.
 Optimizes the size of parse table, by combining some states.
 Information may get lost.
SLR Parsers
1. Formation of augmented grammar G’ for
the given grammar G
2. Construction of LR(0) collection of
items.
To find LR(0) collection of items Closure(I)
and Goto(I,X) have to be computed.
3. Finding first and follow of non-terminals
4. Construction of parse table.
Formation of Augmented
Grammar
 The augmented grammar G’, is G with a new start
symbol S’ and an additional production S’ -> S
E->E+T|T
T->T*F|F
 The augmented grammar G’ is given by
E’->E
E->E+T|T
T->T*F|F
Items and the LR(O)
Automaton
How does a shift-reduce parser know when
to shift and when to reduce?
How does the parser know that symbol on the
top of the stack is not a handle?
An LR parser makes shift-reduce decisions
by maintaining states to keep track of all the
operations.
States represent sets of "items."
Items and the LR(O)
Automaton
 An LR(O) item of a grammar G is a production of G, with a
dot at some position in the body.
 Production A -> XYZ yields the four items
A -> ·XYZ
A -> X·YZ
A -> XY·Z
A -> XYZ·
 A ->ε generates only one item, A ->.
 An item indicates how much of a production we have seen
at a given point in the parsing process.
Items and the LR(O)
Automaton
 The item A -> ·XYZ indicates that we hope to see a string derivable from XYZ next
on the input.
 Item A -> X·YZ indicates that we have just seen on the input a string derivable from
X and that we hope next to see a string derivable from YZ.
 Item A -> XY·Z indicates that we have just seen on the input a string derivable from
XY and that we hope next to see a string derivable from Z.
 Item A -> XYZ· indicates that we have seen the body XY Z and that it may be time
to reduce XYZ to A
Items and the LR(O)
Automaton
 Collection of sets of LR(0) items, called the
canonical LR(0) collection.
 Provides the basis for constructing a deterministic
finite automaton that is used to make parsing
decisions.
 Automaton is called an LR(0) automaton.
 Each state of the LR(0) automaton represents a
set of items in the canonical LR(0) collection.
Construction of LR(0) Items
Items are viewed as states in NFA.
Grouped to form same states.
Process of grouping together is called subset
Construction Algorithm.
Closure and goto operations have to be
computed.
Closure of Item Sets
 If I is a set of items for a grammar G, then CLOSURE(I)
is the set of items constructed from I by the two rules:
Initially, add every item in I to CLOSURE(I).
If A -> α·Bγ is in CLOSURE(I) and B -> γ is a
production, then add the item B -> γ to CLOSURE(I),
if it is not already there. Apply this rule until no more
new items can be added to CLOSURE (I).
Computation of CLOSURE
E’  E
E  E + T | T
T  T * F | F
F  ( E ) | id
I0
E’  .E
E  .E + T
E  .T
T  .T * F
T .F
F  .( E )
F  .id
Computation of CLOSURE
SetOfltems CLOSURE (I) {
J = I;
repeat
for ( each item A -> a·Bγ in J )
for ( each production B ->γ of G )
if (B ->.γ is not in J )
add B ->.γ to J;
until no more items are added to J on one round;
return J;
}
 Kernel items : The initial item, S' ->·S, and all items whose dots are not at
the left end.
 Non-kernel items : All items with their dots at the left end, except for S' ->
·S.
The Function GOTO
 GOTO (I, X) is defined to be the closure of the set of all items [A -> αX.β] such that
[A -> α.Xβ] is in I.
 If I is the set of two items { [E' -> E·] , [E -> E· + T] } , then
 GOTO(I, +) contains the items
 E -> E + ·T
 T -> ·T * F
 T -> ·F
 F -> · (E)
 F -> ·id
The Function GOTO
Grammar
S  E + S | E
E  num
E  num .
S’  S . $
num
ES’  . S $
S  .E + S
S  . E
E  .num
1
S  E . +S
S  E .
2
S  E + S .
5
S  E + . S
S  . E + S
S  . E
E  . num
3
S’  S $ .
7
4
S
S
$
+
E
num
num + $ E S
1 s4 g2 g6
2 s3 SE
3 s4 g2 g5
4 Enum Enum
5 SE+S
6 s7
7 accept
S' S
S   L=R
S   R
L   *R
L   id
R   L
id
S' S
S   L=R
S   R
L   *R
L   id
R   L
L  id 
S  L =R
R  L 
S' S 
I0
I1
I2
I3
S  R I4
L  * R
R   L
L   id
L   * R
I5
S
L
*
id R
S  L=  R
R   L
L   *R
L   id
I6
=
R
S  L=R 
R  L 
L
L
I7
id
I3
*
*
L  *R 
R
I8
I9
state action goto
id = * $ S L R
0 s3 s5 1 2 4
1 accept
2 s6/r(RL)
3 r(Lid) r(Lid)
4 r(SR)
5 s3 s5 7 8
6 s3 s5 7 9
7 r(RL) r(RL)
8 r(L*R) r(L*R)
9 r(SL=R)
Structure of the LR Parsing
Table
1. The ACTION function takes as arguments a state i and a terminal
a (or $, the input endmarker). The value of ACTION[i, a] can have
one of four forms:
a) Shift j , where j is a state. The action taken by the parser shifts
input a to the stack, but uses state j to represent a .
b) Reduce A ->β. The action of the parser reduces β on the top of
the stack to head A.
c) Accept. The parser accepts the input and finishes parsing;
d) Error. The parser discovers an error in its input and takes some
corrective action.
2. Extend the GOTO function, defined on sets of items, to states: if
GOTo [Ii , A] = Ij , then GOTO also maps a state i and a
nonterminal A to state j .
Construction of SLR Parse
Table
1. Construct C={I0,I1…….In} the collection sets of LR(0)
items for G’.
2. Initial state of the parser is constructed from the set of items
for [S’->S]
3. State I is constructed from Ii. The parsing actions are
determined as follows.
1. If [A->α.aβ] is in Ii and GOTO(Ii,a]=Ij, then ACTION[i,a]
is set to ‘shift j’ here ‘a’ must be a terminal.
2. If[A->α.] is in Ii, then ACTION[i,a] is set to reduce A->a
for all ‘a’ Follow(A).
3. If S’->S is in Ii, than action[i, $] is set to ‘accept’.
Construction of SLR parsing
table
4. GOTO transitions are constructed for all non-terminals. If GOTO(Ii,A) =Ij, then
goto(i,A)of the parse table is set to j.
5. All other entries are error entries.
LR-Parser Configurations
 Helps to have complete state o its stack and the remaining
input. A configuration of an LR parser is a pair:(s0s1
………… sm, aiai+1…………an$).
 where the first component is the stack contents and the
second component is the remaining input.
Behavior of the LR Parser
LR-parsing algorithm.
 INPUT: An input string w and an LR-parsing table with functions ACTION
and GOTO for a grammar G.
 OUTPUT: If w is in L ( G), the reduction steps of a bottom-up parse for W ;
otherwise, an error indication.
 METHOD: Initially, the parser has So on its stack, where So is the initial
state, and w$ in the input buffer.
LR-Parser Configurations
Bottom up parser
Bottom up parser
Bottom up parser
Bottom up parser
Bottom up parser
Bottom up parser
Bottom up parser

More Related Content

What's hot (20)

Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Time space trade off
Time space trade offTime space trade off
Time space trade off
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 

Similar to Bottom up parser

ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTFutureTechnologies3
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingR Islam
 
LR parsing
LR parsingLR parsing
LR parsingichikaz3
 
lr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxlr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxsrilakshmis17
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12Rabia Khalid
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bisonvip_du
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sortKrish_ver2
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Iffat Anjum
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisaujihisa
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaDaniel Cukier
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.pptBapanKar2
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxGeetanjaliSingh82
 
Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptxRamBhardwaj11
 

Similar to Bottom up parser (20)

ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPTch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
ch5-bottomupparser_jfdrhgfrfyyssf-gfrrt.PPT
 
Assignment7
Assignment7Assignment7
Assignment7
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
LR parsing
LR parsingLR parsing
LR parsing
 
lr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptxlr parsers bottom up parsers slr parser.pptx
lr parsers bottom up parsers slr parser.pptx
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
 
Introduction of bison
Introduction of bisonIntroduction of bison
Introduction of bison
 
3.5 merge sort
3.5 merge sort3.5 merge sort
3.5 merge sort
 
Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Ch06
Ch06Ch06
Ch06
 
Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4Lecture 07 08 syntax analysis-4
Lecture 07 08 syntax analysis-4
 
Compiler Design Unit 2
Compiler Design Unit 2Compiler Design Unit 2
Compiler Design Unit 2
 
Recursion.pptx
Recursion.pptxRecursion.pptx
Recursion.pptx
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
LR-Parsing.ppt
LR-Parsing.pptLR-Parsing.ppt
LR-Parsing.ppt
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptx
 
Counting trees.pptx
Counting trees.pptxCounting trees.pptx
Counting trees.pptx
 
Slr parser
Slr parserSlr parser
Slr parser
 
Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptx
 

More from Akshaya Arunan

Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined NetworkTraffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined NetworkAkshaya Arunan
 
Enhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDNEnhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDNAkshaya Arunan
 
OpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingOpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingAkshaya Arunan
 
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSISA TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSISAkshaya Arunan
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationAkshaya Arunan
 

More from Akshaya Arunan (7)

Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined NetworkTraffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
 
Enhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDNEnhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDN
 
Akshayappt
AkshayapptAkshayappt
Akshayappt
 
OpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingOpenSec Policy-Based Security Using
OpenSec Policy-Based Security Using
 
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSISA TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 

Recently uploaded (20)

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 

Bottom up parser

  • 2. Bottom-Up Parsing  Bottom-up parsing is more general than top- down.  A bottom-up parser builds a derivation by working from the input sentence back toward the start symbol S  Preferred method in practice  Also called LR parsing L means that tokens are read left to right R means that it constructs a rightmost derivation
  • 3. Bottom-up Parsing • Two types of bottom-up parsing: • Operator-Precedence parsing • LR parsing • covers wide range of grammars. • SLR – simple LR parser • LR – most general LR parser • LALR – intermediate LR parser (Lookahead LR parser)
  • 4. Bottom-Up Parsing LR parsing reduces a string to the start symbol by inverting productions: Str input string of terminals repeat Identify β in str such that A →β is a production Replace β by A in str until str = S (the start symbol) OR all possibilities are exhausted
  • 5. Bottom-Up Parsing  int + (int) + (int)  E + (int) + (int)  E + (E) + (int)  E + (int)  E + (E)  E E → E + ( E ) | int A rightmost derivation in reverse
  • 6.
  • 7. Reductions Bottom-up parsing as the process of "reducing" a string w to the start symbol of the grammar. At each reduction step, a substring that matches the body of a production is replaced by the non-terminal at the head of that production.
  • 8. Handle Handle of a string: Substring that matches the RHS of some production AND whose reduction to the non-terminal on the LHS is a step along the reverse of some rightmost derivation.
  • 9. Handles  Formally: Handle of a right sentential form  is <A  , location of  in > i.e. A   is a handle of  at the location immediately after the end of , if:S => A =>   A certain sentential form may have many different handles.  Right sentential forms of a non-ambiguous grammar have one unique handle
  • 10. Example S  aABe  aAde  aAbcde  abbcde S  aABe A  Abc | b B  d S  aABe is a handle of aABe in location 1. B  d is a handle of aAde in location 3. A  Abc is a handle of aAbcde in location 2. A  b is a handle of abbcde in location 2.
  • 11. Handle Pruning  A rightmost derivation in reverse can be obtained by “handle-pruning.” abbcde Find the handle = b at loc. 2 aAbcde b at loc. 3 is not a handle: aAAcde ... blocked.
  • 12. Handle-pruning  The process of discovering a handle & reducing it to the appropriate left-hand side is called handle pruning.  Handle pruning forms the basis for a bottom-up parsing method. To construct a rightmost derivation S = 0  1  2  …  n-1  n = w Apply the following simple algorithm for i  n to 1 by -1 Find the handle Ai i in i Replace i with Ai to generate i-1
  • 13. 1. S -> 0 S1|01 indicate the handle in each of the following right-sentential forms: 1. 000111 2. 00S11 2. For the grammar S -> S S + | S S * | a indicate the handle in each of the following right-sentential forms: 1. SSS + a * + 2. SS + a * a+ 3. aaa * a + +. 3. Give bottom-up parses for the following input strings 1. The input 000111 according to the grammar of Exercise 1 2. The input aaa * a + + according to the grammar of 2
  • 14. Shift Reduce Parsing with a Stack Two problems:  locate a handle  decide which production to use (if there are more than two candidate productions).
  • 15. Shift-reduce Parsing A shift-reduce parser is a stack automaton with four actions  Shift — next word is shifted onto the stack  Reduce — right end of handle is at top of stack Locate left end of handle within the stack Pop handle off stack & push appropriate lhs  Accept — stop parsing & report success  Error — call an error reporting/recovery routine Accept & Error are simple Shift is just a push and a call to the scanner Reduce takes |rhs| pops & 1 push
  • 16. x - 2 * y Stack Input Handle Action $ id - num * id None shift $ id - num * id 1. Shift until the top of the stack is the right end of a handle 2. Find the left end of the handle and reduce 0 Goal  Expr 1 Expr  Expr + Term 2 | Expr - Term 3 | Term 4 Term  Term * Factor 5 | Term / Factor 6 | Factor 7 Factor  number 8 | id 9 | ( Expr )
  • 17. Back to x - 2 * y Stack Input Handle Action $ id - num * id none shift $ id - num * id 8,1 reduce 8 $ Factor - num * id 6,1 reduce 6 $ Term - num * id 3,1 reduce 4 $ Expr - num * id 1. Shift until the top of the stack is the right end of a handle 2. Find the left end of the handle and reduce 0 Goal  Expr 1 Expr  Expr + Term 2 | Expr - Term 3 | Term 4 Term  Term * Factor 5 | Term / Factor 6 | Factor 7 Factor  number 8 | id 9 | ( Expr )
  • 18. Back to x - 2 * y Stack Input Handle Action $ id - num * id none shift $ id - num * id 8,1 reduce 8 $ Factor - num * id 6,1 reduce 6 $ Term - num * id 3,1 reduce 4 $ Expr - num * id 1. Shift until the top of the stack is the right end of a handle 2. Find the left end of the handle and reduce 0 Goal  Expr 1 Expr  Expr + Term 2 | Expr - Term 3 | Term 4 Term  Term * Factor 5 | Term / Factor 6 | Factor 7 Factor  number 8 | id 9 | ( Expr ) Expr is not a handle at this point because it does not occur at this point in the derivation.
  • 19. Back to x - 2 * y Stack Input Handle Action $ id - num * id none shift $ id - num * id 8,1 reduce 8 $ Factor - num * id 6,1 reduce 6 $ Term - num * id 3,1 reduce 3 $ Expr - num * id none shift $ Expr - num * id none shift $ Expr - num * id 1. Shift until the top of the stack is the right end of a handle 2. Find the left end of the handle and reduce 0 Goal  Expr 1 Expr  Expr + Term 2 | Expr - Term 3 | Term 4 Term  Term * Factor 5 | Term / Factor 6 | Factor 7 Factor  number 8 | id 9 | ( Expr )
  • 20. Back to x - 2 * y Stack Input Handle Action $ id - num * id none shift $ id - num * id 8,1 reduce 8 $ Factor - num * id 6,1 reduce 6 $ Term - num * id 3,1 reduce 3 $ Expr - num * id none shift $ Expr - num * id none shift $ Expr - num * id 7,3 reduce 7 $ Expr - Factor * id 6,3 reduce 6 $ Expr - Term * id 1. Shift until the top of the stack is the right end of a handle 2. Find the left end of the handle and reduce 0 Goal  Expr 1 Expr  Expr + Term 2 | Expr - Term 3 | Term 4 Term  Term * Factor 5 | Term / Factor 6 | Factor 7 Factor  number 8 | id 9 | ( Expr )
  • 21. Back to x - 2 * y Stack Input Handle Action $ id - num * id none shift $ id - num * id 8,1 reduce 8 $ Factor - num * id 6,1 reduce 6 $ Term - num * id 3,1 reduce 3 $ Expr - num * id none shift $ Expr - num * id none shift $ Expr - num * id 7,3 reduce 7 $ Expr - Factor * id 6,3 reduce 6 $ Expr - Term * id none shift $ Expr - Term * id none shift $ Expr - Term * id 1. Shift until the top of the stack is the right end of a handle 2. Find the left end of the handle and reduce 0 Goal  Expr 1 Expr  Expr + Term 2 | Expr - Term 3 | Term 4 Term  Term * Factor 5 | Term / Factor 6 | Factor 7 Factor  number 8 | id 9 | ( Expr )
  • 22. Back to x - 2 * y 5 shifts + 9 reduces + 1 accept Stack Input Handle Action $ id - num * id none shift $ id - num * id 8,1 reduce 8 $ Factor - num * id 6,1 reduce 6 $ Term - num * id 3,1 reduce 3 $ Expr - num * id none shift $ Expr - num * id none shift $ Expr - num * id 7,3 reduce 7 $ Expr - Factor * id 6,3 reduce 6 $ Expr - Term * id none shift $ Expr - Term * id none shift $ Expr - Term * id 8,5 reduce 8 $ Expr - Term * Factor 4,5 reduce 4 $ Expr - Term 2,3 reduce 2 $ Expr 0,1 reduce 0 $ Goal none accept 1. Shift until the top of the stack is the right end of a handle 2. Find the left end of the handle and reduce 0 Goal  Expr 1 Expr  Expr + Term 2 | Expr - Term 3 | Term 4 Term  Term * Factor 5 | Term / Factor 6 | Factor 7 Factor  number 8 | id 9 | ( Expr )
  • 23. Goal <id,x> Term Fact. Expr – Expr <id,y> <num,2> Fact. Fact.Term Term * Stack Input Action $ id - num * id shift $ id - num * id reduce 8 $ Factor - num * id reduce 6 $ Term - num * id reduce 3 $ Expr - num * id shift $ Expr - num * id shift $ Expr - num * id reduce 7 $ Expr - Factor * id reduce 6 $ Expr - Term * id shift $ Expr - Term * id shift $ Expr - Term * id reduce 8 $ Expr - Term * Factor reduce 4 $ Expr - Term reduce 2 $ Expr reduce 0 $ Goal accept Back to x - 2 * y Corresponding Parse Tree
  • 24. Conflicts During Shift-Reduce Parsing Conflicts “shift/reduce” or “reduce/reduce” Example: stmt  if expr then stmt | if expr then stmt else stmt | other (any other statement) Stack Input if … then stmt else … Shift/ Reduce Conflict We can’t tell whether it is a handle
  • 25. LR Parsing Bottom-up parser based on a concept called LR(k) parsing "L" is for left-to-right scanning of the input. "R" for constructing a rightmost derivation in reverse, “k” for the number of input symbols of lookahead that are used in making parsing decisions.
  • 26. Why LR Parsers? For a grammar to be LR it is sufficient that a left-to-right shift-reduce parser be able to recognize handles of right-sentential forms when they appear on top of the stack.
  • 27. Why LR Parsers?  LR parsers can be constructed to recognize all programming language constructs for which context-free grammars can be written.  The LR-parsing method is the most general non- back-tracking shift-reduce parsing method.  An LR parser can detect a syntactic errors.  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 or LL methods.
  • 28. Components of LR Parser X Y Z $ A + B $ Parsing Program Parse Table Action Goto output stack Input buffer
  • 29. Techniques for Creating Parse Table  SLR: Construct parsing table for small set of grammars called SLR(1).  Easy to develop.  CLR(CANONICAL LR) : Most powerful. Generates a large parse table. More difficult develop. Works for all types of CFG May detect position of error.  LALR(LOOK AHEAD LR) :Widely used method.  Optimizes the size of parse table, by combining some states.  Information may get lost.
  • 30. SLR Parsers 1. Formation of augmented grammar G’ for the given grammar G 2. Construction of LR(0) collection of items. To find LR(0) collection of items Closure(I) and Goto(I,X) have to be computed. 3. Finding first and follow of non-terminals 4. Construction of parse table.
  • 31. Formation of Augmented Grammar  The augmented grammar G’, is G with a new start symbol S’ and an additional production S’ -> S E->E+T|T T->T*F|F  The augmented grammar G’ is given by E’->E E->E+T|T T->T*F|F
  • 32. Items and the LR(O) Automaton How does a shift-reduce parser know when to shift and when to reduce? How does the parser know that symbol on the top of the stack is not a handle? An LR parser makes shift-reduce decisions by maintaining states to keep track of all the operations. States represent sets of "items."
  • 33. Items and the LR(O) Automaton  An LR(O) item of a grammar G is a production of G, with a dot at some position in the body.  Production A -> XYZ yields the four items A -> ·XYZ A -> X·YZ A -> XY·Z A -> XYZ·  A ->ε generates only one item, A ->.  An item indicates how much of a production we have seen at a given point in the parsing process.
  • 34. Items and the LR(O) Automaton  The item A -> ·XYZ indicates that we hope to see a string derivable from XYZ next on the input.  Item A -> X·YZ indicates that we have just seen on the input a string derivable from X and that we hope next to see a string derivable from YZ.  Item A -> XY·Z indicates that we have just seen on the input a string derivable from XY and that we hope next to see a string derivable from Z.  Item A -> XYZ· indicates that we have seen the body XY Z and that it may be time to reduce XYZ to A
  • 35. Items and the LR(O) Automaton  Collection of sets of LR(0) items, called the canonical LR(0) collection.  Provides the basis for constructing a deterministic finite automaton that is used to make parsing decisions.  Automaton is called an LR(0) automaton.  Each state of the LR(0) automaton represents a set of items in the canonical LR(0) collection.
  • 36. Construction of LR(0) Items Items are viewed as states in NFA. Grouped to form same states. Process of grouping together is called subset Construction Algorithm. Closure and goto operations have to be computed.
  • 37. Closure of Item Sets  If I is a set of items for a grammar G, then CLOSURE(I) is the set of items constructed from I by the two rules: Initially, add every item in I to CLOSURE(I). If A -> α·Bγ is in CLOSURE(I) and B -> γ is a production, then add the item B -> γ to CLOSURE(I), if it is not already there. Apply this rule until no more new items can be added to CLOSURE (I).
  • 38. Computation of CLOSURE E’  E E  E + T | T T  T * F | F F  ( E ) | id I0 E’  .E E  .E + T E  .T T  .T * F T .F F  .( E ) F  .id
  • 39. Computation of CLOSURE SetOfltems CLOSURE (I) { J = I; repeat for ( each item A -> a·Bγ in J ) for ( each production B ->γ of G ) if (B ->.γ is not in J ) add B ->.γ to J; until no more items are added to J on one round; return J; }
  • 40.  Kernel items : The initial item, S' ->·S, and all items whose dots are not at the left end.  Non-kernel items : All items with their dots at the left end, except for S' -> ·S.
  • 41. The Function GOTO  GOTO (I, X) is defined to be the closure of the set of all items [A -> αX.β] such that [A -> α.Xβ] is in I.  If I is the set of two items { [E' -> E·] , [E -> E· + T] } , then  GOTO(I, +) contains the items  E -> E + ·T  T -> ·T * F  T -> ·F  F -> · (E)  F -> ·id
  • 43. Grammar S  E + S | E E  num E  num . S’  S . $ num ES’  . S $ S  .E + S S  . E E  .num 1 S  E . +S S  E . 2 S  E + S . 5 S  E + . S S  . E + S S  . E E  . num 3 S’  S $ . 7 4 S S $ + E num
  • 44. num + $ E S 1 s4 g2 g6 2 s3 SE 3 s4 g2 g5 4 Enum Enum 5 SE+S 6 s7 7 accept
  • 45. S' S S   L=R S   R L   *R L   id R   L id S' S S   L=R S   R L   *R L   id R   L L  id  S  L =R R  L  S' S  I0 I1 I2 I3 S  R I4 L  * R R   L L   id L   * R I5 S L * id R S  L=  R R   L L   *R L   id I6 = R S  L=R  R  L  L L I7 id I3 * * L  *R  R I8 I9
  • 46. state action goto id = * $ S L R 0 s3 s5 1 2 4 1 accept 2 s6/r(RL) 3 r(Lid) r(Lid) 4 r(SR) 5 s3 s5 7 8 6 s3 s5 7 9 7 r(RL) r(RL) 8 r(L*R) r(L*R) 9 r(SL=R)
  • 47.
  • 48.
  • 49. Structure of the LR Parsing Table 1. The ACTION function takes as arguments a state i and a terminal a (or $, the input endmarker). The value of ACTION[i, a] can have one of four forms: a) Shift j , where j is a state. The action taken by the parser shifts input a to the stack, but uses state j to represent a . b) Reduce A ->β. The action of the parser reduces β on the top of the stack to head A. c) Accept. The parser accepts the input and finishes parsing; d) Error. The parser discovers an error in its input and takes some corrective action. 2. Extend the GOTO function, defined on sets of items, to states: if GOTo [Ii , A] = Ij , then GOTO also maps a state i and a nonterminal A to state j .
  • 50. Construction of SLR Parse Table 1. Construct C={I0,I1…….In} the collection sets of LR(0) items for G’. 2. Initial state of the parser is constructed from the set of items for [S’->S] 3. State I is constructed from Ii. The parsing actions are determined as follows. 1. If [A->α.aβ] is in Ii and GOTO(Ii,a]=Ij, then ACTION[i,a] is set to ‘shift j’ here ‘a’ must be a terminal. 2. If[A->α.] is in Ii, then ACTION[i,a] is set to reduce A->a for all ‘a’ Follow(A). 3. If S’->S is in Ii, than action[i, $] is set to ‘accept’.
  • 51. Construction of SLR parsing table 4. GOTO transitions are constructed for all non-terminals. If GOTO(Ii,A) =Ij, then goto(i,A)of the parse table is set to j. 5. All other entries are error entries.
  • 52. LR-Parser Configurations  Helps to have complete state o its stack and the remaining input. A configuration of an LR parser is a pair:(s0s1 ………… sm, aiai+1…………an$).  where the first component is the stack contents and the second component is the remaining input.
  • 53. Behavior of the LR Parser
  • 54. LR-parsing algorithm.  INPUT: An input string w and an LR-parsing table with functions ACTION and GOTO for a grammar G.  OUTPUT: If w is in L ( G), the reduction steps of a bottom-up parse for W ; otherwise, an error indication.  METHOD: Initially, the parser has So on its stack, where So is the initial state, and w$ in the input buffer.