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

Lecture3 Parser Full

This document discusses different parsing techniques including top-down parsing, recursive descent parsing, predictive parsing, bottom-up parsing, shift-reduce parsing, LR parsing, SLR parsing, CLR parsing and LALR parsing. It provides details on the concepts, algorithms, and steps involved in each parsing technique with examples. Key points covered include recursive descent using procedures for each grammar entity, backtracking in parsing, predictive parsing using lookahead and parsing tables, shift and reduce steps in shift-reduce parsing, and constructing LR parsing tables.

Uploaded by

sabbir hossain
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)
49 views

Lecture3 Parser Full

This document discusses different parsing techniques including top-down parsing, recursive descent parsing, predictive parsing, bottom-up parsing, shift-reduce parsing, LR parsing, SLR parsing, CLR parsing and LALR parsing. It provides details on the concepts, algorithms, and steps involved in each parsing technique with examples. Key points covered include recursive descent using procedures for each grammar entity, backtracking in parsing, predictive parsing using lookahead and parsing tables, shift and reduce steps in shift-reduce parsing, and constructing LR parsing tables.

Uploaded by

sabbir hossain
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/ 30

Compiler Design (CSE 335)

Lecture3 - Parser

Papia Akter
Lecturer, Dept. of CSE
Prime University
Top Down Parsing
Recursive Descent Parsing
• Recursive descent is a top-down parsing technique that constructs the parse tree from
the top and the input is read from left to right.
• It uses procedures for every terminal and non-terminal entity.
• This parsing technique recursively parses the input to make a parse tree, which may
or may not require back-tracking. But the grammar associated with it (if not left
factored) cannot avoid back-tracking.
• A form of recursive-descent parsing that does not require any back-tracking is
known as predictive parsing.
This parsing technique is regarded recursive as it uses context-free grammar which is
recursive in nature.
Back-tracking
Top- down parsers start from the root node (start symbol) and match the input string
against the production rules to replace them (if matched). To understand this, take the
following example of CFG:
S → rXd | rZd
X → oa | ea
Z → ai
For an input string: read, a top-down parser, will behave like this:
Predictive Parser
• Predictive parser is a recursive descent parser, which has the capability to predict which
production is to be used to replace the input string.
• The predictive parser does not suffer from backtracking.
• To accomplish its tasks, the predictive parser uses a look-ahead pointer, which points to
the next input symbols.
• To make the parser back-tracking free, the predictive parser puts some constraints on the
grammar and accepts only a class of grammar known as LL(k) grammar.
• Predictive parsing uses a stack and a parsing table to parse the input and generate
a parse tree. Both the stack and the input contains an end symbol $ to denote that
the stack is empty and the input is consumed.
• The parser refers to the parsing table to take any decision on the input and stack
element combination.

In recursive descent parsing, the parser may have more than


one production to choose from for a single instance of input,
whereas in predictive parser, each step has at most one
production to choose. There might be instances where there is
no production matching the input string, making the parsing
procedure to fail.
LL Parser
• An LL Parser accepts LL grammar. LL grammar is a subset of context-free
grammar but with some restrictions to get the simplified version, in order to
achieve easy implementation.
• LL grammar can be implemented by means of both algorithms namely, recursive-
descent or table-driven.
• LL parser is denoted as LL(k). The first L in LL(k) is parsing the input from left
to right, the second L in LL(k) stands for left-most derivation and k itself
represents the number of look ahead. Generally k = 1, so LL(k) may also be
written as LL(1).
LL(1) Parsing Table
Grammar FIRST FOLLOW
E → TE’ { ( , id } {$,)}
E’ → + TE’ / ∈ {+,∈} {$,)}
T → FT’ { ( , id } {+,$,)}
T’ → * FT’ / ∈ {*,∈} {+,$,)}
F → (E) / id { ( , id } {*,+,$,)}

+ * ( ) id $
E → TE’ E → TE’ E → TE’
E’ → + TE’ / ∈ E’ → + TE’ E’ → ∈ E’ → ∈

T → FT’ T → FT’ T → FT’


T’ → * FT’ / ∈ T’ → ∈ T’ → * FT’ T’ → ∈ T’ →∈
F → (E) / id F → (E) F → id
Check whether the given string is accepted or rejected for the LL(1) Parser.
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. Here, we start from a sentence and then apply production rules
in reverse manner in order to reach the start symbol. The image given below depicts the
bottom-up parsers available.
Shift-Reduce Parsing
• Shift reduce parsing is a process of reducing a string to the start symbol of a grammar.
• Shift reduce parsing uses a stack to hold the grammar and an input tape to hold the string.

• 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.
Grammar:
1.S → S+S
2.S → S-S
3.S → (S)
4.S → a
Input string: a1-(a2+a3)
LR Parser
The LR parser is a non-recursive, shift-reduce, bottom-up parser. It uses a wide class of
context-free grammar which makes it the most efficient syntax analysis technique. LR
parsers are also known as LR(k) parsers,
Where,
L stands for left-to-right scanning of the input stream;
R stands for the construction of right-most derivation in reverse, and
k denotes the number of lookahead symbols to make decisions.
LR algorithm:
The LR algorithm requires stack, input, output and parsing table. In all type of LR parsing,
input, output and stack are same but parsing table is different.

Fig: Block diagram of LR parser


Input buffer is used to indicate end of input and it contains the string to be parsed followed
by a $ Symbol.
A stack is used to contain a sequence of grammar symbols with a $ at the bottom of the
stack.
Parsing table is a two dimensional array. It contains two parts: Action part and Go To part.
LR (1) Parsing
Various steps involved in the LR (1) Parsing:
• For the given input string write a context free grammar.
• Check the ambiguity of the grammar.
• Add Augment production in the given grammar.
• Create Canonical collection of LR (0) items.
• Draw a data flow diagram (DFA).
• Construct a LR (1) parsing table.
Augment Grammar
Augmented grammar G` will be generated if we add one more production in the given grammar G. It helps
the parser to identify when to stop the parsing and announce the acceptance of the input.
Example, Given grammar
S → AA
A → aA | b
The Augment grammar G` is represented by
S`→ S
S → AA
A → aA | b
Canonical Collection of LR(0) items
• An LR (0) item is a production G with dot at some position on the right side of the
production.
• LR(0) items is useful to indicate that how much of the input has been scanned up to a
given point in the process of parsing.
• In the LR (0), we place the reduce node in the entire row.
• Example
Given grammar:
S → AA
A → aA | b
Add Augment Production and insert '•' symbol at the first position for every production in
G
S` → •S
S → •AA
A → •aA
A → •b
For I0 State: Add Augment production to the I0 State and Compute the Closure
I0 = Closure (S` → •S)
I0= S` → •S
S → •AA
A → •aA
A → •b
i) Go to (I0, S) = closure (S` → S•) = S` → S• = I1
Here, the Production is reduced, so close the State.
I1= S` → S•

ii) Go to (I0, A) = closure (S → A•A) = I2


I2 = S→A•A
A → •aA
A → •b
iii) Go to (I0,a) = Closure (A → a•A) = I3
I3 = A → a•A
A → •aA
A → •b

iv) Go to (I0, b) = Closure (A → b•) = A → b• = I4

For I2 State: Go to (I2, A) = Closure (S → AA•) = S →A A• = I5


Go to (I2,a) = Closure (A → a•A) = (same as I3)
Go to (I2, b) = Closure (A → b•) = (same as I4)

For I3 State: Go to (I3, A) = Closure (A → aA•) = A → aA• = I6


Go to (I3, a) = Closure (A → a•A) = (same as I3)
Go to (I3, b) = Closure (A → b•) = (same as I4)
Drawing DFA:
The DFA contains the 7 states I0 to I6.
LR(0) Table
• If a state is going to some other state on a terminal then it correspond to a shift
move.
• If a state is going to some other state on a variable then it correspond to go to move.
• If a state contain the final item in the particular row then write the reduce node
completely.
Explanation:
• I0 on S is going to I1 so write it as 1.
• I0 on A is going to I2 so write it as 2.
• I2 on A is going to I5 so write it as 5.
• I3 on A is going to I6 so write it as 6.
• I0, I2and I3on a are going to I3 so write it as S3 which means that shift 3.
• I0, I2 and I3 on b are going to I4 so write it as S4 which means that shift 4.
• I4, I5 and I6 all states contains the final item because they contain • in the right
most end. So rate the production as production number.
SLR (1) Parsing
S→E
E→E+T|T
T→T*F|F
F → id
Add Augment Production and insert '•' symbol at the first position for every production in G
S` → •E
E → •E + T
E → •T
T → •T * F
T → •F
F → •id
CLR ( 1 ) Parsing
CLR ( 1 ) Grammar
S → AA
A → aA
A→ b

Add Augment Production, insert '•' symbol at the first position for every production in G and also
add the lookahead.
1.S` → •S, $
2.S → •AA, $
3.A → •aA, a/b
4.A → •b, a/b
LALR ( 1 ) Parsing

LALR ( 1 ) Grammar
S → AA
A → aA
A→ b

Add Augment Production, insert '•' symbol at the first position for every production in G and also
add the look ahead.
S` → •S, $
S → •AA, $
A → •aA, a/b
A → •b, a/b
LL vs. LR
LL LR
• Does a leftmost derivation. • Does a rightmost derivation in reverse.
• Starts with the root nonterminal on the stack. • Ends with the root nonterminal on the stack.
• Ends when the stack is empty. • Starts with an empty stack.
• Uses the stack for designating what is still to be • Uses the stack for designating what is already
expected. seen.
• Builds the parse tree top-down. • Builds the parse tree bottom-up.
• Continuously pops a nonterminal off the stack, • Tries to recognize a right hand side on the stack,
and pushes the corresponding right hand side. pops it, and pushes the corresponding
nonterminal.
• Expands the non-terminals. • Reduces the non-terminals.
• Reads the terminals when it pops one off the • Reads the terminals while it pushes them on the
stack. stack.
• Pre-order traversal of the parse tree. • Post-order traversal of the parse tree.

You might also like