Professional Documents
Culture Documents
Bottom Up Parsing
Bottom Up Parsing
Dr Pradosh Kumar
March 9, 2024
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 1 / 44
LR PARSER
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 2 / 44
Types of LR parser
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 3 / 44
Types of LR parser
Canonical LR Parsers (CLR).
The Canonical LR parses are powerful than the SLR parsers.
• It uses LR (1) items instead of LR(0) items, for constructing
the parsing table. It is difficult to implement in comparison
with the SLR counterpart.
• It reduces the shift/reduce conflict and the reduce/reduce
conflict.
Look-ahead LR parser (LALR)
• LALR Parser is lookahead LR parser. It is the most powerful
parser which can handle large classes of grammar.
• The size of CLR parsing table is quite large as compared to
other parsing table.
• LALR reduces the size of this table.LALR works similar to
CLR. The only difference is , it combines the similar states of
CLR parsing table into one single state.
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 4 / 44
Advantages of LR Parsers
Advantages of LR Parsers
• LR parsers are constructed to recognize all Programming
Languages
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 5 / 44
Components of LR Parser
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 6 / 44
Components of LR Parser
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 7 / 44
Components of LR Parser
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 8 / 44
Action and GOTO Table
• Action Table
• If Action[ Sm, ai] = Shift S
Where S is a State, then the Parser pushes ai and S on to the
Stack.
• If Action[ Sm, ai] = Reduce A− > 𝛽 ,
Then ai and Sm are replaced by A
if S was the state appearing below ai in the Stack, then
GOTO[S, A] is consulted and the state pushed onto the stack.
• Action[ Sm, ai] = Accept, Parsing is completed
• If Action[ Sm, ai] = Error,The Parser discovered an Error.
• GOTO Table
• The GOTO table specifies which state to put on top of the
stack after a reduce
Rows are State Names;Columns are Non-Terminals
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 9 / 44
Construction of LR Parse Table
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 10 / 44
Augmented Grammar
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 13 / 44
Example
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 14 / 44
Example
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 15 / 44
Example
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 16 / 44
Example
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 17 / 44
Example
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 18 / 44
Example
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 19 / 44
SLR Parsing
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 20 / 44
Construction of SLR Parsing Table
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 21 / 44
Construction of SLR Parsing Table
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 22 / 44
Step-1: Formation of Augmented Grammar
Augmented Grammar:
S‘ -> S
S -> AA
A -> aA
A -> b
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 23 / 44
Step-2: LR(0) Item Sets
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 24 / 44
Step-3: Find FOLLOW() from Grammar
FOLLOW(S)=$
FOLLOW(A)=a,b,$
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 25 / 44
Construction of SLR Parsing Table
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 26 / 44
Example
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 27 / 44
Example
Let the following be the grammar. Construct the SLR Parse Table
and show the parsing action for the string id*id+id
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 28 / 44
Example
LR parsing table.
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 29 / 44
Example
SLR parsing Action.
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 30 / 44
Example
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 31 / 44
Step-1: Augmented Grammar
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 32 / 44
Step-2:LR (0) Item Sets
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 33 / 44
DFA for LR(0) Items
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 34 / 44
Step-3: Finding Follow()
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 35 / 44
Step-4: Parse Table
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 36 / 44
CLR PARSING
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 37 / 44
Limitation of SLR Parsing
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 38 / 44
LR(1) Item Sets
The LR(k) Items are used to represent the set of possible states in
a parser
• An LR(k) item is a pair [A-> 𝛼·𝛽 , a ],a is the look-head of the
LR(1) item (a is a terminal or end-marker.)
• 𝛼 is a production from G with · (dot) at some position in the
RHS
• 𝛽 is a Lookahead String containing k symbols
Every LR(1) item in I is in closure(I)
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 39 / 44
Example
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 40 / 44
Augmented Grammar
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 41 / 44
LR(1) Item Set
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 42 / 44
Canonical LR Parsing Table
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 43 / 44
Parsing Table
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 44 / 44