Wa0001

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Types of Parsing

Parsing: 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.

 Back-tracking : 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
As the name suggests, bottom-up parsing starts with the input symbols and tries to construct the
parse tree up to the start symbol.

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

Top-Down Parsing
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 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:

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.
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.
Parse Tree vs Syntax Tree
Parse Tree

Parse tree is a hierarchical structure that defines the derivation of the grammar to yield
input strings. In parsing, the string is derived using the start symbol. The root of the parse
tree is that start symbol. It is the graphical description of symbols that can be terminals or
non-terminals. Parse tree follows the precedence of operators. The deepest sub-tree
traversed first. Therefore, the operator in the parent node has less precedence over the
operator in the sub-tree.

Parse Tree for expression: 1*2+3

Syntax Tree:

A syntax tree is a tree that displays the syntactic structure of a program while ignoring
inappropriate analysis present in a parse tree. Thus, the syntax tree is nothing more than a
condensed form of the parse tree. The operator and keyword nodes of a parse tree are
shifted to their parent and a group of individual production is replaced by an individual
link. For example, for the above parse tree.
Let us see the comparison between the Parse tree and Syntax Tree.

Parse Tree Syntax Tree

It can contain operators & operands at any It contains operands at leaf node
node of the tree, i.e., either interior node or & operators as interior nodes of
leaf node. Tree.

It contains duplicate or redundant It does not include any redundant


information. data.

Parse Tree can be changed to Syntax Tree by Syntax Tree cannot be changed to
the elimination of redundancy, i.e., by Parse Tree.
compaction.

You might also like