Recursion
Recursion
Parsing is classified into two categories, i.e. Top Down Parsing and Bottom-Up
Parsing. Top-Down Parsing is based on Left Most Derivation whereas Bottom Up
Parsing is based on Right Most Derivation.
Classification of Top Down Parsers
the process of constructing the parse tree which starts from the root and goes
down to the leaf is Top-Down Parsing.
Top Down Parsers uses leftmost derivation to construct a parse tree.
Classification of Top Down Parsers
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. This parsing technique
recursively parses the input to make a parse tree.
This parsing technique is regarded recursive as it uses context-free grammar
which is recursive in nature.
Recursive Descent Parsing
go with the first alternative and compare with the given String
If matching doesn’t occur then go with the second alternative and compare with
the given String.
If matching again not found then go with the alternative and so on.
Moreover, If matching occurs for at least one alternative, then the string is parsed
successfully.
LL(1) or Table Driven or Predictive
Parser –
A form of recursive-descent parsing that does not require any back-tracking is
known as predictive parsing.
It is efficient.
It needs special form of grammar called LL(1) grammar.
Non recursive (table driven) predictive parser is also known as LL(1)
Parser.
LL(1) or Table Driver or Predictive
Parser –
In LL1, first L stands for Left to Right and second L stands for Left-most
Derivation. 1 stands for number of Look Aheads token used by parser while
parsing a sentence.
LL(1) parsing is constructed from the grammar which is free from left recursion
and ambiguity.
Recursion-
Example-
S → Sa / ∈
(Left Recursive Grammar)
A grammar is said to be recursive if it contains at least one production that has the
same variable at both its LHS and RHS.
OR
A grammar is said to be recursive if and only if it generates infinite number of
strings.
Example-
S → Sa / b
(Left Recursive Grammar)
Right Recursive Grammar-
Example-
S → aS / b
(Right Recursive Grammar)
Non-Recursive Grammar-
Example-
S → aA / bB
A→a/b
B→c/d
(Non-Recursive Grammar)
A grammar is said to have indirect left recursion if, starting from any symbol of the
grammar, it is possible to derive a string whose head is that symbol.
For example,
A→Bα
B→Aγ
or
A --> Br
B --> Cd
C --> A
Where A, B, C are non-terminals and r, d, t are terminals.
Here, starting with A, we can derive A again on substituting C to B and B to A.
Left Recursion
Example:
(1) A => Aα | β
(2) S => Aα | β
A => Sd
Ambiguous grammar
The grammar which is both left recursive and right recursive is always
ambiguous.
Example-
E → E + E / E – E / E x E / id
(Ambiguous Grammar)
Ambiguous grammar
Top down parsers can not decide which production must be chosen to parse the
string.
Problem with Left Recursion:
To avoid this situation, it is converted into its equivalent right recursive grammar.
This is done by eliminating left recursion from the left recursive grammar.
To remove left recursion , we use left factoring.
Left Factoring-
In left factoring,
We make one production for each common prefixes.
The common prefix may be a terminal or a non-terminal or a combination of both.
Rest of the derivation is added by new productions.
Left Factoring-
the grammar obtained after the process of left factoring is called as Left Factored
Grammar.
Example-
Left Factoring-
Left recursion is eliminated by converting the grammar into a right recursive grammar.
If we have the left-recursive pair of productions-
A → Aα / β
(Left Recursive Grammar)
where β does not begin with an A.
Then, we can eliminate left recursion by replacing the pair of productions with-
A → βA’
A’ → αA’ / ∈
(Right Recursive Grammar)
This right recursive grammar functions same as left recursive grammar.
THANK
YOU