0% found this document useful (0 votes)
57 views

Compiler Construction Unit 3 Part-4 LR Parser Predictive Parsing CSE

This document discusses bottom-up parsing and LR parsers. It provides details on: - Shift-reduce parsing uses shift and reduce steps, where shift advances the input pointer and reduce replaces grammar rule RHS with LHS. - LR parsers scan input from left-to-right and construct a rightmost derivation in reverse, including LR(0), SLR(1), LNR(1), and CLR(1) variants. - The LR(0) parser uses closure and goto operations on LR(0) items to build the parsing table, which is then used to parse inputs based on the current state.

Uploaded by

Pankaj Kumawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Compiler Construction Unit 3 Part-4 LR Parser Predictive Parsing CSE

This document discusses bottom-up parsing and LR parsers. It provides details on: - Shift-reduce parsing uses shift and reduce steps, where shift advances the input pointer and reduce replaces grammar rule RHS with LHS. - LR parsers scan input from left-to-right and construct a rightmost derivation in reverse, including LR(0), SLR(1), LNR(1), and CLR(1) variants. - The LR(0) parser uses closure and goto operations on LR(0) items to build the parsing table, which is then used to parse inputs based on the current state.

Uploaded by

Pankaj Kumawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

LEX

Compiler Construction
Unit 3 Part-4 LR Parser
Predictive parsing
CSE

By Himanshu Swarnkar
Engineering College Banswara
Parser

Top Down Bottom Up Parser


Parser (BUP)
(TDP) SR Parser(shift Reduce)

TDP with Full TDP Without Operator LR Parser


Backtracking Backtracking Precedence (Scanning from L to R, Reverse
Parser of Right most derivation)

Brute force
Method Recursive Descent Non Recursive LR(0) SLR(1) LNR(1) CLR(1)
Descent(LL(1))
Bottom Up Parse Tree S
Bottom Up Parsing
 Bottom up parsing stars with the input string and tries to construct the parse tree
up to the start symbol
 Means Starts from the terminals and ends on the start Symbol
A B
 Note: Bottom Up parser follows Reverse right most derivation and if focus on
what to reduce. Right Most Derivation
 Example Given Grammar G(VTPS), S→aABe
Production are S→aABe →aAde A
A →Abc/b →aAbcde
B →d →abbcde
String w= abbcde a b b c d e

Shift-Reduce Parsing
 Shift-reduce parsing uses two unique steps for bottom-up parsing.
 These steps are known as shift-step and reduce-step.
Shift step:
 The shift step refers to the advancement of the input pointer to the next input symbol, which is called the shifted symbol.
 This symbol is pushed onto the stack.
 The shifted symbol is treated as a single node of the parse tree.
Reduce step :
 When the parser finds a complete grammar rule (RHS) and replaces it to (LHS), it is known as reduce-step.
 This occurs when the top of the stack contains a handle.
 To reduce, a POP function is performed on the stack which pops off the handle and replaces it with LHS non-terminal symbol.
LR Parser
 A general shift reduce parsing is LR parsing. The L stands for scanning the input from left to right and R stands for constructing
a rightmost derivation in reverse.
Benefits of LR parsing:
 Many programming languages using some variations of an LR parser. It should be noted that C++ and Perl are exceptions to it.
 LR Parser can be implemented very efficiently.
 LR Parsers detect syntactic errors, as soon as possible.

LR Parser
(Scanning from L to R, Reverse of Right most derivation)

LR(0) SLR(1) LNR(1) CLR(1)


LR(0) Parser
 We need two functions –
Closure() Input Buffer
Goto()
Augmented Grammar $
 If G is a grammar with start symbol S then G’, the
augmented grammar for G, is the grammar with new
start symbol S’ and a production S’ -> S.
 The purpose of this new starting production is to LR(0) Parser
indicate to the parser when it should stop parsing and
announce acceptance of input. $

 Let a grammar be Input Stack


S →AA
A → aA | b LR(0) Parser Table
 The augmented grammar for the above grammar will
be
S’ → S S →.ABC
S → AA S → A.BC
A → aA | b S → AB.C
 LR(0) Items S → ABC.
An LR(0) is the item of a grammar G is a production
of G with a dot at some position in the right side.
S’ → S Closure Operation:
S → AA  If I is a set of items for a grammar G, then closure(I) is the set
A → aA | b of items constructed from I by the two rules:
S → AA. 1.Initially every item in I is added to closure(I).
S S’ → S. A
2.If A → α.Bβ is in closure(I) and B → γ is a production
S’ → .S A S → A.A then add the item B → .γ to I, If it is not already there.
S → .AA A → .aA | .b  We apply this rule until no more items can be added to
A → .aA | .b a a closure(I).
A → a.A a
b A → .aA | .b Goto Operation :
Goto(I,X) = 1. Add I by moving dot after X.
A → b. b A b 2. Apply closure to first step.

A → aA.
Note: Apply first Goto than closure
Final Item: Dot at the end of RHS

Canonical Collection of LR(0) Items


S’ → S LR(0) Parsing Table
S → AA P1
A → aA | b
P2 P3 I1 I5 Action Goto
S S’ → S. A S → AA. (for Terminals) (for Variables)
S’ → .S I2 S → A.A a b $ A S
A
S → .AA A → .aA | .b
A → .aA | .b a I0 S3 S4 I2 I1
I3 a
A → a.A | .b a I1
b I Accept
I0 4
A → .aA | .b
A → b. b A b I2 S3 S4 I5
I6 States
I3 S3 S4 I6
A → aA.
I4 r3 r3 r3

I5 r1 r1 r1

I6 r2 r2 r2
S’ → S Action
S Goto
S → AA P1 (for Terminals) (for Variables)
A → aA P2
A A → b P3 a b $ A S
Input string: aabb I0 S3 S4 I2 I1
I1 Accept
A
I2 S3 S4 I5

A A I3 S3 S4 I6
Accept
I4 r3 r3 r3
Note: In reduce we
actually reduce the aabb$ I5 r1 r1 r1
previous symbol
I6 r2 r2 r2

LR(0) Parsing Table


I0 a I3 a I3 b I4 A I6 A I6 A I2 b I4 A I5 S I1

Apply the reduce: See the number of symbol of RHS and reduce twice of it and push LHS in TOP of stack

You might also like