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

CD Unit 2 RV

The document discusses different types of bottom-up parsing techniques used in compiler design, including: 1) Shift-reduce parsing, which uses a stack and input tape to shift symbols and reduce strings using production rules. 2) Operator precedence parsing, which handles grammars where operators have precedence over other terminals. It uses a precedence table to parse strings. 3) LR parsing, which is a general technique that parses based on looking ahead and reducing based on parsing tables. LR parsers include LR(1), SLR(1), LALR(1) parsers.

Uploaded by

Akash
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)
26 views

CD Unit 2 RV

The document discusses different types of bottom-up parsing techniques used in compiler design, including: 1) Shift-reduce parsing, which uses a stack and input tape to shift symbols and reduce strings using production rules. 2) Operator precedence parsing, which handles grammars where operators have precedence over other terminals. It uses a precedence table to parse strings. 3) LR parsing, which is a general technique that parses based on looking ahead and reducing based on parsing tables. LR parsers include LR(1), SLR(1), LALR(1) parsers.

Uploaded by

Akash
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/ 21

COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-1)

Compiler Design: Basic Parsing Techniques


Parser is that phase of compiler which takes token string as input and with the help of existing
grammar, converts it into the corresponding parse tree. Parser is also known as Syntax
Analyzer.
Syntax analyzers follow production rules defined by means of context-free grammar. The way
the production rules are implemented (derivation) divides parsing into two types : top-down
parsing and bottom-up parsing.

Top-down Parsing
When the parser starts constructing the parse tree from the start symbol and then tries to
transform the start symbol to the input, it is called top-down parsing.
Recursive descent parsing : 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.
Backtracking : It means, if one derivation of a production fails, the syntax analyzer
restarts the process using different rules of same production. This technique may process
the input string more than once to determine the right production.

Bottom-up Parsing
Bottom up parsing is also known as shift-reduce parsing.
Bottom up parsing is used to construct a parse tree for an input string.
In the bottom up parsing, the parsing starts with the input symbol and construct the parse
tree up to the start symbol by tracing out the rightmost derivations of string in reverse.

RAVIKANT NIRALA Page 1


COMPILER DESIGN (KCS-502)

Example:

Input string : a + b * c
Production rules:
S→E
E→E+T
E→E*T
E→T
T → id

Let us start bottom-up parsing


a+b*c

Read the input and check if any production matches with the input:
a+b*c
T+b*c
E+b*c
E+T*c
E*c
E*T
E
S

RAVIKANT NIRALA Page 2


COMPILER DESIGN (KCS-502)

Example:

Production
E→T
T→T*F
T → id
T→F
F → id

Parse Tree representation of input string "id * id" is as follows:

Bottom up parsing is classified in to various parsing. These are as follows:

1. Shift-Reduce Parsing
2. Operator Precedence Parsing
3. Table Driven LR Parsing

a. LR( 1 )
b. SLR( 1 )
c. CLR ( 1 )
d. LALR( 1 )

RAVIKANT NIRALA Page 3


COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-2)

Compiler Design: Top-Down Parsing


We have learnt in the last chapter that the top-down parsing technique parses the input, and starts
constructing a parse tree from the root node gradually moving down to the leaf nodes. The types
of top-down parsing are depicted below:

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 WRITTEN NOTES
X → oa | ea
Z → ai

RAVIKANT NIRALA Page 1


COMPILER DESIGN (KCS-502)

For an input string: read, a top-down parser, will behave like this:
It will start with S from the production rules and will match its yield to the left-most letter of the
input, i.e. ‘r’. The very production of S (S → rXd) matches with it. So the top-down parser
advances to the next input letter (i.e. ‘e’). The parser tries to expand non-terminal ‘X’ and
checks its production from the left (X → oa). It does not match with the next input symbol. So
the top-down parser backtracks to obtain the next production rule of X, (X → ea).
Now the parser matches all the input letters in an ordered manner. The string is accepted.

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.

RAVIKANT NIRALA Page 2


COMPILER DESIGN (KCS-502)

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
aheads. Generally k = 1, so LL(k) may also be written as LL(1).

RAVIKANT NIRALA Page 3


COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-3)

Compiler Design: Bottom-Up Parsing


Shift reduce parsing WRITTEN NOTES COPY

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.
Sift reduce parsing performs the two actions: shift and reduce. That's why it is known as
shift reduces parsing.
At the shift action, the current symbol in the input string is pushed to a stack.
At each reduction, the symbols will replaced by the non-terminals. The symbol is the
right side of the production and non-terminal is the left side of the production.

Example:

Grammar:

S → S+S
S → S-S
S → (S)
S→a

Input string:

a1-(a2+a3)

RAVIKANT NIRALA Page 1


COMPILER DESIGN (KCS-502)

Parsing table:

There are two main categories of shift reduce parsing as follows:

1. Operator-Precedence Parsing
2. LR-Parser

RAVIKANT NIRALA Page 2


COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-4)

Compiler Design: Bottom-Up Parsing


Operator precedence parsing NOTES COPY

Operator precedence grammar is kinds of shift reduce parsing method. It is applied to a small
class of operator grammars.

A grammar is said to be operator precedence grammar if it has two properties:

No R.H.S. of any production has a ∈.


No two non-terminals are adjacent.

Operator precedence can only established between the terminals of the grammar. It ignores the
non-terminal.

There are the three operator precedence relations:

a ⋗ b means that terminal "a" has the higher precedence than terminal "b".

a ⋖ b means that terminal "a" has the lower precedence than terminal "b".

a ≐ b means that the terminal "a" and "b" both have same precedence.

Precedence table:

Parsing Action

Both end of the given input string, add the $ symbol.


Now scan the input string from left right until the ⋗ is encountered.

RAVIKANT NIRALA Page 1


COMPILER DESIGN (KCS-502)

Scan towards left over all the equal precedence until the first left most ⋖ is encountered.
Everything between left most ⋖ and right most ⋗ is a handle.
$ on $ means parsing is successful.

Example

Grammar:

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

Given string:

w = id + id * id

Let us consider a parse tree for it as follows:

RAVIKANT NIRALA Page 2


COMPILER DESIGN (KCS-502)

On the basis of above tree, we can design following operator precedence table:

Now let us process the string with the help of the above precedence table:

RAVIKANT NIRALA Page 3


COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-5)

Compiler Design: Bottom-Up Parsing


Predictive Parsing: LR Parser
LR parsing is one type of bottom up parsing. It is used to parse the large class of grammars. 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.

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

RAVIKANT NIRALA Page 1


COMPILER DESIGN (KCS-502)

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.

RAVIKANT NIRALA Page 2


COMPILER DESIGN (KCS-502)

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

Drawing DFA:

The DFA contains the 7 states.

RAVIKANT NIRALA Page 3


COMPILER DESIGN (KCS-502)

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.

Productions are numbered as follows:

S → AA ... (1)
A → aA ... (2)
A → b ... (3)

Production Number is used in reduction.

RAVIKANT NIRALA Page 4


COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-6)

Compiler Design: Parsing Techniques

RAVIKANT NIRALA Page 1


COMPILER DESIGN (KCS-502)

RAVIKANT NIRALA Page 2


COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-7)

Compiler Design: LL(1) Parsing

RAVIKANT NIRALA Page 1


COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-8)

Compiler Design: LR(0) Parsing

RAVIKANT NIRALA Page 1


COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-9)

Compiler Design: LR(0) Parsing & SLR(1) Parsing

RAVIKANT NIRALA Page 1


COMPILER DESIGN (KCS-502)

UNIT-2 (Lecture-10)

Compiler Design: CLR(1) Parsing & LALR(1) Parsing

RAVIKANT NIRALA Page 1

You might also like