Complexity and Computability Theory I

Download Report

Transcript Complexity and Computability Theory I

Complexity and Computability
Theory I
Lecture #10
Instructor: Rina Zviel-Girshin
Lea Epstein
Overview
 Regular grammars
 Examples
 Conversion of an NFA to a Regular Grammar
 Conversion of a Regular Grammar to an NFA
 Chomsky grammar hierarchy
 Operations over grammars
 Chomsky normal form
Rina Zviel-Girshin @ASC
2
Regular grammar
A grammar is called regular if each
production has one of the following forms:
Sw
or
SwT
where w* and S,TV.
Rina Zviel-Girshin @ASC
3
Regular grammar example
• Example
S  012
S  0A
A  0A
A 0
Rina Zviel-Girshin @ASC
4
Examples of regular grammars
• Construct a regular grammar for the
following regular expression:
0*
• Regular grammar:
S 0S | 
Rina Zviel-Girshin @ASC
5
Examples of regular grammars
• Construct a regular grammar for the
following regular expression:
(0+1)+
• Regular grammar:
S 0S | 1S | 0 | 1
Rina Zviel-Girshin @ASC
6
Examples of regular grammars
• Construct a regular grammar for the
following regular expression:
0*+1*
• Regular grammar:
S  | A | B
A 0A | 0
B 1B | 1
Rina Zviel-Girshin @ASC
7
Examples of regular grammars
• Construct a regular grammar for the
following regular expression:
(01)+
• Regular grammar:
S 01S | 01
Rina Zviel-Girshin @ASC
8
Conversion of NFA to Regular
Grammars
• A regular grammar can be constructed for
any NFA.
The basic idea:
translation of transition functions of an
automaton to rules of the resulting regular
grammar.
Rina Zviel-Girshin @ASC
9
Algorithm of conversion
1. Rename all states of NFA to a set of capital letters.
2. Name the start state of NFA S.
3. Translate each transition
(A,)=B into the rule AB
and
(A,)=B into the rule AB.
4. Add the rule A for each final state A of the
NFA.
Rina Zviel-Girshin @ASC
10
Example of conversion of NFA
to RG
0
0,1
q0
q1
1
0
We name q0: S and q1: A
The regular grammar is:
S 0S | 0A | 1A | 
A 0A | 1S
Rina Zviel-Girshin @ASC
11
Conversion of Regular
Grammars to NFA
• A NFA can be constructed for any regular
grammar G.
The basic idea:
construction of an NFA that accepts the
language of the given regular grammar.
Rina Zviel-Girshin @ASC
12
Algorithm
1. Transform all rules of the grammar to following
form:
Ax or AxB where x is either  or  and BV
(by adding variables to the grammar).
2. The start state of NFA is the grammar's start
symbol S.
3. For each rule:
AB construct a state transition
 B
A
from A to B labeled .
AB construct a state transition
A  B
from A to B labeled .
Rina Zviel-Girshin @ASC
13
Algorithm (cont.)
4. Add a new state F for each rule A and
construct a state transition
from A to the new state F A  F
labeled .
5. The final states of the NFA are F together
with all A such that there is a rule A.
Rina Zviel-Girshin @ASC
14
Example
S 0S | 11A
A 1A | 0
New grammar is:
S 0S | 1B
B 1A
A 1A | 0
Rina Zviel-Girshin @ASC
15
Example (cont.)
• The resulting NFA is:
1
S
1
B
1
A
0
F
0
Rina Zviel-Girshin @ASC
16
Chomsky grammar hierarchy
• The Chomsky hierarchy is an hierarchy of classes
of formal grammars that generate formal
languages.
• A formal grammar consists of:
– a finite set of terminal symbols,
– a finite set of nonterminal symbols,
– a set of production rules with a left- and a right-hand
side consisting of a word of these symbols,
– a start symbol.
Rina Zviel-Girshin @ASC
17
Chomsky hierarchy
• Chomsky hierarchy consist of 4 types of
grammars:
–
–
–
–
Regular (type 3)
Context-free (type 2)
Context-sensitive (type 1)
Recursively enumerable (type 0)
Rina Zviel-Girshin @ASC
18
Regular grammars
• Restricted to rules as:
Sa or SaT
where a and S,TV (different from our
definition - a*)
• Generates regular languages.
• These languages are exactly all languages
that can be decided by a FA .
Rina Zviel-Girshin @ASC
19
Context-free grammars
• Restricted to rules as:
Ab, AV and b(VU)*
• Generates context-free languages.
• These languages are exactly all languages
that can be decided by a nondeterministic
pushdown automaton PDA.
Rina Zviel-Girshin @ASC
20
Context-sensitive grammars
• Restricted to rules as:
αAβαγβ
AV and α , β, γ (VU)*
• Generates context-sensitive languages.
• These languages are exactly all languages
that can be decided by a linear-bounded
nondeterministic Turing machine BTM.
Rina Zviel-Girshin @ASC
21
Example
• L={anbn| n>=1}
CSG G:
S  aSBC
S  abC
CB  BC
bB  bb
bC  b
S
aSBC
a abC BC
Rina Zviel-Girshin @ASC
a abB C
a abb C
a abb
22
Recursively enumerable
grammar
• Another name unrestricted grammar.
• No restrictions on the rules.
• Generates recursively enumerable
languages RE.
• These languages are exactly all languages
that can be recognized by Turing machine.
Rina Zviel-Girshin @ASC
23
Relations between types
• Every regular language is context-free.
• Every context-free language is contextsensitive.
• Every context-sensitive language is
recursively enumerable.
• These are all proper inclusions.
Rina Zviel-Girshin @ASC
24
Operations over grammars
We can perform the
– union
– concatenation
– Kleene star
operations over grammars.
Rina Zviel-Girshin @ASC
25
Union
Given two languages and their grammars:
• L1 with G1= (V1,1,S1,R1) and
• L2 with G2 = (V2,2,S2,R2) V 1  V 2  
we construct their union by merging their
grammars:
• G = (V1V2S, 12, S,
R1R2{SS1| S2})
Rina Zviel-Girshin @ASC
26
Union (cont.)
• The rule
SS1 | S2
• means that a string w in L(G) can be
derived either from S1 or from S2.
Rina Zviel-Girshin @ASC
27
Concatenation
Given two languages and their grammars:
• L1 with G1= (V1,1,S1,R1) and
• L2 with G2 = (V2,2,S2,R2)
V1 V 2  
we construct their concatenation :
• G = (V1V2S, 12, S,
R1R2{SS1S2})
Rina Zviel-Girshin @ASC
28
Concatenation (cont.)
• The rule
SS1S2
• means that a string w in L(G) is a
concatenation of two parts w=uv,
• such that u is derived from S1 and v is
derived from S2.
Rina Zviel-Girshin @ASC
29
Kleene star
• Given a language and its grammar:
• L1 with G1= (V1,1,S1,R1)
• we construct L1* by the grammar:
• G = (V1 S, 1, S, R1 {SS1S|})
Rina Zviel-Girshin @ASC
30
Kleene star (cont.)
• The rule SS1S
• means that a word w in L(G) is built of two
parts w=uv such that u is derived from S1
and v is derived from S.
• The rule S
• means a final derivation of S or derivation
of the  string.
Rina Zviel-Girshin @ASC
31
Simplified grammars
• Every context-free grammar can be
rewritten in a simplified form.
• A simplified form of the grammar is a
grammar that
– doesn't have  rules and
– doesn't have unit rules.
•  rule is a rule of the form: A.
• Unit rule is a rule of the form: AB.
Rina Zviel-Girshin @ASC
32
Removing  rules
• A context-free language that does not
contain  can be written with a CFG without
 rules.
• If L then remove  from L.
• Build a CFG without  rules.
• Add a S'S |  rule to it.
Rina Zviel-Girshin @ASC
33
How to find the set of all
variables A such that A*?
Variable A is called nullable if *?
N 0  { A  V | A   is a productionrule}
i0
REPEAT
i  i 1
N i  N i 1  { A | A   is a productionrule
and  is a string of elem entsof N i 1}
UNTIL N i  N i 1
Rina Zviel-Girshin
@ASC
N i is the set of Nullable
Variables
34
Algorithm for removing  rules
1. Find the set of all variables A such that A*.
2. For each rule of G of the form Bw where
w(V)* create all possible new rules Bw'
where w' is obtained by deleting one or more
occurrences of variables found in step 1.
( If a variable on the right side can derive  then
create another rule where this variable on the
right side is deleted).
Rina Zviel-Girshin @ASC
35
Algorithm for removing  rules
3. The resulting grammar consists of the
original rules together with new one
constructed at step 2 minus -rules.
Rina Zviel-Girshin @ASC
36
A Simple Example
S aA
A 
A   is an  rule.
By rule 2 of the algorithm to the rule: S aA
we add the rule S a.
By rule 3 of the algorithm we delete A .
Rina Zviel-Girshin @ASC
37
Simple Example (cont.)
• The resulting grammar is:
S aA | a
• It is obvious that the first rule can’t be used
to derive any word.
• So it can be deleted.
• The minimized grammar is:
S
a
Rina Zviel-Girshin @ASC
38
Example
S aBaC
B bB | C
C cC | 
Variables C and B can derive .
• C
• BC
Rina Zviel-Girshin @ASC
39
Example (cont.)
• So we will delete B and C from the right
side of the rules:
Original grammar New rules
S aBaC
SaaC | aBa | aa
B bB
Bb
B C
B
C cC
Cc
C
none
Rina Zviel-Girshin @ASC
40
Example (cont.)
• The resulting grammar after removing rules is:
S aBaC | aaC | aBa | aa
B bB | b | C
C cC | c
Rina Zviel-Girshin @ASC
41
Removing unit rules
• A context-free grammar that contains unit
rules can be rewritten without unit rules.
• Unit rule: A  B
Rina Zviel-Girshin @ASC
42
Algorithm for removing unit
rules
1. For each unit rule AB , delete this rule
from the grammar and add all productions
of B to A.
( If Bw then after deleting AB a rule
Aw should be added to A rules.)
2. Repeat step 1 until all unit rules are
removed.
Rina Zviel-Girshin @ASC
43
Example
SA | b
A B | b
B bB | a
First we will eliminate AB unit rule.
• Ab
(old rule)
• AbB | a
(rules of B)
• AbB | a | b
(union of old and new rules)
Rina Zviel-Girshin @ASC
44
Example (cont.)
• The resulting grammar is:
SA | b
A bB | a | b
B bB | a
Rina Zviel-Girshin @ASC
45
Example (cont.)
• Now we will eliminate SA unit rule.
Sb
(old rule)
S bB | a | b
(rules of A)
SbB | a | b
(union of old and new rules)
The resulting grammar is:
S bB | a | b
A bB | a | b
B bB | a
Rina Zviel-Girshin @ASC
46
Example (cont.)
• Now A is not reachable from S and can be
removed. (A is not used in any right-hand
side production).
• The resulting grammar is:
S bB | a | b
B bB | a
Rina Zviel-Girshin @ASC
47
Example (cont.)
• The resulting grammar is:
S bB | a | b
B bB | a
• What is the language of G?
L(G) = a + b + b+a
Rina Zviel-Girshin @ASC
48
Chomsky normal form
• Any context-free grammar can be written in a
special form called Chomsky form.
• A CFG is in Chomsky normal form if every rule
of it written a following form:
ABC or
A
where  and B,CV.
• If a language contains  then the S rule is
allowed.
Rina Zviel-Girshin @ASC
49
Chomsky normal form (cont.)
• The Chomsky normal form has several uses.
• Any string of length n can be derived in
2n-1 steps.
• The derivation tree is a binary tree.
Rina Zviel-Girshin @ASC
50
Converting CFG to CFG in
Chomsky form
1. Add a new start symbol S' and the rule
S'S to CFG.
2. Remove all -rules from CFG.
3. Remove all unit rules from CFG.
4. Convert all remaining rules into a proper
form.
Rina Zviel-Girshin @ASC
51
4.1. Replace each terminal a in a rule whose
right-hand side has two or more symbols
with variable A.
Also add a rule Aa to CFG.
4.2. For each rule of the form AB1B2..Bn
where n3 replace it with the following
rules:
AB1C and C B2..Bn
• Continue this step till all the rules have the
proper form (right-hand side of length2).
Rina Zviel-Girshin @ASC
52
Example
Write the following grammar in Chomsky
normal form.
S A | 0B0
A S | 1
B A | 0
Rina Zviel-Girshin @ASC
53
Example (cont.)
• We will add the S'S rule.
• There is no -rules.
• Unit rules are SA, AS, BA, S'S.
• We will start from AS
A 1
(old rule)
A A | 0B0 (new rules : the rule AA
has no meaning) so we leave A 0B0 | 1
Rina Zviel-Girshin @ASC
54
Example (cont.)
• Now we eliminate BA.
B 0
(old rule)
B 0B0 | 1
(new rule)
together B 0B0 | 1 | 0
Rina Zviel-Girshin @ASC
55
Example (cont.)
• Now we eliminate SA.
S 0B0
(old rule)
S 0B0 | 1
(new rule)
together S 0B0 | 1
• Now we eliminate S'S.
S' 0B0 | 1
(new rule)
Rina Zviel-Girshin @ASC
56
Example (cont.)
• We will throw away the rules of S and A
since S and A are not reachable from S'.
S'1 | 0B0
B 0B0 | 1 | 0
Rina Zviel-Girshin @ASC
57
Example (cont.)
• Now we will write all rules in Chomsky
form.
S'1 | S0BS0
B S0BS0 | 1 | 0
S0 0
Rina Zviel-Girshin @ASC
58
Example (cont.)
• We will replace the S0BS0 right side with
S0C and CBS0
• The resulting grammar is:
S'1 | S0C
B S0C | 1 | 0
CBS0
S0 0
Rina Zviel-Girshin @ASC
59
Any Questions?
Rina Zviel-Girshin @ASC
60