CSC312
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  N2  N3  … Nn  
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
A  B 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
W  Z
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 A  a | b
X  Zb B  a | b
Y  bW X  b
Z  AB Y  b
W  Z 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
W  Z
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
A  B | b
B  S | 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

Lec-hdbehebensjshshsus28 Grammatical Format.ppt

  • 1.
    CSC312 Automata Theory Lecture #28 Chapter # 13 by Cohen Grammatical Format
  • 2.
    Semiword: A semiword isa 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
  • 3.
    Regular Grammar: A CFGis 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
  • 4.
    NULL-Productions: -Productions The productionof 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
  • 5.
    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
  • 6.
    Nullable-Productions: A production iscalled nullable if is of the form N   or there is derivation that starts at N and leads to  i.e. N1  N2  N3  … Nn   where N1,N2,…,Nn are non-terminals Example: S  AA | bB A  aa | B B  aS |  . 6
  • 7.
    Example: S  AA| bB A  aa | B B  aS |  Here S  AA and A  B 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
  • 8.
    Example: S  XY X Zb Y  bW Z  AB W  Z 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
  • 9.
    Old productions NewProductions S  XY A  a | b X  Zb B  a | b Y  bW X  b Z  AB Y  b W  Z 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 W  Z 9
  • 10.
    Unit Production: The productionof the form nonterminal  one terminal, is called unit production. Removing Unit Production: Examples: Consider the following CFG S  A | bb A  B | b B  S | 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
  • 11.
    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
  • 12.
    Examples: Consider the followingCFG 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