Assignment Presentation1(1)
Assignment Presentation1(1)
• Indirect Left Recursion: This occurs when a non-terminal refers to another non-terminal
that eventually leads back to itself. For example, in the productions.
A → Bα
B → Aβ | γ
Here, A indirectly refers to itself through B.
• Cont....
Objectives of Eliminating Left Recursion:
Ø Prevent Infinite Recursion: Ensures that parsers can terminate properly rather than
entering an infinite loop.
Ø Facilitate Top-Down Parsing: Makes the grammar suitable for parsing techniques that
rely on leftmost derivations, such as recursive descent parsers.
Steps to Eliminate Immediate Left Recursion
1. Identify Left Recursive Productions: Find productions of the form A→Aα∣ β.
2. Transform the Productions: Replace the left-recursive productions with new
productions to eliminate left recursion:
Rewrite the productions as:
A→βA′
A′→αA′∣ ϵ
Here,A′ is a new non-terminal representing the continuation after β.
Cont....
This transformation removes the left recursion, allowing for proper parsing.
Example 2: Indirect Left Recursion Consider the following CFG:
A → Bα
B → Aβ | γ
Step 1: Identify Left Recursive Productions
There is indirect left recursion since A can derive B, which can derive back to A.
Step 2: Transform the Productions
First, we need to rewrite productions for B:
Replace B→Aβ with the new definitions of A:
A→γA′
A′→βA′|ε
Substitute back into A:
A→γA′
Resolve the left recursion in A:
Finally, we get:
Cont....
A→γA′A→γA′
A′→αA′|βA′|ε
Final Grammar:
A → γ A'
A' → α A' | β A' | ε
5.Factoring:
Factoring in context-free grammars (CFGs) is a transformation technique used to eliminate
ambiguity and improve the efficiency of parsing by restructuring productions that share
common prefixes.
Factor out common prefixes in productions to make the grammar suitable for predictive
parsing (e.g., transforming A → αβ | αγ into A → αA' and A' → β | γ).The primary goal of
factoring is to simplify the grammar so that it can be parsed more easily, especially by
predictive parsers.
Cont....
Objectives of Factoring
ü Remove Ambiguity: By factoring out common prefixes, we reduce the potential for multiple
derivations for the same string, thus clarifying the grammar.
ü Facilitate Predictive Parsing: Factoring makes it easier for parsers to decide which
production to use based on the next input symbol, enhancing the parsing process.
ü Simplify Grammar: It reduces the complexity of productions, making the grammar easier to
understand and manage.
Steps to Factor a Grammar
a. Identify Common Prefixes: Look for productions that start with the same sequence of
symbols.
b.Create New Non-terminals: Introduce a new non-terminal symbol to represent the common
prefix.
c. Rewrite Productions: Rewrite the original productions to reflect the new structure, ensuring
that the common prefix is factored out
Cont....
Final Grammar:
S → A S'
S' → B | C
A→a
B→b
C→c
Example 3: Factor with Multiple Alternatives Consider the following CFG:
E → x | y | xz | yz
Step 1: Identify Common Prefixes
The productions x and xz share the prefix x, and y and yz share the prefix y.
Step 2: Create New Non-terminal
Introduce a new non-terminal E′.
Cont....
Step 3: Rewrite Productions
Rewrite the productions as follows
E→xE′∣ yE′
E′→z∣ ε
Final Grammar:
E → x E' | y E'
E' → z | ε
6.Chomsky Normal Form (CNF):
Chomsky Normal Form (CNF) is a specific type of context-free grammar (CFG) that is used in formal
language theory and computer science, particularly in parsing algorithms.
Convert the grammar to CNF, where every production is of the form A → BC or A → a, facilitating
certain parsing algorithms.A CFG is said to be in CNF if all its productions meet one of the following
criteria:
a. Binary Productions: Each production is of the form A→BC, where A is a non-terminal and B and
C are non-terminals.
Cont....
b. Terminal Productions: Each production is of the form A→a, where A is a non-terminal and aa is a
terminal symbol.
c. Start Symbol Production: The start symbol can derive the empty string (if the language includes
the empty string) with a special rule S→ϵ, but this is only allowed if S does not appear on the right-
hand side of any production.
Objectives of CNF
Simplification for Parsing: CNF simplifies parsing algorithms, especially the CYK algorithm,
which can efficiently parse strings in polynomial time.
Clarity and Consistency: CNF ensures that the grammar is structured in a uniform way, which
can help in understanding the grammar and its properties.
Theoretical Foundations: CNF is useful in proofs and theoretical discussions about context-free
languages and their properties.
Cont....
a. Remove Null Productions: Eliminate productions that
derive the empty string, unless the language requires it.
b. Remove Unit Productions: Eliminate productions of the
form A→B where A and B are non-terminals.
c. Remove Useless Symbols: Remove non-terminals and
productions that do not contribute to the derivation of terminal
strings.
d.Convert to Binary Productions: Ensure that all productions
are either binary or terminal productions. If a production has
more than two non-terminals, split it into multiple productions.
e. Convert Terminals in Mixed Productions: If a production
mixes terminals and non-terminals (e.g A→aB), replace the
terminal with a new non-terminal that derives the terminal.
Cont....