0% found this document useful (0 votes)
41 views32 pages

Recursion

Uploaded by

sibghamehboob6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views32 pages

Recursion

Uploaded by

sibghamehboob6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

1

IN THE NAME OF ALLAH


THE MOST BENEFICIENT, THE MOST
MERCIFUL
Classification of Top Down Parsers

 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

 With Backtracking: Recursive Descent Parsing

 Without Backtracking: Predictive Parsing or LL(1) Parsing or Table Driven


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

 It is general parsing technique, but not widely used as it is not efficient(slower).


 It involves backtracking(if choice of a production rule does not work, we
backtrack to try other alternatives).
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-

 Recursion can be classified into following three types-


 Left Recursion
 Right Recursion
 General Recursion

Right Recursion-

 A production of grammar is said to have right recursion if the rightmost variable


of its RHS is same as variable of its LHS.
 A grammar containing a production having right recursion is called as Right
Recursive Grammar.

Left Recursion-

 A production of grammar is said to have left recursion if the leftmost variable of


its RHS is same as variable of its LHS.
 A grammar containing a production having left recursion is called as Left
Recursive Grammar.

Left Recursion-

 Example-

 S → Sa / ∈
 (Left Recursive Grammar)

 Left recursion is considered to be a problematic situation for Top down parsers.


 Therefore, left recursion has to be eliminated from the grammar.

Types of grammar

 On the basis of number of strings, grammars are classified as-


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.

 A recursive grammar may be either-


 Left recursive grammar
 Right recursive grammar
Left Recursive Grammar-

 A recursive grammar is said to be left recursive if the leftmost variable of RHS is


same as variable of LHS.
 OR
 A recursive grammar is said to be left recursive if it has Left Recursion.

 Example-

 S → Sa / b
 (Left Recursive Grammar)
Right Recursive Grammar-

 A recursive grammar is said to be right recursive if the rightmost variable of RHS


is same as variable of LHS.
 OR
 A recursive grammar is said to be right recursive if it has right recursion.

 Example-

 S → aS / b
 (Right Recursive Grammar)
Non-Recursive Grammar-

 A grammar is said to be non-recursive if it contains no production that has the


same variable at both its LHS and RHS.
 OR
 A grammar is said to be non-recursive if and only if it generates finite number of
strings.
 A non-recursive grammar has neither left recursion nor right recursion.
Non-Recursive Grammar-

 Example-

 S → aA / bB
 A→a/b
 B→c/d
 (Non-Recursive Grammar)

 The language generated from this grammar is L = { aa , ab , bc , bd }


 Since the grammar generates finite number of strings, therefore it is a non-
recursive grammar.
Indirect Left Recursion

 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

 This kind of grammar is not suitable for Top down parsers.

 Top down parsers can not decide which production must be chosen to parse the
string.
Problem with Left Recursion:

 If a left recursion is present in any grammar then, during compilation there is a


chance that the grammar will create infinite loop.
 when the parser encounters the same non-terminal in its derivation, it becomes
hard for it to judge when to stop parsing the left non-terminal and it goes into an
infinite loop.
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-

 Left factoring is a process by which the grammar is transformed to make it useful


for Top down parsers.
 it is a grammar transformation technique called Elimination of left recursion,
which provides a method to generate, given a left recursive grammar into another
grammar that is equivalent and is not left recursive.
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 Factoring is a grammar transformation technique. It consists in "factoring


out" prefixes which are common to two or more productions.
 For example, going from:
 A→αβ|αγ
 to:
 A → α A'
 A' → β | γ
Elimination of Left Recursion

 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

You might also like