0% found this document useful (0 votes)
4 views

Assignment Presentation1(1)

gdzg

Uploaded by

berihufsaha12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment Presentation1(1)

gdzg

Uploaded by

berihufsaha12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

NO Name of student ID

1 Berihu Fsaha Aku1202929

2 Desta Measho Aku1203248

3 Mekonen Girmay Aku1201483

4 Genet Andu Aku1208706


Cont....

• What are the Techniques transforming a context-free grammar


(CFG) ?
transforming a context-free grammar (CFG) involves several techniques
that can help simplify or standardize the grammar for various applications, such
as parsing. Here are some common techniques:
1. Removing Useless Symbols:
Removing useless symbols from a context-free grammar (CFG) involves
eliminating non-terminals and productions that do not contribute to the
generation of strings in the language defined by the grammar. Useless symbols
can be categorized into two types:
Cont....

• Non-generating Symbols: Non-terminals that do not derive any terminal


strings.
• Unreachable Symbols: Non-terminals that cannot be reached from the start
symbol of the grammar.
Steps to Remove Useless Symbols
a. Identify Generating Symbols:
Start from the terminal symbols and iteratively mark non-terminals that can derive
terminal strings.
A non-terminal is considered generating if there exists a production that can
eventually lead to a string of terminal symbols.
b. Identify Reachable Symbols:
Starting from the start symbol, mark all non-terminals that can be reached
through productions.
• A non-terminal is reachable if there exists a sequence of productions leading
from the start symbol to that non-terminal.
Cont....

c.Remove Useless Symbols:


Remove all non-terminals that are either non-generating or unreachable,
along with their corresponding productions.
Example 1: Removing Non-generating Symbols Consider the following CFG:
S → AB
A→a
B→b
C→c
Analysis: The generating symbols are A and B because they can produce
terminal strings (a and b).
C is a non-generating symbol because it does not derive any terminal string.
Cont....
After Removal:
S → AB
A→a
B→b
The non-terminal C and its production are removed.
Example 2: Removing Unreachable Symbols Consider the following CFG:
S→A
A→a
B→b
Analysis:
Starting from S, we can reach A, which can produce a.
However, B is unreachable from S since there is no production leading from S to B.
Cont....
After Removal:
S→A
A→a
The non-terminal B and its production are removed.
• Combined Example Consider a CFG:
S→A|B
A → aA | ε
B→b
C→c
Analysis:
From the productions, we see that A and B are generating symbols
(A can yield a or ε, and B can yield b).
However, C is non-generating, as it does not produce any terminal strings.
Reachability: S can reach A and B, but cannot reach C.
After Removal:
S→A|B
A → aA | ε
B→b
In this case, C is removed due to being non-generating and unreachable.
Cont....
2. Eliminating Null Productions:
Null productions in a context-free grammar (CFG) are productions that derive the
empty string (ε). Eliminating these productions is essential for simplifying the
grammar and making it suitable for parsing and other applications. Null productions
can complicate parsing and lead to ambiguities, so removing them helps clarify the
grammar's structure.
Objectives of Eliminating Null Productions
Simplification: Removing ε-productions reduces the number of productions,
making the grammar easier to understand and work with.
Avoiding Ambiguity: Null productions can introduce ambiguities in derivations
and parse trees, complicating the parsing process.
Facilitating Parsing: Many parsing algorithms (like LL and LR) work more
effectively when the grammar does not include null productions.
Cont....
Steps to Eliminate Null Productions

i. Identify Nullable Non-terminals: A non-terminal is considered nullable if it can derive ε


either directly(through a production like A → ε) or indirectly through other productions.
ii. Modify Productions: For each production that contains a nullable non-terminal, create
new productions that omit that non-terminal. This step involves creating combinations of the
remaining non-terminals that can derive terminal strings.
iii. Remove Null Productions: Finally, remove the original null productions from the
grammar.
Example 1: Simple Case Consider the following CFG:
S→A|B
A → aA | ε
B→b
Cont....
Step 1: Identify Nullable Non-terminals
A is nullable because it can derive ε.
Step 2: Modify Productions
We need to modify the production for S:
The original productions for S are S → A and S → B.
Since A is nullable, we can derive a new production S → B | ε (which is effectively
just S → B since S must derive a string).
• New Productions:
For A, we can derive:
From S → A, we can include a production S → ε if we consider S to be nullable.
The production for A remains unchanged.
Step 3: Remove Null Productions
The null production A → ε is removed.
Final Grammar:
S→A|B
A → aA
B→b
Cont....

Example 2: Multiple Nullable Non-terminals Consider a more complex CFG:


S → AB | ε
A → aA | ε
B→b
Step 1: Identify Nullable Non-terminals
Both S and A are nullable.
The production S → AB can produce:
S → AB
S → A (if B is omitted)
S → B (if A is omitted)
S → ε (already there)
New Productions:
The new productions for S will be:
S → AB | A | B
Step 3: Remove Null Productions
The null production S → ε is removed.
Cont....
Final Grammar:
S → AB | A | B
A → aA
B→b
3. Eliminating Unit Productions:
Unit productions in a context-free grammar (CFG) are productions of the form A→BA→B,
where AA and BB are both non-terminals. These productions are often seen as unnecessary
because they can be replaced with the productions of the non-terminal BB, thereby
simplifying the grammar.
Objectives of Eliminating Unit Productions
Simplification: Removing unit productions reduces the number of productions in the
grammar, making it simpler and easier to analyze.
Efficiency: A grammar without unit productions is generally more efficient for parsing
algorithms, as it reduces the number of derivation steps needed to produce strings.
Clarity: By reducing the dependency on intermediate non-terminals, the grammar
becomes clearer and more direct, which aids in understanding the language structure
Cont....

Steps to Eliminate Unit Productions


i.Identify Unit Productions: Look for all productions of the form A→B.
ii.Replace Unit Productions: For each unit production A→B, replace it with all productions
of B. If B has productions B→α1∣ α2∣ ...∣ αn, then add productions A→α1∣ α2∣ ...∣ αn​ to
the grammar.
iii. Remove Original Unit Productions: Finally, remove the original unit productions from
the grammar.
Example 1: Simple Case Consider the following CFG:
S→A
A→a|b
Step 1: Identify Unit Productions
The production S→A is a unit production
Step 2: Replace Unit Productions
Cont....
We replace S→AS→A with the productions of AA:
AA produces aa and bb, so we have:
S→aS→a
S→bS→b
Step 3: Remove Original Unit Productions
Remove the production S→AS→A.
Final Grammar:
S→a|b
A→a|b
(Note: In this case, we could also choose to remove non-terminal AA if it’s no longer
needed.)
Example 2: Multiple Unit Productions Consider a more complex CFG:
S→A|B
A→a
B→c|D
D→b
Cont....

Step 1: Identify Unit Productions


The productions S→A and S→B are unit productions.
The production B→D is also a unit production.
Step 2: Replace Unit Productions
Replace S→A with A’s productions:
S→a
Replace S→B with B’s productions:
S→c
S→D
Replace D in S→D with D’s productions:
S→b
New Productions for S:
Combine all:
S→a∣ c∣ b
Cont....

Step 3: Remove Original Unit Productions


Remove the unit productions S→A, S→B, and B→D.
Final Grammar:
S→a|c|b
A→a
B→c|b
D→b
4. Eliminating Left Recursion:
left recursion in a context-free grammar (CFG) occurs when a non-terminal in a
production can eventually derive itself as the leftmost symbol in its own derivation. This
can lead to infinite recursion during top-down parsing, making it problematic for
parsers that use recursive descent. Therefore, eliminating left recursion is essential for
creating grammars that can be efficiently parsed.
Cont....
Types of Left Recursion
• Immediate Left Recursion: This occurs when a non-terminal directly refers to itself in its
own production. For example, in the production A→Aα∣ βA→Aα∣ β, where αα is a string of
terminals or non-terminals and β is a string that does not start with A.

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

Example 1: Immediate Left Recursion


Consider the following CFG:
A → Aα | β
Step 1: Identify Left Recursive Productions
The production A→Aα is left recursive.
Step 2: Transform the Productions
We introduce a new non-terminal AA′:
Rewrite as:
A→βA′
A′→αA′|ε
Final Grammar:
A → β A'
A' → α A' | ε
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....

Example 1: Simple Factoring Consider the following CFG:


A → aB | aC
Step 1: Identify Common Prefixes
The productions aB and aC share the common prefix aa.
Step 2: Create New Non-terminal
Introduce a new non-terminal A′ to capture the choices after the common prefix.
Step 3: Rewrite Productions
Rewrite the productions as follows:
A→aA
A′→B∣ C
Final Grammar:
A → aA'
A' → B | C
Cont....
This transformation removes the ambiguity and clarifies the structure of the grammar.
Example 2: More Complex Factoring Consider the following CFG:
S → AB | AC
A→a
B→b
C→c
Step 1: Identify Common Prefixes
The productions AB and AC share the common prefix A.
Step 2: Create New Non-terminal
Introduce a new non-terminal S′ to represent the choices after A.
Step 3: Rewrite Productions
Rewrite the productions as follows:
S→AS′
CS′→B∣ C
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....

Example 1: Simple CFG


Consider the following CFG:
S → AB | a
A→a
B→b
Step 1: Identify Productions
The productions are already in a suitable form, but we need to ensure they conform to CNF.
Step 2: Remove Null Productions:
There are no null productions.
Step 3: Remove Unit Productions:
There are no unit productions.
Step 4: Convert to Binary Productions:
The production S→AB is fine, but we need to express aa as a separate production.
To ensure all productions fit CNF, we can rewrite it as:
S → A1B | A2
A2 → a
A1 → a
B→b
Cont....
Final CNGrammar:
S → A1B | A2
A1 → a
A2 → a
B→b
Example 2: More Complex CFG
Consider the following CFG:
S → aAB | b
A→a|ε
B→b
Step 1: Remove Null Productions
Since A→ε, we need to remove it:
S → aAB | aB | b
A→a
B→b
7. Greibach Normal Form (GNF):
Cont....
Greibach Normal Form (GNF) is a specific type of context-free grammar (CFG) where all productions are
structured such that each production starts with a terminal symbol followed by zero or more non-terminal symbols.
Convert the grammar to GNF, where every production is of the form A → aα, providing a structure suitable for
top-down parsing. In formal terms, a CFG is in GNF if all of its productions are of the form:
A→aα
where:
A is a non-terminal,
a is a terminal,
α is a (possibly empty) string of non-terminals.
Objectives of GNF Cont....
Facilitate Top-Down Parsing: GNF is particularly useful for top-down parsing algorithms, especially
recursive descent parsers, because it allows the parser to make decisions based solely on the next
input symbol.
Eliminate Left Recursion: GNF inherently avoids left recursion, which can lead to infinite loops in
top-down parsers.
Clarity and Structure: GNF provides a clear and structured way to represent grammars, making it
easier to analyze and implement.
Steps to Convert a CFG tAo GNF
I.Remove Null Productions: Eliminate productions that derive the empty string, unless necessary for
the language.
II.Remove Unit Productions: Eliminate productions of the form A→BA→B, where both are non-
terminals.
III.Remove Left Recursion: Any left recursion must be eliminated, as GNF does not allow it.
IV.Reorganize Productions: Ensure that all productions fit the GNF structure, starting with a terminal
followed by non-terminals
Cont....

Example 1: Simple CFG


Consider the following CFG
S → AB | a
A→a|b
B→b
Step 1: Remove Null Productions
There are no null productions to remove.
Step 2: Remove Unit Productions
There are no unit productions to remove.
Step 3: Remove Left Recursion
There is no left recursion.
Step 4: Convert to GNF
The production S→AB needs to be rearranged to fit the GNF format. We can rewrite S as follows:
Cont....
Introduce a new non-terminal:
S→aCS→aC
C→B∣ AC→B∣ A
Now update AA and BB:
A→aD∣ bDA→aD∣ bD
D→bD→b
Final GNF Grammar:
S → aA' | aB
A' → a | b
B→b
Example 2: More Complex CFG
Consider the following CFG:
S → aAB | b
A→a|ε
B→b
Step 1: Remove Null Productions
Cont....
A→ε must be removed, leading to:
S → aAB | aB | b
A→a
B→b
Step 2: Remove Unit Productions
There are no unit productions.
Step 3: Remove Left Recursion
There is no left recursion.
Step 4: Convert to GNF
Rearranging the productions to make sure they start with a terminal:
Introduce new productions:
S→aC∣ b
C→AB∣ B
Cont....
Now set:
A→aDA→aD
D→BD→B
B→bB→b
Final GNF Grammar:
S → aC | b
C → AB | B
A→a
B→b
8. Eliminating Non-Terminal Symbols:.
Eliminating non-terminal symbols from a context-free grammar (CFG) refers to the process of
removing non-terminals that do not contribute to the generation of terminal strings. Simplify the
grammar by eliminating non-terminal symbols that do not affect the languageThis process helps
simplify the grammar and can improve the efficiency of parsing. Non-terminal symbols can be
considered useless if they are not reachable from the start symbol or if they do not lead to any
Cont.... of Eliminating Non-Terminal Symbols
Objectives
Simplification: Removing unnecessary non-terminals reduces the complexity of the grammar,
making it easier to understand and work with.
Improved Efficiency: A simplified grammar can lead to more efficient parsing, as there are
fewer symbols for the parser to manage.
Clarity: A grammar with only relevant non-terminals is clearer and more focused on the
language it describes.
Steps to Eliminate Non-Terminal Symbols
I . Identify Useless Symbols:
II. Non-generating Symbols: Identify non-terminals that do not derive any terminal string.
III. Unreachable Symbols: Identify non-terminals that cannot be reached from the start symbol.
IV.Remove Useless Symbols: Eliminate the identified non-generating and unreachable non-
terminals along with their productions.
Cont....
Example 1: Simple CFG
Consider the following CFG:
S→A|B
A→a
B→b
C→c
Step 1: Identify Non-Terminal Symbols
The non-terminals are S,A,B,S,A,B, and CC.
Here, CC is a non-generating symbol because it does not derive any terminal strings.
Step 2: Remove Useless Symbols
We can safely remove CC and its production.
Final Grammar:
S→A|B
A→a
B→b
Cont....
Now the grammar consists only of relevant non-terminals.
Example 2: More Complex CFG
Consider the following CFG:
S→A|B
A → aC | ε
B→b
C→d
D→e
Step 1: Identify Non-Terminal Symbols
The non-terminals are S,A,B,C, and D.
C and D are non-generating symbols because D does not derive any terminal strings (it is not
reachable), and C can derive d but is not necessary for the production of S.
Cont....
Step 2: Remove Useless Symbols
We can remove C and D.
Final Grammar:
S→A|B
A→a|ε
B→b
9.Minimization:
Minimization of context-free grammars (CFGs) refers to the process of
simplifying a grammar by eliminating unnecessary components while preserving
the language it generates.
Minimize the grammar by reducing the number of productions and non-
terminals while preserving the language.The goal is to create a more efficient
and less complex grammar that still produces the same set of strings.
Cont....
Objectives of Minimization
Reduce Complexity: A minimized grammar is easier to understand and work with, making it
simpler for both human readers and parsing algorithms.
Efficiency: Minimizing a grammar can lead to more efficient parsing, as there are fewer
production rules and non-terminals to process.
Eliminate Redundancies: By removing redundant productions and non-terminals, the
grammar becomes more concise.
Cont....

Steps to Minimize a Context-Free Grammar


I.Remove Unreachable Symbols: Identify and eliminate non-terminals that cannot be reached from the
start symbol.
II. Remove Useless Symbols: Eliminate non-terminals that do not generate any terminal strings.
III. Eliminate Redundant Productions: Remove productions that do not contribute to the derivation of
terminal strings or are duplicates.
IV. Combine Equivalent Productions: If two or more non-terminals generate the same language,
consider merging them.
Example 1: Simple Minimization
Consider the following CFG:
S→A|B|C
A→a
B→b
C→d
D→e
Cont....
Step 1: Identify Symbols
The non-terminals are S,A,B,C,D.
Dis unreachable from S since there are no productions leading to D.
Step 2: Remove Unreachable Symbols
Remove Dand its production.
Final Grammar:
S→A|B|C
A→a
B→b
C→d
Example 2: More Complex Minimization
Consider the CFG:
S→A|B|C
A → aA | a
B → bB | b
C → cC | c
D→e
Cont....
• Step 1: Identify Symbols
• The non-terminals are S,A,B,C,D.
• DDis a useless symbol because it does not derive any terminal strings.
• Step 2: Remove Useless Symbols
• Remove D and its production.
• Step 3: Combine Similar Productions
• Notice that A, B, and C are similar in structure. They can be simplified but still need to exist
separately because they generate different terminal strings.
• Final Grammar:
• S→A|B|C
• A → aA | a
• B → bB | b
• C → cC | c
• In this case, there are no further redundancies, as each non-terminal serves a distinct
What are the objectives of transforming a context-free grammar (CFG) ?
transforming a context-free grammar (CFG) serves several important objectives, which are crucial for
various applications in computer science, particularly in the fields of compiler design, language processing,
and formal language theory. Here are the detailed objectives:
1. Simplification of Grammar
Easier Analysis: Simplifying a CFG makes it easier to analyze and understand the structure of the
language it generates. This is particularly useful for developers and linguists studying the properties of the
language.
Reduction of Complexity: By removing unnecessary symbols, productions, and complexities like left
recursion, the grammar becomes more straight forward, facilitating easier implementation in parsers.
2. Facilitation of Parsing
Adaptability to Parsing Algorithms: Transformations like converting to Chomsky Normal Form (CNF)
or Greibach Normal Form (GNF) make grammars more suitable for specific parsing algorithms (e.g., CYK
algorithm for CNF, recursive descent parsers for GNF).
Cont....
Elimination of Ambiguity: By factoring and restructuring the grammar, transformations can help reduce
ambiguity, making it easier for parsers to determine a unique parse tree for a given input.
3. Improved Efficiency
Optimized Parsing Speed: A well-structured grammar can lead to more efficient parsing processes,
reducing the time complexity of parsing operations.
Memory Utilization: By minimizing the number of productions and non-terminals, transformed
grammars can be more memory-efficient during parsing.
. Standardization
Uniformity Across Grammars: Transformations help standardize grammars, making it easier to apply
algorithms and techniques uniformly across different languages and grammars.
Interoperability: Standardized grammars can facilitate better interoperability between different systems
and tools that process languages, such as compilers and interpreters.
Cont....
5. Language Preservation
Ensuring Language Equivalence: A primary goal of transformation techniques is to ensure that
the transformed grammar generates the same language as the original grammar. This is crucial for
maintaining the integrity of language processing applications.
Retaining Semantic Information: While transforming grammars, care must be taken to preserve
the semantic meaning and structure of the original language.
6. Support for Compiler Design
Lexical and Syntax Analysis: In compiler construction, transforming CFGs aids in the design of
lexical analyzers and syntax analyzers, ensuring that source code can be accurately parsed and
analyzed.
Error Detection and Recovery: A clearer and simpler grammar can help in implementing better
error detection and recovery strategies during parsing.
7. Educational Purposes
Teaching Tool: Transforming CFGs can serve as an educational tool to illustrate concepts in
formal languages, automata theory, and compiler design, helping students grasp complex theories
through practical examples.

You might also like