Wa0001
Wa0001
Wa0001
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.
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:
Production rules:
S → E
E → E + T
E → E * T
E → T
T → id
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:
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.
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.
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.
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.