Unit 2 Basic Parsing Techniques
Unit 2 Basic Parsing Techniques
Techniques
Dr. Sushil Kumar
Parsing Techniques
• There are two parsing techniques:
1. Top-down parsing
2. Bottom-up parsing
Principle of parsing techniques
• Parser scans the input string from left to right
• Parser identifies the leftmost or rightmost derivation
• Parser uses the production rules for choosing the
appropriate derivation
• The different parsing techniques use different
approaches in selecting the appropriate rules for
derivation
Types of Parsing
Top Down parser
• 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
1. Backtracking
2. Left recursion
3. Left Factoring
4. Ambiguity
1. Backtracking
• Backtracking is a technique in which for expansion of non-terminal
symbol we choose one alternative and if some mismatch occurs then
we try another alternative if any.
• Example: S -> xPz
P -> yw | y
Input String: xyz
2. Left Recursion
that there is a derivation of the form A=> Aa for some ∝
• A grammar G is said to be left recursive if it has an non-terminal A such
• Examples:
S → Sa | b
S → Ab A → Ad | Sa | e
S → Ac A→Ab | Bc | a B → Bb | Aa | Sc | b
Elimination of Left Recursion which
is immediate
• Grammar Containing Left Recursion
A→β1A'|β2A'|......|βnA’
A'→∝1A'|∝2A'|....|∝mA’|∈
A -> ∝ β1 | ∝ β2 | ∝ β3 |…………..| ∝ βn
A -> ∝A’
• After removal of left factoring:
A -> β1 | β2 | β3|…………..| βn
Example 1: Consider the following Grammar:
S -> iEtS | iEtSeS |a
E -> b
After removal of left factoring:
S -> iEtSS’ | a
S’ -> Є | eS
E -> b
Example 2: Remove left factoring
A -> aAB | aA | a
B -> bB | b
A -> aA’
A’ -> AB | a | Є
B -> bB’
B’ -> b | Є
4. Ambiguity
• The ambiguous grammar is not desirable in top-down parsing.
• Hence, we need to remove the ambiguity from the grammar if it is
present
• How to check the ambiguity?
1. If we find two or more leftmost derivation for particular input string or
2. If we find two or more rightmost derivation for particular input string
• Example: Check the following grammar is ambiguous or not.
E -> E+E | E*E | id
How to remove ambiguity?
1. If the grammar has left associative operator (such as +,-,*,/)
1. Then induce left recursion
Grammar is Grammar is
ambiguous. Grammar is unambiguous as
unambiguous but well as free from
left recursion. left recursion.
Types of top-down parsing
Top Down
Parser
Predictive
Backtracking
Parser
Recursive- Descent
LL(1) Parser
(RD) Parser
Backtracking Parsing
• A backtracking parser tries different production rules to find
the match for the input string (Brute-force method).
• Backtracking parser is powerful than predictive parser.
• But backtracking is slower and requires exponential time in
general.
• Hence, backtracking is not preferred for practical
implementation of compilers.
Predictive Parser
• This parser tries to predict the next construction using one or more
lookahead symbols from input string.
• There are two types of predictive parser:
1. Recursive –Descent (RD) Parser
2. LL(1) parser:
First L stands for Left to right scanning, second L stands for
Leftmost derivation, and 1 means that it uses only one input
symbol (lookahead) to predict the parsing process.
Recursive –Descent (RD) Parser