0% found this document useful (0 votes)
3 views68 pages

CD Unit 2

The document covers Compiler Design, specifically focusing on the role of parsers in syntax analysis, which is the second phase of a compiler. It discusses various parsing techniques including Top-Down and Bottom-Up parsing, their classifications, and methods for handling grammar issues such as ambiguity and left recursion. Additionally, it provides examples and algorithms related to parsing and grammar transformations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views68 pages

CD Unit 2

The document covers Compiler Design, specifically focusing on the role of parsers in syntax analysis, which is the second phase of a compiler. It discusses various parsing techniques including Top-Down and Bottom-Up parsing, their classifications, and methods for handling grammar issues such as ambiguity and left recursion. Additionally, it provides examples and algorithms related to parsing and grammar transformations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

Compiler Design

UNIT-2
by
V VEERA RASAD
Asst. Prof., Department of CSE
Aditya College of Engineering

Approved by AICTE, Accredited by NAAC and


Permanently affiliated to JNTUK, Kakinada,
Topics
 The Role of a parser
 Context free Grammars Writing A grammar
 Top Down ​Parsing
 Bottom Up Parsing
 Introduction to LR Parser

Department of CSE, Aditya College of Engineering, Sur 2


Date : 09/06/2025
Introduction
 Syntax Analysis or Parsing is the second phase of
a compiler.
 A Lexical Analyzer can identify tokens with the
help of Regular Expressions and pattern rules.
 But a Lexical Analyzer cannot check the syntax of
a given sentence due to the limitations of the
Regular Expressions.
 Regular Expressions cannot check balancing
tokens, such as parenthesis.
 Therefore, this phase uses Context-Free Grammar
(CFG), which is recognized by Push-Down
Automata

Department of CSE, Aditya College of Engineering, Sur 3


Date : 09/06/2025
The role of
parser

token
Source Lexical Parse tree Rest of Front Intermediate
Analyzer Parser End
program representation
getNext
Token

Symbol
table

4
 Syntax Analyzer or Parser takes the input from a
Lexical Analyzer in the form of token streams.
 The Parser analyzes the source code (token stream)
against the production rules to detect any errors in
the code.
 The output of this phase is a parse tree
 Parser accomplishes two tasks, i.e., parsing the
code, looking for errors and generating a Parse tree
as the output of the phase.
 Parsers are expected to Parse the whole code even
if some errors exist in the program.
 Parsers use error recovering strategies

Department of CSE, Aditya College of Engineering, Sur 5


Date : 09/06/2025
Classification of Parsing
Techniques

Department of CSE, Aditya College of Engineering, Sur 6


Date : 09/06/2025
(1) Top Down Parser
 Top-Down Parser is the parser which
generates parse for the given input string
with the help of grammar productions by
expanding the Non-Terminals
 It uses Left Most Derivation.

Top-Down Parser is classified into 2 types:


(i) Recursive Descent Parser
(ii) Non-Recursive Descent Parser

Department of CSE, Aditya College of Engineering, Sur 7


Date : 09/06/2025
(i) Recursive Descent
Parser
 It is a common form of top-down parsing.
 It is called recursive as it uses recursive

procedures to process the input.


 Recursive descent parsing suffers from

backtracking

Department of CSE, Aditya College of Engineering, Sur 8


Date : 09/06/2025
(ii) Non- Recursive Descent
Parser
 It is also known as LL(1) Parser or Predictive
Parser or without backtracking parser or
Dynamic parser.
 It uses parsing table to generate the parse

tree instead of backtracking

Department of CSE, Aditya College of Engineering, Sur 9


Date : 09/06/2025
(2) Bottom Up Parser
 Bottom Up Parser is the parser which
generates the parse tree for the given input
string with the help of grammar productions
by compressing the non-terminals i.e. it
starts from non-terminals and ends on the
stat symbol.
 It uses reverse of the right most derivation.

Bottom-up parser is classified into 2 types:


(i) LR parser
(ii) Operator Precedence parser

Department of CSE, Aditya College of Engineering, Sur 10


Date : 09/06/2025
(i) LR Parser
 LR parser is the bottom-up parser which
generates the parse tree for the given string
by using unambiguous grammar.
 It follow reverse of right most derivation

LR Parser is of 4 types:
(a) LR(0)
(b) SLR(1)
(c) LALR(1)
(d) CLR(1)

Department of CSE, Aditya College of Engineering, Sur 11


Date : 09/06/2025
(ii) Operator Precedence
Parser
 It generates the parse tree form given
grammar and string but the only condition
is two consecutive non-terminals and
epsilon never appear in the right-hand side
of any production

Department of CSE, Aditya College of Engineering, Sur 12


Date : 09/06/2025
GRAMMAR
A 4-tuple G = <VN, VT, P, S> of a language L(G)
– VN is a set of nonterminal symbols used to write the grammar

– VT is the set of terminals (set of words in the language L(G))

– P is a set of production rules


– S is a special symbol in VN, called the start symbol of the grammar

Derivation

A derivation is basically a sequence of production rules, in order to get the


input string. During parsing, we take two decisions for some sentential
form of input:
• Deciding the non-terminal which is to be replaced.

• Deciding the production rule, by which, the non-terminal will be replaced.

To decide which non-terminal to be replaced with production rule, we can


have two options

Department of CSE, Aditya College of Engineering, Sur 13


Date : 09/06/2025
 Left-most Derivation
If the sentential form of an input is scanned
and replaced from left to right, it is called left-
most derivation. The sentential form derived by
the left-most derivation is called the left-
sentential form.
 Right-most Derivation
If we scan and replace the input with
production rules, from right to left, it is known as
right-most derivation. The sentential form
derived from the right-most derivation is called
the right-sentential form

Department of CSE, Aditya College of Engineering, Sur 14


Date : 09/06/2025
Example
Production rules:
E→E+E
E→E*E
E → id
Input string: id + id * id
 The left-most derivation is:

E→E*E
E→E+E*E
E → id + E * E
E → id + id * E
E → id + id * id

Department of CSE, Aditya College of Engineering, Sur 15


Date : 09/06/2025
 The right-most derivation is:
E→E+E
E→E+E*E
E → E + E * id
E → E + id * id
E → id + id * id

Department of CSE, Aditya College of Engineering, Sur 16


Date : 09/06/2025
Parse Tree
 A parse tree is a graphical depiction of a derivation.
 It is convenient to see how strings are derived from the
start symbol.
 The start symbol of the derivation becomes the root of
the parse tree.
 Let us see this by an example from the last topic.
Take the left-most derivation of a + b * c
 The left-most derivation is:
E→E*E
E→E+E*E
E → id + E * E
E → id + id * E
E → id + id * id

Department of CSE, Aditya College of Engineering, Sur 17


Date : 09/06/2025
Parse Tree for the given input
string

Department of CSE, Aditya College of Engineering, Sur 18


Date : 09/06/2025
In a Parse Tree:
 All leaf nodes are terminals.
 All interior nodes are non-terminals.
 In-order traversal gives original input string.

A parse tree depicts associatively and


precedence of operators. The deepest sub-
tree is traversed first, therefore the operator
in that sub-tree gets precedence over the
operator which is in the parent nodes

Department of CSE, Aditya College of Engineering, Sur 19


Date : 09/06/2025
Ambiguity
A grammar G is said to be ambiguous
if it has more than one parse tree (left or
right derivation) for at least one string.
Example
E→E+E
E→E–E
E → id

Department of CSE, Aditya College of Engineering, Sur 20


Date : 09/06/2025
For the string id - id + id, the above
grammar generates two parse trees:

Department of CSE, Aditya College of Engineering, Sur 21


Date : 09/06/2025
 The language generated by an ambiguous
grammar is said to be inherently
ambiguous.
 Ambiguity in grammar is not good for a

compiler construction.
 No method can detect and remove

ambiguity automatically, but it can be


removed by either re-writing the whole
grammar without ambiguity

Department of CSE, Aditya College of Engineering, Sur 22


Date : 09/06/2025
Left Recursion
 A grammar becomes left-recursive if it has any non-terminal ‘A’ whose derivation
contains ‘A’ itself as the left-most symbol.

 Left-recursive grammar is considered to be a problematic situation for top-down


parsers.

 Top-down parsers start parsing from the Start symbol, which in itself is non-
terminal.

Example:
(1) A => Aα | β
It is an example of immediate left recursion, where A is any non-terminal
symbol and α represents a string of non-terminals.

(2) S => Aα | β
A => Sd
It is an example of indirect-left recursion.

Department of CSE, Aditya College of Engineering, Sur 23


Date : 09/06/2025
Removal of Left Recursion
One way to remove left recursion is to
use the following technique:
The production
A => Aα | β
After removing Left Recursion the production
will be
A => βA‘
A'=> αA' | ε

Department of CSE, Aditya College of Engineering, Sur 24


Date : 09/06/2025
Left Recursion Elimination
Example
Consider the following grammar
E -> E+T | T
T -> T*F | F
F -> (E) | id
After elimination of Left Recursion the grammar will be
E -> TE’
E’ -> +TE’ | ε
T -> FT’
T’ -> *FT’ | ε
F -> (E) | id

Department of CSE, Aditya College of Engineering, Sur 25


Date : 09/06/2025
Left Factoring
• Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive or top-down
parsing.
• We can easily perform left factoring:
– If we have A->αβ1 | αβ2 then we replace it with
• A -> αA’
• A’ -> β1 | β2
– For each non-terminal A, find the longest prefix α
common to two or more of its alternatives. If α≠ ɛ, then
replace all of A-productions A->αβ1 |αβ2 | … | αβn | γ
by
• A -> αA’ | γ
• A’ -> β1 |β2 | … | βn

Department of CSE, Aditya College of Engineering, Sur 26


Date : 09/06/2025
Left Factoring example
Consider the following grammar
S -> i E t S | i E t S e S | a
E -> b

After applying Left factoring the grammar will


be
S -> i E t S S’ | a
S’ -> e S | ɛ
E -> b

Department of CSE, Aditya College of Engineering, Sur 27


Date : 09/06/2025
Top-Down Parsing
 The advantage of TDP is that, the parsers can be
constructed easily by hand using top down methods
 The nature of TDP technique is characterized by

presenting three methods of performing such


parsers.
(1) Brute Force Method – It is presented both
informally and formally and is accompanied by a
Parsing algorithm
(2) Recursive Descent Parser – It is a Parsing
technique which does not allow backtracking
(3) Parsing Technique – Which allows Top-down
parsing with limited or partial backtracking

Department of CSE, Aditya College of Engineering, Sur 28


Date : 09/06/2025
Backtracking ( Top Down Parsing
with Brute Force Mehod )
Top Down Parsing with full backup is a Brute Force
method of Parsing
Algorithm
Step1: Given particular Non-Terminal that is to be
expanded, the first production for this Non-terminal
is applied
Step2 : Within this newly expanded string, the
next(Left most) Non-terminal is selected for
expansion and its first production is applied
Step3 : Step2 is repeated for all subsequent Non-
terminals that are selected until such time as the
Step2 can’t or should not be continued.

Department of CSE, Aditya College of Engineering, Sur 29


Date : 09/06/2025
This termination may be present in which
case the string has been successfully
parsed
(i) No more Non-terminals may be present, in
which case the string has been
successfully parsed.
(ii) It may result from an incorrect expansion
which does not match the appropriate
segment of the source string.

Department of CSE, Aditya College of Engineering, Sur 30


Date : 09/06/2025
(1) Illustrate the Brute Force method with the following
grammar
S  aAd | aB
Ab|c
B  ccd | ddc
Input string : accd

Department of CSE, Aditya College of Engineering, Sur 31


Date : 09/06/2025
(2) Illustrate Brute Force method with the
following grammar
S  cAd
A  ab | a
Input string : cad

Department of CSE, Aditya College of Engineering, Sur 32


Date : 09/06/2025
Computing

First
To compute FIRST(X), apply following rules until no
more terminals or ɛ can be added to any First set:

1. If X is a Terminal then FIRST(X) = {X}.

2. If X is a Non-Terminal , X  aα then FIRST(X) = {a}

3. If X is a Non-Terminal, X  ɛ is a production then FIRST(X) = { ɛ }

4. If X is a Non-Terminal, X  Y1Y2Y3……Yn then FIRST(X) = FIRST(Y1). If


ɛ is in FIRS(Y1) then X has FIRST(Y2) and so on. If FIRST(Yn) has ɛ
then ɛ is added to FIRST(X) *

33
Computing Follow
• To compute Follow(A) for all nonterminals A, apply following
rules until nothing can be added to any follow set:
1. If A is the starting symbol of given grammar, then add $ to
FOLLOW(A)
2. If there is a grammar rule, A-> αBβ(β‡ ɛ) then
FOLLOW(B) = FIRST(β) {doesn’t contain ɛ string}
FOLLOW(B) = FIRST(β)-{ɛ} + FOLLOW(A)
3. If there is a grammar rule, A->αB then
FOLLOW(B) = FOLLOW(A)

34
Example of First and
Follow Sets
E -> TE’
E’ -> +TE’ |
Ɛ
T -> FT’
T’ -> *FT’ | Ɛ
F -> (E) | id

35
E -> TE’ FIRST(E) = FIRST(T)
E’ -> +TE’ | Ɛ FIRST(E’) = {+, Ɛ}
T -> FT’ FIRST(T) = FIRST(F)
T’ -> *FT’ | Ɛ FIRST(T’) = {*, Ɛ}
F -> (E) | id FIRST(F) = {(,id}

Department of CSE, Aditya College of Engineering, Sur 36


Date : 09/06/2025
FOLLOW(E) = FIRST()) = {),$}
F  (E)
A-> αBβ
FOLLOW(E’) = FOLLOW(E) = {),$}
E  TE’ E’  +TE’
A-> αB A-> αB
FOLLOW(T) = FIRST(E’) = {+,),$}
E  TE’ E’  +TE’
A-> αBβ A-> αBβ
FOLLOW(T’) = FOLLOW(T) = {+,),$}
T  FT’ T’  *FT’
A-> αB A-> αB
FOLLOW(F) = FIRST(T’) = {+,*,),$}
T  FT’ T’  *FT’
A-> αBβ A-> αBβ

Department of CSE, Aditya College of Engineering, Sur 37


Date : 09/06/2025
S  L=R | R
L  *R | id
R L
Terminals={=,*,id}
Non-Terminals = { S,L,R}
FIRST(S)=FIRST(L)UFIRST(R)={*,id}
FIRST(L)={*,id}
FIRST(R)=FIRST(L)={*,id}
FOLLOW(S)={$}
FOLLOW(L)=FIRST(=R)=FIRST(=)={=} U FOLLOW(R)={$,=}
S L=R R L
A-> αBβ A-> Αb
FOLLOW(R)= FOLLOW(S)UFOLLOW(L)={$}U{=}={$,=}
S L=R SR L*R
A-> αBβ

Department of CSE, Aditya College of Engineering, Sur 38


Date : 09/06/2025
LL(1) Grammars
• Predictive Parsers are those Recursive
Descent Parsers needing no Backtracking
• Grammars for which we can create
Predictive Parsers are called LL(1)
– The first L means scanning input from left to right
– The second L means Leftmost Derivation
– And 1 stands for using one input symbol for Look-ahead
– More general one is LL(k), with k symbol Look-ahead

• A grammar G is LL(1) if and only if


whenever A-> α|β are two distinct
productions of G, the following
conditions hold

Department of CSE, Aditya College of Engineering, Sur 39


Date : 09/06/2025
Predictive Parsing (or) Table Driven Predictive
Parser (or) Non-Recursive Predictive Parser

Department of CSE, Aditya College of Engineering, Sur 40


Date : 09/06/2025
 Recursive Descent Parsing can also be
performed using a stack of activation
records. Such a parser is called Predictive
Parser
 It reads an input string
 Using a stack of activation records and

Parsing table, it produces the output


 The Parse table is constructed using FIRST

and FOLLOW functions.

Department of CSE, Aditya College of Engineering, Sur 41


Date : 09/06/2025
The steps to construct the Predictive Parse
table for a grammar, G are listed below

 Eliminate Left Recursion in grammar, G


 Perform Left Factoring on grammar, G
 Find FIRST and FOLLOW on the symbols in

grammar, G
 Construct the Predictive Parsing Table
 Check if the given input string can be

accepted by the Parser

Department of CSE, Aditya College of Engineering, Sur 42


Date : 09/06/2025
Construction of Predictive
Parsing table
For each production A->α in grammar do the
following:
1. For each terminal a in First(α) add A->αin M[A,a]
2. If ɛ is in First(α), then for each terminal b in
Follow(A) add A-> ɛ to M[A,b].
3. If ɛ is in First(α) and $ is in Follow(A), add A->ɛ to
M[A,$] as well
4. Mark other entries in the Parse table as ERROR.

Department of CSE, Aditya College of Engineering, Sur 43


Date : 09/06/2025
Parsing Action
Consider that ‘a’ is the input symbol
and X is on Top of Stack
(i) If X=a=$ then accept the input string
(ii) If X=a‡$ then POP(X) off the Top of Stack
and advance the input pointer to the next
position
(iii) If X is a Non-Terminal and ‘a’ is the input
then consult M[X,a] in the Parse table(M).

Department of CSE, Aditya College of Engineering, Sur 44


Date : 09/06/2025
 Construct the Predictive Parse Table for the
following grammar,
EE+T | T
TT*F | F
F(E) | id
Show the actions of the parser for the input
string id+id*id
(id+(id*id))

Department of CSE, Aditya College of Engineering, Sur 45


Date : 09/06/2025
E -> TE’
E’ -> +TE’ | Ɛ
T -> FT’
T’ -> *FT’ | Ɛ
F -> (E) | id

46
Parsing action for input string
id+(id*id)
STACK INPUT ACTION
$E id+(id*id)$ ETE’
$E’T id+(id*id)$ TFT’
$E’T’F id+(id*id)$ Fid
$E’T’id id+(id*id)$ pop id
$E’T’ +(id*id)$ T’ Ɛ
$E’ +(id*id)$ E’+TE’
$E’T+ +(id*id)$ POP +
$E’T (id*id)$ TFT’
$E’T’F (id*id)$ F(E)

Department of CSE, Aditya College of Engineering, Sur 47


Date : 09/06/2025
$E’T’)E( (id*id)$ POP (
$E’T’)E id*id)$ ETE’
$E’T’)E’T id*id)$ TFT’
$E’T’)E’T’F id*id)$ Fid
$E’T’)E’T’id id*id)$ pop id
$E’T’)E’T’ *id)$ T’*FT’
$E’T’)E’T’F* *id)$ POP *
$E’T’)E’T’F id)$ Fid
$E’T’)E’T’id id)$ pop id
$E’T’)E’T’ )$ T’e
Department of CSE, Aditya College of Engineering, Sur 48
Date : 09/06/2025
$E’T’)E’ )$ E’e
$E’T’) )$ POP )
$E’T’ $ T’e
$E’ $ E’e
$ $ SUCCESS

Department of CSE, Aditya College of Engineering, Sur 49


Date : 09/06/2025
(1) E TE’
FIRST(TE’) = FIRST(T) = { (,id }
M[E,(] = M[E,id] = ETE’
(2) E’+TE’
FIRST(+TE’) = FIRST(+) ={+}
M[E’,+] = E’  +TE’
(3) TFT’
FIRST(FT’) = FIRST(F) = {(,id}
M[T,(] = M[T,id] = T FT’
(4) T’*FT’
FIRST(*FT’) = FIRST(*) = {*}
M[T’,*] = T’*FT’
(5) F(E)
FIRST((E)) = FIRST(() = {(}
M[F,(] = F(E)
(6) Fid
FIRST(id) = {id}
M[F,id] = Fid

Department of CSE, Aditya College of Engineering, Sur 50


Date : 09/06/2025
(1) E’ Ɛ
FOLLOW(E’) = { ),$ }
M[E’,$] = M[E’,)] = E’ Ɛ
(2) T’ Ɛ
FOLLOW(T’) = { +,),$ }
M[T’,+] = M[T’,)] = M[T’,$] = T’ Ɛ

Department of CSE, Aditya College of Engineering, Sur 51


Date : 09/06/2025
Parsing Action for the input
string id+id*id

Department of CSE, Aditya College of Engineering, Sur 52


Date : 09/06/2025
Recursive Descent Parser
 RDP is a Top Down Parsing method of Syntax
Analysis in which a set of Recursive Procedures is
executed to process the input
 A procedure is associated with each non-terminal of
a grammar
 The look-ahead symbol unambiguously determines
the procedure selected for each non-terminal
 The sequence of procedures called in processing
the input implicitly defines a Parse Tree for the input
 A Parser that uses a set of Recursive Procedures to
recognize its input with no-backtracking is called
Recursive Descent Parser

Department of CSE, Aditya College of Engineering, Sur 53


Date : 09/06/2025
(1) Construct Recursive Descent Parser for
the following grammar
EE+T | T
TT*F | F
F(E) | id
Sol: After elimination of Left Recursion the
given grammar will be
E -> TE’
E’ -> +TE’ | Ɛ
T -> FT’
T’ -> *FT’ | Ɛ
F -> (E) | id

Department of CSE, Aditya College of Engineering, Sur 54


Date : 09/06/2025
Procedure E()
begin
T();
E’();
end
Procedure E’()
begin
if input_symbol = ‘+’ then
begin
ADVANCE();
T();
E’();
end
end

Department of CSE, Aditya College of Engineering, Sur 55


Date : 09/06/2025
Procedure T()
begin
F();
T’();
end

Procedure T’()
begin
if input_symbol = ‘*’ then
begin
ADVANCE();
F();
T’();
end
end

Department of CSE, Aditya College of Engineering, Sur 56


Date : 09/06/2025
Procedure F()
begin
if input_symbol = ‘id’ then
ADVANCE();
else if input_symbol = ‘(‘ then
begin
ADVANCE();
E();
if input_symbol = ‘)’ then
ADVANCE();
else
ERROR();
end
else
ERROR();
end

Department of CSE, Aditya College of Engineering, Sur 57


Date : 09/06/2025
(1) Construct Predictive Parse table for the following
grammar
S(L) | a
LL,S | S
Show the behavior of the Parser on the sentence (a,a)

(2) Construct Predictive Parse table for the following


grammar
SL=R | R
L*R | id
RL
Show the behavior of the Parser on the sentence
id=*id

Department of CSE, Aditya College of Engineering, Sur 58


Date : 09/06/2025
Error Recovery in Predictive
Parsing
 Error is detected during Predictive Parsing
when Terminal on Stack does not match the
next input symbol
 When Non-Terminal, A is on the Top of Stack

and ‘a’ is the next input symbol and the


Parsing table entry M[A,a] is empty
indicating ERROR
 Following are Error Recovery Methods

(i) Panic Mode Recovery


(ii) Phrase Level Recovery

Department of CSE, Aditya College of Engineering, Sur 59


Date : 09/06/2025
(i) Panic Mode Recovery
 This is based on the idea of skipping the
input symbol until a Token in a set of
synchronizing Tokens appear
 The synchronizing sets should be chosen so

that the parser recovery quickly from


ERROR

Department of CSE, Aditya College of Engineering, Sur 60


Date : 09/06/2025
(ii) Phrase Level Recovery
 In the Phrase level recovery the block
entries in the Predictive Parsing table are
filled with pointers to error routines
 These error routines may change, insert or

delete symbols on the input and issue


appropriate error message

Department of CSE, Aditya College of Engineering, Sur 61


Date : 09/06/2025
Bottom Up Parsing
 Bottom-up parsing starts from the leaf
nodes of a tree and works in upward
direction till it reaches the root node.
 Start from a sentence and then apply

production rules in reverse manner in order


to reach the start symbol.

Department of CSE, Aditya College of Engineering, Sur 62


Date : 09/06/2025
Department of CSE, Aditya College of Engineering, Sur 63
Date : 09/06/2025
Example for Bottom Up
Parsing

Department of CSE, Aditya College of Engineering, Sur 64


Date : 09/06/2025
Shift Reduce Parsing
Shift Reduce parser attempts for the
construction of parse in a similar manner as
done in bottom up parsing i.e. the parse tree
is constructed from leaves(bottom) to the
root(up). A more general form of shift reduce
parser is LR parser.
This parser requires some data structures i.e.
(i) A input buffer for storing the input
string.
(ii) A stack for storing and accessing the
production rules

Department of CSE, Aditya College of Engineering, Sur 65


Date : 09/06/2025
Basic Operations
 Shift: This involves moving of symbols from input
buffer onto the stack.
 Reduce: If the handle appears on top of the stack then,
its reduction by using appropriate production rule is
done i.e. RHS of production rule is popped out of stack
and LHS of production rule is pushed onto the stack.
 Accept: If only start symbol is present in the stack and
the input buffer is empty then, the parsing action is
called accept. When accept action is obtained, it is
means successful parsing is done.
 Error: This is the situation in which the parser can
neither perform shift action nor reduce action and not
even accept action.

Department of CSE, Aditya College of Engineering, Sur 66


Date : 09/06/2025
Example – Consider the grammar
S –> S + S
S –> S * S
S –> id
Perform Shift Reduce parsing for input string
“id + id + id”.

Department of CSE, Aditya College of Engineering, Sur 67


Date : 09/06/2025
Department of CSE, Aditya College of Engineering, Sur 68
Date : 09/06/2025

You might also like