0% found this document useful (0 votes)
6 views35 pages

Lec 15

The document discusses regular grammars and their relationship with context-free grammars (CFGs), stating that all regular languages can be generated by CFGs, but not all CFGs generate regular languages. It explains the construction of CFGs from finite automata (FAs) and introduces Chomsky Normal Form (CNF), detailing methods for converting CFGs into CNF by removing nullable and unit productions. Examples illustrate the conversion process and the characteristics of regular grammars and their corresponding transition graphs (TGs).

Uploaded by

zeryabsajid143
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)
6 views35 pages

Lec 15

The document discusses regular grammars and their relationship with context-free grammars (CFGs), stating that all regular languages can be generated by CFGs, but not all CFGs generate regular languages. It explains the construction of CFGs from finite automata (FAs) and introduces Chomsky Normal Form (CNF), detailing methods for converting CFGs into CNF by removing nullable and unit productions. Examples illustrate the conversion process and the characteristics of regular grammars and their corresponding transition graphs (TGs).

Uploaded by

zeryabsajid143
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/ 35

Regular

Regular Grammar
All regular languages can be generated by CFGs.
Some nonregular languages can be generated by CFGs but not
all possible languages can be generated by CFG, e.g.
the CFG
S → aSb|ab generates the language {anbn:n=1,2,3, …},
which is nonregular.
Note: It is to be noted that for every FA, there exists a CFG that
generates the language accepted by this FA. Following is an
example in this regard
Example
Consider the language L expressed by
(a+b)*aa(a+b)*
the language of strings, defined over Σ ={a,b}, containing
aa.

To construct the CFG corresponding to L, consider the FA


accepting L, as follows
Example…
• Path Development
• (From S an a-edge takes us to M) S→ aM
• (From S a b-edge takes us to S) S → bS
• (From M an a-edge takes us to F) M → aF
• (From M a b-edge takes us to S) M → bS
• (From F an a-edge takes us to F) F → aF
• (From F a b-edge takes us to F) F → bF
• (When at the final state F, we can F → ʌ stop if we want to).
Semiword
DEFINATION
For a given CFG a semiword is a string of terminals (maybe
none) concatenated with exactly one nonterminal (on the right),
for example,
(terminal) (terminal) . . . (terminal) (Nonterminal)
Contrast this with word, which is a string of all terminals, and
working string, which is a string of any number of terminals
and nonterminals in any order.
Theorem 20
If all the productions in a given CFG fit one of the two forms:
Nonterminal →semiword
or
Nonterminal → word
(where the word may be ʌ) then the language generated by
this CFG is regular.
Regular Grammar
Definition:
A CFG is said to be a regular grammar if it
generates the regular language i.e. a CFG is said to
be a regular grammar in which each production
is one of the two forms
Nonterminal → semiword
Nonterminal → word
Example
1. The CFG : S → aaS|bbS|ʌ
is a regular grammar. It may be observed that the above CFG
generates the language of strings expressed by the RE (aa+bb)*.
2. The CFG S → aA|bB
A → aS|a
B → bS|b
is a regular grammar. It may be observed that the above CFG
generates the language of strings expressed by RE (aa+bb)+.
Following is a method of building TG corresponding to the regular
grammar.
TG for Regular Grammar
For every regular grammar there exists a TG corresponding to the
regular grammar.
Following is the method to build a TG from the given regular
grammar
1. Define the states, of the required TG, equal in number to that of
nonterminals of the given regular grammar. An additional state
is also defined to be the final state. The initial state should
correspond to the nonterminal S.
2. For every production of the given regular grammar, there are
two possibilities for the transitions of the required TG
Method continued …
(i) If the production is of the form nonterminal → semiword, then
transition of the required TG would start from the state
corresponding to the nonterminal on the left side of the
production and would end in the state corresponding to the
nonterminal on the right side of the production, labeled by
string of terminals in semiword.
(ii)If the production is of the form nonterminal →word, then
transition of the TG would start from the state corresponding to
nonterminal on the left side of the production and would end on
the final state of the TG, labeled by the word. Following is an
example in this regard
Example
Consider the following CFG
S → aaS|bbS|ʌ
The TG accepting the language generated by the above CFG is
given below

aa

bb ʌ
S-- +

The corresponding RE may be (aa+bb)*.


Example
Consider the CFG:
S→aaS |bbS | abX | baX | ʌ
X→aaX | bbX | abS | baS
Chomsky
Chomsky Normal Form
Chomsky Normal Form (CNF): If a CFG has only productions
of the form
nonterminal → string of two nonterminals
or
nonterminal → one terminal
then the CFG is said to be in Chomsky Normal Form (CNF).
Note
It is to be noted that any CFG can be converted to
be in CNF, if the null productions and unit
productions are removed. Also if a CFG contains
nullable productions as well, then the
corresponding new production are also to be
added. Which leads the following theorem
Theorem
All NONNULL words of the CFL can be generated by the
corresponding CFG which is in CNF i.e. the grammar in
CNF will generate the same language except the null
string.
Following is an example showing that a CFG in CNF
generates all nonnull words of corresponding CFL.
Remove Nullable Production
• We have not yet committed ourselves to a definite stand on the social
acceptability of ʌ-productions, that is, productions of the form:
• N→ ʌ
Remove NULL
If, in a certain CFG, there is a production of the form N→ A among the set of
productions, where N is any nonterminal (even S), then we can modify the grammar by
deleting this production and adding the following list of productions in its place. For all
productions of the form:
X → (blah 1) N (blah 2)
where X is any nonterminal (even S or N) and where (blah 1) and (blah 2) are anything
at all (even involving N), add the production
X → (blah 1) (blah 2)
Example 1
For example, in the CFG
S → a | Xb | aYa
X → Y|ʌ
Y → b |X
2
Example 3
Remove Unit Production
A production of the form
one Nonterminal → one Nonterminal
is called a unit production

Bar-Hillel, Perles, and Shamir tell us how to get rid of


these too.
Unit Production Elimination Method
Example
CFG:

Example…
THEOREM 23

If L is a language generated by some CFG, then there is


another CFG that generates all the non-A words of L,
all of whose productions are of one of two basic
forms:
Nonterminal→ string of only Nonterminals
or
Nonterminal → one terminal
Proof
• Let us also assume that the terminals are a and b. We now add two new nonterminals
A and B and the productions
A →a
B→b
• Now for every previous production involving terminals we replace each a with the
nonterminal A and each b with the nonterminal B. For example,
X3 → X4aX1SbbX7a becomes X3 → X4AX1SBBX 7A
• Even if we start with a string of solid terminals
X6 → aaba
we convert it into a string of solid nonterminals
X 6 → AABA
Example
Let us start with the Conversion
CFG: S →X1
S →X1 | X2aX2 |aSb | b S →X2AX2
S →ASB Nonterminal→ string of Nonterminals
Xl → X2X2 | b and two of the form:
S →B
X2 → aX2 | aaX1 Nonterminal →one terminal
X1 →X2X2
X1 →B
X2 →AX2
X2 →AAX1
A →a
B→b
Shorten to Two Non Terminal
Example 1
• Convert the following CFG to Chomsky Normal Form (CNF):
S → aX | Yb
X→S|ʌ
Y → bY | b

• Step 1 - Kill all ʌ productions:


• By inspection, the only nullable nonterminal is X.
• Delete all ʌ productions and add new productions, with all possible
combinations of the nullable X removed.
• The new CFG, without ʌ productions, is:
S → aX | a | Yb
X→S
Y → bY | b
Example(cont…)
S → aX | a | Yb
X→S
Y → bY | b

• Step 2 - Kill all unit productions:


• The only unit production is X → S, where the S can be replaced with all
• S’s non-unit productions (i.e. aX, a, and Yb).
• The new CFG, without unit productions, is:
S → aX | a | Yb
X → aX | a | Yb
Y → bY | b
Example (cont…)
S → aX | a | Yb
X → aX | a | Yb
Y → bY | b

• Step 3 - Replace all mixed strings with solid nonterminals.


• Create extra productions that produce one terminal, when doing the replacement.
• The new CFG, with a RHS consisting of only solid nonterminals or one terminal is:
S → AX | YB | a
X → AX | YB | a
Y → BY | b
A→a
B→b
Example: 02
• Convert the following CFG to Chomsky Normal Form (CNF):
S → AA
A → B | BB
B → abB | b | bb
Example 02
• Step 1 - Kill all /\ productions:
• There are no /\ productions, so none of the non-terminals is nullable.
• The CFG remains unchanged.
• Step 2 - Kill all unit productions:
• The only unit production is A → B, where the B can be replaced with all
B’s non-unit productions (i.e. all of them).
• The new CFG, without unit productions, is:
S → AA
A → BB | abB | b | bb
B → abB | b | bb
Example 02 (cont…)
• Step 3 - Replace all mixed strings with solid nonterminals.
• Create extra productions that produce one terminal, when doing the
replacement.
• The new CFG, with a RHS consisting of only solid nonterminals or one
terminal is:
S → AA
A → BB | XYB | b | YY
B → XYB | b | YY
X→a
Y→b
Solution: 02 (cont…)
• Step 4 - Shorten the strings of nonterminals to length 2.
• Create new, intermediate nonterminals to accomplish this.
• The new CFG, in CNF is:
S → AA
A → BB | RB | b | YY
B → RB | b | YY
R → XY
X→a
Y→b

You might also like