Docsity

Parsing Techniques And Its Types, Study Guides, Projects, Research of Compiler Construction

Toggle sidebar
bg1
1
What is parsing? What is top down and bottom up parsing and
its sub types with parse tree and derivation?
Parsing
Definition:
A parser is a compiler or interpreter component that breaks data into smaller elements for easy translation
into another language. A parser takes input in the form of a sequence of tokens or program instructions
and usually builds a data structure in the form of a parse tree or an abstract syntax tree.
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
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. The types of top-down parsing are depicted below:
pf3
pf4
pf5
Discount

On special offer

Partial preview of the text

Download Parsing Techniques And Its Types and more Study Guides, Projects, Research Compiler Construction in PDF only on Docsity!

What is parsing? What is top down and bottom up parsing and

its sub types with parse tree and derivation?

Parsing

Definition:

A parser is a compiler or interpreter component that breaks data into smaller elements for easy translation into another language. A parser takes input in the form of a sequence of tokens or program instructions and usually builds a data structure in the form of a parse tree or an abstract syntax tree.

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  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. The types of top-down parsing are depicted below:

For example

Consider the grammar E:: = E+T | T (E is the start symbol) T ::= T*F | F F ::= id (assume some finite set

of possible ids) .Here is a top-down parse of the sentence x+y*z. Note that it is LL: we always expand the

leftmost nonterminal node.

x (^) y

z

E

F

T T F

T

F

E

The top-down parse corresponds to the derivation

E E+T

T+T

F+T

x+T

x+T*F

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.

Example: This is a rightmost derivation! int * int + int T → int int * T + int T → int * T T + int T → int T + T E → T T + E E → T+E E

Parse tree:

E

T (^) E

int (^) * T T

int int

Types of Bottom-up Parsing

Shift-Reduce Parsing

Shift-reduce parsing use 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. EXAMPLE Rightmost derivation of a+b+c, handles in red S → E → Ε + Τ → Ε + id → Ε + Τ + id → Ε + id + id → T + id + id → id + id + id

Parse tree: