Title: CSI 3104 /Winter 2006: Introduction to Formal Languages Chapter 13: Grammatical Format
1CSI 3104 /Winter 2006 Introduction to Formal
Languages Chapter 13 Grammatical Format
- Chapter 13 Grammatical Format
- I. Theory of Automata
- ? II. Theory of Formal Languages
- III. Theory of Turing Machines
2Chapter 13 Grammatical Format
- Theorem. All regular languages are context-free
languages. - Proof. We show that for any FA, there is a CFG
such that the language generated by the grammar
is the same as the language accepted by the FA. - By constructive algorithm.
- Input a finite automaton.
- Output a CF grammar.
3Chapter 13 Grammatical Format
- The alphabet ? of terminals is the alphabet of
the FA. - Nonterminals are the state names. (The start
state is renamed S.)
- For every edge, create a production
X ? aY
X ? aX
- For every final state, create a production
- X ? ?
4Chapter 13 Grammatical Format
S ? aM bS M ? aF bS F ? aF bF ?
babbaaba
S ? bS ? baM ? babS ? babbS ?babbaM ?babbaaF ?
babbaabF ? babbaabaF ? babbaaba
5Chapter 13 Grammatical Format
- Definition A semiword is a sequence of terminals
(possibly none) followed by exactly one
nonterminal. - (terminal)(terminal)(terminal)(Nonterminal)
- Definition A CFG is a regular grammar if all of
its productions have the form - Nonterminal ? semiword
- or
- Nonterminal ? word (a sequence of terminals
or ?)
6Chapter 13 Grammatical Format
- Theorem. All languages generated by regular
grammars are regular. - Proof. By constructive algorithm. We build a
transition graph. - The alphabet ? of the transition graph is the set
of terminals. - One state for each nonterminal. The state named
S is the start state. We add one final state . - Transitions
Nx ? wyNz
Np? wq
7Chapter 13 Grammatical Format
Example S ? aaS S ? bbS S ? L
Alternative Algorithm
- Np? wq whenever wq is not ?
- For each transition of the form N? ?, we mark the
state for N with ..
8Chapter 13 Grammatical Format
Example
S ? aaS bbS abX baX ? X ? aaX bbX
abS baS
EVEN-EVEN
9Chapter 13 Grammatical Format
- If the empty word is in the language, a
production of the form N ? ? (called a
?-production) is necessary. - The existence of a production of the form N ? ?
does not necessarily mean that ? is a part of the
language. - S ? aX S ? a X ? ?
- Theorem. Let L be a language generated by a CFG.
There exists a CFG without productions of the
form X ? ? such that - If ??L, L is generated by the new grammar.
- If ??L, all words of L except for ? are generated
by the new grammar.
10- Proof For each N ? ? and all
- X ? smt1 N smt2
- include production X ? smt1 smt2
- and eliminate N ? ?.
- Example S ? aSa bSb ?
- L-? generated with
- S ? aSa bSb aa bb
11Problem Creating new ? productions
- Example S ? a Xb aYa X ? Y ? Y ? b
X - S ? a Xb b aYa X ? Y Y ? b X ?
- Definition N nullable if N ?
- In example, X and Y are nullable Y ?X? ?
- Modified replacement rule
- Include X ? smt1 smt2 for each
- X ? smt1 N smt2 and N nullable,
- Delete all (old and new) ?-productions
- Ex S ? a Xb aYa b aa X ? Y Y ? b X
- How to find all nullable nonterminals in a CFG?
12Chapter 13 Grammatical Format
- Definition. A production of the form
- Nonterminal ? Nonterminal is called a unit
production. - Theorem. Let L be a language generated by a CFG
that has no ?-productions. Then there is another
CFG without ? -productions and without unit
productions that generates L. - Proof A ? B B ? S1 S2 .. Replace with A ? S1
S2 - Does it always work ?
- Example S ? A bb A ? B b
13Modified elimination rule
- Whenever A ? X1? X2? ?B introduce
- A ? S1S2S3 for all B ? S1S2S3 ..
- And eliminate all unit productions
- Example S ? A bb A ? B b B ? S a
- S ? bb b a A ? b a bb B ? a bb b
14- Theorem. Let L be a language generated by a CFG.
Then there exists another CFG that generates all
words of L (except ?) such that all the
productions are of the form - Nonterminal ? sequence of
Nonterminals - Nonterminal ? one terminal
- Proof Introduce A?a B?b and replace a, b,
with A, B,.. in all productions - Example S?XXaYaSbb X?YYb Y?aYaaX
- becomes?
15Chapter 13 Grammatical Format
- Definition. A CFG in is said to be in Chomsky
Normal Form (CNF) if all the productions have the
form - Nonterminal ? (Nonterminal)(Nonterminal)
- Nonterminal ? terminal
- Theorem. Let L be a language generated by a CFG.
There there is another grammar which is in CNF
that generates all the words of L (except ?).
16CNF construction
- Proof following previous theorem, one can
- Eliminate all ?-productions
- Eliminate all unit productions
- Reduce to X ? X1X2Xn and X?terminal
- Replace first type, if ngt2, with
- X?X1R1 R1?X2R2 R2?X3R3 Rn-3?Xn-2Rn-2 Rn-2 ?
Xn-1Xn
17Chapter 13 Grammatical Format
Definition. In a sequence of terminals and
nonterminals in a derivation (called a working
string), if there is at least one nonterminal,
then the first one is called the leftmost
nonterminal. Definition. A leftmost derivation is
a derivation where at each step, a production is
applied to the leftmost nonterminal in the
working string. Example. S ? aSX b
X ? Xb a S ? aSX ? aaSXX ? aabXX ? aabXbX ?
aababX ? aababa
18Chapter 13 Grammatical Format
S ? XY X ? XX a Y ? YY b
Example
aaabb
Derivation II
Derivation I
19Chapter 13 Grammatical Format
S ? XY ? XXY ? aXY ? aXXY ? aaXY ?
aaaY ? aaaYY ? aaabY ? aaabb
20Chapter 13 Grammatical Format
S ? XY ? XXY ? XXXY ? aXXY ? aaXY ?
aaaY ? aaaYY ? aaabY ? aaabb
21Chapter 13 Grammatical Format
Theorem. Any word that is in the language
generated by a CFG has a leftmost
derivation. Proof do a preorder traversal in the
derivation tree Example. S ? (S) S ? S S p
q S ? (S) ? (S ? S) ? (p ? S) ? (p ? (S)) ?
(p ? (S ? S)) ? (p ? (S ? S)) ? (p ? (p ?
S)) ? (p ? (p ? q))