0% found this document useful (0 votes)
37 views9 pages

Recursive vs Non-Recursive Grammar

1. Grammars can be recursive or non-recursive. A recursive grammar contains at least one production with the same variable on both sides, allowing infinite strings. A non-recursive grammar generates a finite number of strings. 2. Recursive grammars can be left recursive or right recursive depending on whether the leftmost or rightmost variable matches the left side. Left recursive grammars are converted to right recursive form for top-down parsing. 3. Non-recursive grammars generate a finite number of strings and have neither left nor right recursion. Common prefixes in a grammar can be removed through left factoring to aid top-down parsing.

Uploaded by

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

Recursive vs Non-Recursive Grammar

1. Grammars can be recursive or non-recursive. A recursive grammar contains at least one production with the same variable on both sides, allowing infinite strings. A non-recursive grammar generates a finite number of strings. 2. Recursive grammars can be left recursive or right recursive depending on whether the leftmost or rightmost variable matches the left side. Left recursive grammars are converted to right recursive form for top-down parsing. 3. Non-recursive grammars generate a finite number of strings and have neither left nor right recursion. Common prefixes in a grammar can be removed through left factoring to aid top-down parsing.

Uploaded by

Sinan Ahmed
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Recursive Grammar
2. Non-Recursive Grammar

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


1. Left recursive grammar
2. Right recursive grammar

A) 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)

B) 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)

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

NOTE
A non-recursive grammar has neither left recursion nor right recursion.
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.

Important Notes-

Note-01:

The grammar which is either left recursive or right recursive is always unambiguous.
Examples-
• S → aS / b (Unambiguous Grammar)
• S → Sa / b (Unambiguous Grammar)

Note-02:

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)

Note-03:

• Left recursive grammar is not suitable for Top down parsers.


• This is because it makes the parser enter into an infinite loop.
• 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.

Note-04:

• The conversion of left recursive grammar into right recursive grammar and vice-versa is
decidable.

Recursion-

Recursion can be classified into following three types-

1. Left Recursion
2. Right Recursion
3. General Recursion

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

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.

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.

2. 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.
Example-

S → aS / ∈
(Right Recursive Grammar)

• Right recursion does not create any problem for the Top down parsers.
• Therefore, there is no need of eliminating right recursion from the grammar.

Also Read- Types of Recursive Grammar

3. General Recursion-

• The recursion which is neither left recursion nor right recursion is called as general
recursion.

Example-

S → aSb / ∈

Problem-01:

Consider the following grammar and eliminate left recursion-


A → ABd / Aa / a
B → Be / b

Solution-

The grammar after eliminating left recursion is-


A → aA’
A’ → BdA’ / aA’ / ∈
B → bB’

B’ → eB’ / ∈

Left Factoring | Left Factoring Examples

Grammar With Common Prefixes-

If RHS of more than one production starts with the same symbol,

then such a grammar is called as


Grammar With Common Prefixes.

Example-

A → αβ1 / αβ2 / αβ3


(Grammar with common prefixes)

• This kind of grammar creates a problematic situation for Top down parsers.
• Top down parsers cannot decide which production must be chosen to parse the string in
hand.
To remove this confusion, we use left factoring.

Left Factoring-

Left factoring is a process by which the grammar with common prefixes is transformed

to make it useful for Top down parsers.

How?
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.

The grammar obtained after the process of left factoring is called as Left Factored
Grammar.

Example-

Do left factoring in the following grammar-


S → iEtS / iEtSeS / a
E→b

Solution-

The left factored grammar is-


S → iEtSS’ / a
S’ → eS / ∈
E→b
Relationship Between Left Recursion, Left Factoring &
Ambiguity-

There is no relationship between Left Recursion, Left Factoring and Ambiguity of Grammar.

• All the three concepts are independent and has nothing to do with each other.
• The presence or absence of left recursion does not impact left factoring and ambiguity
anyhow.
• The presence or absence of left factoring does not impact left recursion and ambiguity
anyhow.
• The presence or absence of ambiguity does not impact left recursion and left factoring
anyhow.

Common questions

Powered by AI

Right recursion does not need to be eliminated for top-down parsers because it does not cause the infinite loop problem associated with left recursion. In right recursion, the recursive variable appears at the end of the production, so the parser processes intermediate symbols before returning to the recursive call, allowing the parser to eventually terminate naturally . This contrasts with left recursion, where the immediate recursive call leads to potentially infinite loops .

Converting a left recursive grammar to a right recursive one involves restructuring the grammar to eliminate the immediate recursive call. In a left recursive structure like A → Aα / β, the transformation changes it to A → βA' and A' → αA' / ε, making the recursion operate at the right end of the production . This transformation results in a grammar that top-down parsers can handle efficiently, as it prevents the infinite loop issue inherent in left recursion .

To handle grammars with common prefixes, left factoring is employed. This technique restructures the grammar to create singular productions for each common prefix, followed by new productions that cover the remaining differential derivations . This strategy benefits parsing by reducing ambiguity, allowing parsers, especially top-down ones, to select appropriate production rules without conflict or ambiguity, greatly enhancing parsing efficiency and accuracy in syntax analysis .

Left recursion can cause top-down parsers to enter an infinite loop because the leftmost variable in the production is the same as the LHS variable, continuously initiating the same recursive call without progress . To mitigate this, left recursion is converted into right recursion through a transformation process. This involves reworking the grammar so that it starts with a non-recursive production, followed by recursive additions . This conversion allows the top-down parser to effectively process the grammar without entering infinite loops.

Left factoring is used to resolve issues in grammars with common prefixes by reorganizing productions so that parsers can unambiguously decide which production to use . In left factoring, a single production is created for each common prefix, followed by new productions for the remaining derivations. This makes the grammar suitable for top-down parsers, which might struggle with choosing between multiple productions that start with the same prefix, a scenario that can lead to confusion in parsing without left factoring .

Top-down parsers are challenged by left recursion because it creates an immediate leftmost recursive call, causing the parser to reduplicate its efforts without advancing, leading to infinite recursion. This is particularly problematic because top-down parsers proceed by expanding non-terminals from left to right . Grammar refactoring, like converting left recursive grammars into right recursive grammars, eliminates this direct recursion, allowing parsers to advance through productions without becoming trapped in non-terminating loops .

A grammar that is both left recursive and right recursive is ambiguous because multiple parsing paths can lead to the same derivation, creating uncertainty in the grammar's interpretation . This ambiguity complicates syntactic analysis and reduces the predictability of parsing processes, as parsers cannot consistently determine a single, correct derivation path for input strings, potentially leading to multiple valid parses of the same input.

An unambiguous, recursive grammar provides a clear, unique parse tree for each input string due to having well-defined production rules without alternative or competing derivations . This distinction is crucial for syntax analysis as it ensures predictability and reliability in parsing, which simplifies the development of compilers and interpreters, making them more efficient and less error-prone.

Left recursion, left factoring, and ambiguity in grammars are independent concepts. The presence or absence of left recursion does not affect left factoring and ambiguity, and vice versa . This independence means that addressing an issue in one area, such as eliminating left recursion or applying left factoring, does not inherently solve or create issues related to ambiguity.

A recursive grammar is characterized by the ability to generate an infinite number of strings because it contains at least one production with the same variable on both its LHS and RHS . Conversely, a non-recursive grammar generates a finite number of strings and lacks any production with the same variable on both sides, indicating no recursion . These properties influence whether the language processing system can handle infinite or only finite linguistic constructions, significantly affecting parsing and language generation capabilities.

You might also like