Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

LR PARSING

Dr Pradosh Kumar

March 9, 2024

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 1 / 44
LR PARSER

• LR parsers are a type of Bottom-up parsers.


• LR parser reads their input from left to right and produces a
right-most derivation.
• It is called a Bottom-up parser because it attempts to reduce
the top-level grammar productions by building up from the
leaves.
• In the LR parsing, "L" stands for left-to-right scanning of the
input.
• "R" stands for constructing a right most derivation in reverse.
• "K" is the number of input symbols of the look ahead used to
make number of parsing decision.

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 2 / 44
Types of LR parser

LR parsers can be classified into following types.


1. LR (0) Parser
2.Simple LR-Parser (SLR)
3.Canonical LR Parser (CLR)
4.LALR Parser.
Simple LR Parsers (SLR)
• The Simple LR parsers are the easiest to implement.
• It uses LR (0) items to construct the parsing table and works
as a shift-reduce parser.
• It is easy to implement, but not powerful and still has some
shift/reduce and reduce/reduce conflicts even for an
unambiguous grammar.

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

• The LR-parsing is Non-Backtracking ShiftReduce Parser

• An LR parser can detect a syntactic errors

• It scans input string from left-to-right

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 5 / 44
Components of LR Parser

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 6 / 44
Components of LR Parser

• The above figure is the overall architecture of the LR parser


• The Same architecture is applicable for SLR, CLR and LALR
parsers.
• All the parsers will use a stack and a parsing table.
• The input string is appended with a $ and the stack is loaded
with an initial stack symbol.
• The parsing table will have two sections: action and goto.
• The action field will correspond to the shift, reduce, accept,
and error actions.
• The goto field helps manipulate the stack to decide on one of
the actions.

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 7 / 44
Components of LR Parser

Parse Table The LR Shift-Reduce Parsers can be efficiently


implemented by computing a table to guide the processing
The Parsing Table consists of two parts:
1. Action Function
2. GOTO function.
The Action Table
The Action Table specifies the actions of the parser (e.g., shift or
reduce), for the given parse state and the next token
Rows are State Names;
Columns are Terminals
The LR Parser determines Sm, the state on top of the stack and ai
the Current Input symbol.
It then consults Action[ Sm, ai] which can take one of four values:
Shift,Reduce,Accept, Error

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

Construction of LR Parse Table


Given the grammar G, the following steps are carried out to
construct the Parse Table
• Formation of the augmented grammar G’ for the given
grammar
• Construction of LR(0) items: To find LR(0) items, Closure(I)
and GOTO(I, X) have to be computed.
• Finally, Construct the parse table

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 10 / 44
Augmented Grammar

• If G is a Grammar with the Start Symbol S, the Augmented


Grammar G’is G with a New Start Symbol S‘, and New
Production S‘->S.
• The Purpose of the Augmented Grammar is to indicate to the
parser when it should stop parsing and announce the
acceptance of the input
Example:
S -> AA
A -> aA | b
Augmented Grammar:
S‘ -> S
S -> AA
A -> aA
A -> b
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 11 / 44
Canonical collection of LR(0) items

Canonical collection of LR(0) items


A production with a dot at some position on the right-hand side of
the production is called the LR (0) item.
Example: The possible LR (0) items for a production A -> BCD
A -> ·BCD
A -> B ·CD
A -> BC ·D
A -> BCD·
The production A-> 𝜀 generates only one item, A->.
At any point of the parsing process, LR (0) item indicates how
much portion of a production we have seen.
Closure Item: An Item created by the closure operation on a
state.
Complete Item: An Item where the Item Dot is at the end of the
RHS.
IFHE,FST,Hyderabad
Dr.Pradosh Kumar 12 / 44
Computation of Closure

procedure to compute closure (I)


begin
Repeat
for each rule A->𝛼 · B𝛽 in I and B-> 𝛾 in G
Add B->·𝛾 in I
Until no more elements can be added to I;
end
goto (I,X): If there is a production A->𝛼 ·X𝛽 in I then goto (I,X) is
defined as closure of the set of items of A->𝛼 X·𝛽 where I is set of
items and X is grammar symbol (non-terminal)
.

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 13 / 44
Example

Construct the LR(0) Parse Table from the given Grammar


S -> AA
A -> aA | b
Augmented Grammar:
S‘ -> S
S -> AA
A -> aA
A -> b

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

• SLR is very easy to construct and is similar to LR parsing.


• The only difference between SLR parser and LR(0) parser is
that in LR(0) parsing table, there’s a chance of ‘shift
reduced’conflict because we enter ‘reduce’corresponding
to all terminal states.
• We can solve this problem by entering ‘reduce’
corresponding to FOLLOW of LHS of production in the
completing state.
• This is called SLR(1) collection of items

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 20 / 44
Construction of SLR Parsing Table

1 Formation of augmented grammar


2 Find LR(0) collection of items
3 Find FOLLOW of LHS of production
4 Finally construct Parse Table

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 21 / 44
Construction of SLR Parsing Table

Construct the SLR Parse Table from the given Grammar


S -> AA
A -> aA | b

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

Consider the following Grammar


S->AS / b
A->SA /a
Construct the SLR parse table for the grammar.Show the actions
of the parser for the input string abab.

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

Construct the SLR Parse Table from the given Grammar


S->Aa | bAc| Bc | bBa
A -> d
B -> d

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 31 / 44
Step-1: Augmented Grammar

S->Aa | bAc| Bc | bBa


A -> d
B -> d
Augment Grammar:
S’-> S …rule 0
S ->Aa …’.. rule 1
S -> bAc …rule 2
S ->Bc …. rule 3
S -> bBa ….rule 4
A -> d …. rule 5
B -> d …rule 6

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

• The CLR parser stands for canonical LR parser.


• It is a more powerful LR parser.It makes use of lookahead
symbols.
• This method uses a large set of items called LR(1) items.
• The main difference between LR(0) and LR(1) items is that,
in LR(1) items, it is possible to carry more information in a
state, which will rule out useless reduction states.
• This extra information is incorporated into the state by the
lookahead symbol.
• The general syntax becomes [A-> 𝛼.B, a ] where A-> 𝛼 .B is
the production and a is a terminal or right end marker $
• LR(1) items=LR(0) items + look ahead

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 37 / 44
Limitation of SLR Parsing

The SLR Parser discussed in the earlier has certain flaws.


1.On single input,the State may be included a Final Item and a
Non- Final Item. This may result in a Shift-Reduce Conflict.
2.A State may include Two Different Final Items. This might result
in a Reduce-Reduce Conflict
3. SLR(1) Parser reduces only when the next token is in Follow of
the left-hand side of the production.
4. SLR(1) can reduce shift-reduce conflicts but not reduce-reduce
conflicts
These two conflicts are reduced by CLR(1) Parser by keeping track
of lookahead information in the parser states.
This is also called as LR(1) grammar

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)

• If A->𝛼·B𝛽 , a in closure(I) and B -> 𝛾 is a production rule of G;


then B -> ·𝛾 ,b will be in the closure(I) for each terminal b in
FIRST(𝛽 a)

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 39 / 44
Example

Construct the CLR Parse Table from the given grammar


S–>AA
A–>aA|b

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 40 / 44
Augmented Grammar

STEP 1 � Find augmented grammar


S’–>.S [Rule-0]
S–>.AA [Rule-1]
A–>.aA [Rule-2]
A–>.b [Rule-3]

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 41 / 44
LR(1) Item Set

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 42 / 44
Canonical LR Parsing Table

Initially construct set of items C = I0, I1, I2 ……In where C is a


collection of LR (1) items for G
.
Parsing actions are based on each item or state I1.
Various Actions are
If A− > 𝛼 · a𝛽 is in Ii and goto (Ii, a) = Ij then create an entry in
Action table Action [i, a] = shift j"
. If A− > 𝛼·, a is in Ii then set in Action table Action [i, a] to reduce
A− > 𝛼 . Here, A should not be S.
If S− > S· is in Ii then Action ( i, $ ) = accept"
. The goto part of the SLR table can be filled as
If goto (Ii, A) = Ij then goto [i, A] = j

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 43 / 44
Parsing Table

IFHE,FST,Hyderabad
Dr.Pradosh Kumar 44 / 44

You might also like