Lec-28 Grammatical Format
Lec-28 Grammatical Format
Lec-28 Grammatical Format
Automata Theory
Lecture # 28
Chapter # 13 by Cohen
Grammatical Format
Semiword:
A semiword is a string of terminal (maybenone)
concaenated with exactly one nonterminal (on the
right). i.e. of the shape
(terminal) (terminal) … (terminal)(Nonterminal)
Theorem 22:
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.
2
Regular Grammar:
A CFG is called a regular grammar if each of its
productions is of one of the two forms
Nonterminal semiword
or Nonterminal word
Example:
Consider the CFG
S aaS | bbS |
Example:
Consider the regular CFG
S aaS | bbS | abX | baX |
S aaS | bbS | abS | baS
3
NULL-Productions: -Productions
The production of the form
Non-terminal
is said to be NULL production.
Note:
If a CFG has a Null production, then it is possible to
construct another CFG without Null production
accepting the same language with the exception of
the word .
Removing Null-Production:
Delete all the Null productions and add new
productions by substituting the in the old
productions.
4
Example:
Consider the CFG
S aSbSa |
Now remove the and add new productions as
Old production New productions
S aSbSa S aSba
S abSa
S aba
So the new CFG without is
S aSbSa |aSba | abSa | aba
5
Nullable-Productions:
A production is called nullable if is of the form
N
or there is derivation that starts at N and leads to
i.e.
N1 N 2 N 3 … N n
where N1,N2,…,Nn are non-terminals
Example:
S AA | bB
A aa | B
B aS |
.
6
Example:
S AA | bB
A aa | B
B aS |
Here
S AA and
AB are nullable productions while
B is null production
Removing Nullable-Production:
Remove all -productions by substituting the in
the old productions and also substitute for the
nullable productions.
7
Example:
S XY
X Zb
Y bW
Z AB
WZ
A aA | bA |
B Ba | Bb |
Here A and B are null productions, whereas
Z AB, W Z are nullable productions
Now removing all the null productions we get the
following new productions
8
Old productions New Productions
S XY Aa|b
X Zb Ba|b
Y bW Xb
Z AB Yb
WZ Z A (by substituting for B)
A aA | bA Z B
B Ba | Bb
So the final CFG is
S XY A aA | bA | a | b
X Zb | b B Ba | Bb | a | b
Y bW | b
Z AB | A | B
WZ
9
Unit Production:
The production of the form
nonterminal one terminal, is called unit production.
Removing Unit Production:
Examples:
Consider the following CFG
S A | bb
AB|b
BS|a
Separate the unit production from non-unit productions.
Now delete unit productions and add the new
productions that can be generated by using the deleted
unit productions.
10
Chomsky Normal Form (CNF):
A CFG is said to be in CNF if it has all its
productions of the form
nonterminal string of two nonterminals
nonterminal on terminal
Note:
Any CFG can be converted into CNF, if the null
productions and the unit productions are
remobed. Also if a CFG contains nullable
productions as well then the corresponding new
productions are also to be added.
11
Examples:
Consider the following CFG
S aSa | bSb | a | b | aa | bb
Convert it into equivalent CNF.
Steps for converting any CFG into CNF
i)Remove all the null and nullable productions
ii)Kill all the unit productions
iii)Convert all the productions of multiple letters into the
form
nonterminal string of two nonterminal
by converting all terminal in strings of multiple
letters into nonterminals and then introduce new
productions in the form of pairs of nonterminals.
12