KEMBAR78
CFG to CNF | PPTX
GROUP MEMBERS
▪ Zain UL Abideen
(BSSE-FA17-048)
Theory
Of
Automata
■ Presentation
CFGTO CNF
CFG
CFG stands for context-free grammar.
It is a formal grammar which is used to
generate all possible patterns of
strings in a given formal language.
CFG
Context-free grammar G can be
defined by four tuples as
G = (V, T, P, S)
CFG
G is the grammar, which consists of a set of the production
rule. It is used to generate the string of a language.
T is the final set of a terminal symbol. It is denoted by
lower case letters.
V is the final set of a non-terminal symbol. It is denoted by
capital letters.
P is a set of production rules, which is used for replacing
non-terminals symbols(on the left side of the production) in
a string with other terminal or non-terminal symbols(on the
right side of the production).
S is the start symbol which is used to derive the string. We
can derive the string by repeatedly replacing a non-
terminal by the right-hand side of the production until all
non-terminal have been replaced by terminal symbols.
CNF
CNF stands for Chomsky normal form. A
CFG(context free grammar) is in CNF(Chomsky
normal form) if all production rules satisfy one of
the following conditions:
• Start symbol generating ε. For example, A → ε.
• A non-terminal generating two non-terminals.
For example, S → AB.
• A non-terminal generating a terminal. For
example, S → a.
CONVERTTING
CFGTO CNF
Steps for converting CFG into CNF
■ Step 1: Eliminate start symbol from the RHS. If the start symbolT is at the right-hand side
of any production, create a new production as: S1 → S
■ Where S1 is the new start symbol.
■ Step 2: In the grammar, remove the null, unit and useless productions.You can refer to
the Simplification of CFG.
■ Step 3: Eliminate terminals from the RHS of the production if they exist with other non-
terminals or terminals. For example, production S → aA can be decomposed as:
■ S → RA
■ R → a
■ Step 4: Eliminate RHS with more than two non-terminals. For example, S → ASB can be
decomposed as:
■ S → RS
■ R → AS
EXAMPLE
Convert the given CFG to CNF. Consider the given
grammar G1:
S → a | aA | B
A → aBB | ε
B → Aa | b
SOLUTION:
Step 1: We will create a new production S1 → S, as the start symbol S
appears on the RHS. The grammar will be:
S1 → S
S → a | aA | B
A → aBB | ε
B → Aa | b
Step 2: As grammar G1 contains A → ε null production, its removal from the grammar yields:
S1 → S
S → a | aA | B
A → aBB
B → Aa | b | a
Now, as grammar G1 contains Unit production S → B, its removal yield:
S1 → S
S → a | aA | Aa | b
A → aBB
B → Aa | b | a
Also remove the unit production S1 → S, its removal from the grammar yields:
S0 → a | aA | Aa | b
S → a | aA | Aa | b
A → aBB
B → Aa | b | a
Step 3: In the production rule S0 → aA | Aa, S → aA | Aa, A → aBB and B → Aa, terminal a
exists on RHS with non-terminals. So we will replace terminal a with X:
S0 → a | XA | AX | b
S → a | XA | AX | b
A → XBB
B → AX | b | a
X → a
Step 4: In the production rule A → XBB, RHS has more than two symbols, removing it from
grammar yield:
S0 → a | XA | AX | b
S → a | XA | AX | b
A → RB
B → AX | b | a
X → a
R → XB
Hence, for the given grammar, this is the required CNF.
END
OF
Presentation
THANK
YOU

CFG to CNF

  • 2.
    GROUP MEMBERS ▪ ZainUL Abideen (BSSE-FA17-048)
  • 3.
  • 4.
  • 5.
    CFG CFG stands forcontext-free grammar. It is a formal grammar which is used to generate all possible patterns of strings in a given formal language.
  • 6.
    CFG Context-free grammar Gcan be defined by four tuples as G = (V, T, P, S)
  • 7.
    CFG G is thegrammar, which consists of a set of the production rule. It is used to generate the string of a language. T is the final set of a terminal symbol. It is denoted by lower case letters. V is the final set of a non-terminal symbol. It is denoted by capital letters. P is a set of production rules, which is used for replacing non-terminals symbols(on the left side of the production) in a string with other terminal or non-terminal symbols(on the right side of the production). S is the start symbol which is used to derive the string. We can derive the string by repeatedly replacing a non- terminal by the right-hand side of the production until all non-terminal have been replaced by terminal symbols.
  • 8.
    CNF CNF stands forChomsky normal form. A CFG(context free grammar) is in CNF(Chomsky normal form) if all production rules satisfy one of the following conditions: • Start symbol generating ε. For example, A → ε. • A non-terminal generating two non-terminals. For example, S → AB. • A non-terminal generating a terminal. For example, S → a.
  • 9.
  • 10.
    Steps for convertingCFG into CNF ■ Step 1: Eliminate start symbol from the RHS. If the start symbolT is at the right-hand side of any production, create a new production as: S1 → S ■ Where S1 is the new start symbol. ■ Step 2: In the grammar, remove the null, unit and useless productions.You can refer to the Simplification of CFG. ■ Step 3: Eliminate terminals from the RHS of the production if they exist with other non- terminals or terminals. For example, production S → aA can be decomposed as: ■ S → RA ■ R → a ■ Step 4: Eliminate RHS with more than two non-terminals. For example, S → ASB can be decomposed as: ■ S → RS ■ R → AS
  • 11.
    EXAMPLE Convert the givenCFG to CNF. Consider the given grammar G1: S → a | aA | B A → aBB | ε B → Aa | b
  • 12.
    SOLUTION: Step 1: Wewill create a new production S1 → S, as the start symbol S appears on the RHS. The grammar will be: S1 → S S → a | aA | B A → aBB | ε B → Aa | b
  • 13.
    Step 2: Asgrammar G1 contains A → ε null production, its removal from the grammar yields: S1 → S S → a | aA | B A → aBB B → Aa | b | a Now, as grammar G1 contains Unit production S → B, its removal yield: S1 → S S → a | aA | Aa | b A → aBB B → Aa | b | a Also remove the unit production S1 → S, its removal from the grammar yields: S0 → a | aA | Aa | b S → a | aA | Aa | b A → aBB B → Aa | b | a
  • 14.
    Step 3: Inthe production rule S0 → aA | Aa, S → aA | Aa, A → aBB and B → Aa, terminal a exists on RHS with non-terminals. So we will replace terminal a with X: S0 → a | XA | AX | b S → a | XA | AX | b A → XBB B → AX | b | a X → a Step 4: In the production rule A → XBB, RHS has more than two symbols, removing it from grammar yield: S0 → a | XA | AX | b S → a | XA | AX | b A → RB B → AX | b | a X → a R → XB Hence, for the given grammar, this is the required CNF.
  • 15.