complexity and computability theory i

60
Complexity and Computability Theory I Lecture #10 Instructor: Rina Zviel- Girshin Lea Epstein

Upload: ita

Post on 07-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Complexity and Computability Theory I

Complexity and Computability Theory I

Lecture #10

Instructor: Rina Zviel-Girshin

Lea Epstein

Page 2: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 2

Overview

Regular grammarsExamplesConversion of an NFA to a Regular Grammar Conversion of a Regular Grammar to an NFA Chomsky grammar hierarchyOperations over grammarsChomsky normal form

Page 3: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 3

Regular grammar

A grammar is called regular if each production has one of the following forms:

Sw

or

SwT

where w* and S,TV.

Page 4: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 4

Regular grammar example

• Example

S 012

S 0A

A 0A

A 0

Page 5: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 5

Examples of regular grammars

• Construct a regular grammar for the following regular expression:

0*

• Regular grammar:

S 0S |

Page 6: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 6

Examples of regular grammars

• Construct a regular grammar for the following regular expression:

(0+1)+

• Regular grammar:

S 0S | 1S | 0 | 1

Page 7: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 7

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

Page 8: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 8

Examples of regular grammars

• Construct a regular grammar for the following regular expression:

(01)+

• Regular grammar:

S 01S | 01

Page 9: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 9

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.

Page 10: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 10

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 AB

and

(A,)=B into the rule AB.

4. Add the rule A for each final state A of the NFA.

Page 11: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 11

Example of conversion of NFA to RG

We name q0: S and q1: A The regular grammar is:

S 0S | 0A | 1A | A 0A | 1S

q0 q1

0,1

0

q0

1

0

Page 12: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 12

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.

Page 13: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 13

Algorithm 1. Transform all rules of the grammar to following

form: Ax or AxB where x is either or and BV

(by adding variables to the grammar).2. The start state of NFA is the grammar's start

symbol S.3. For each rule: AB construct a state transition from A to B labeled . AB construct a state transition from A to B labeled .

A B

A B

Page 14: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 14

Algorithm (cont.)

4. Add a new state F for each rule A and construct a state transition

from A to the new state F

labeled .

5. The final states of the NFA are F together with all A such that there is a rule A.

A F

Page 15: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 15

Example

S 0S | 11A

A 1A | 0

 

New grammar is:

S 0S | 1B

B 1A

A 1A | 0

Page 16: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 16

Example (cont.)

• The resulting NFA is:

S B1

0

1 A

1

0 F

Page 17: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 17

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.

Page 18: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 18

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)

Page 19: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 19

Regular grammars

• Restricted to rules as:Sa or SaT

where a and S,TV (different from our definition - a*)

• Generates regular languages.• These languages are exactly all languages

that can be decided by a FA .

Page 20: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 20

Context-free grammars

• Restricted to rules as:

Ab, AV and b(VU)*

• Generates context-free languages.

• These languages are exactly all languages that can be decided by a nondeterministic pushdown automaton PDA.

Page 21: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 21

Context-sensitive grammars

• Restricted to rules as:α A β α γ β AV and α , β, γ (VU)*

• Generates context-sensitive languages.• These languages are exactly all languages

that can be decided by a linear-bounded nondeterministic Turing machine BTM.

Page 22: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 22

Example

• L={anbn| n>=1}

CSG G:

S aSBC

S abC

CB BC

bB bb

bC b

S

a S B C

a a b C B C

a a b B C

a a b b C

a a b b

Page 23: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 23

Recursively enumerablegrammar

• 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.

Page 24: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 24

Relations between types

• Every regular language is context-free.

• Every context-free language is context-sensitive.

• Every context-sensitive language is recursively enumerable.

• These are all proper inclusions.

Page 25: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 25

Operations over grammars

We can perform the

– union

– concatenation– Kleene star

operations over grammars.

Page 26: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 26

Union

Given two languages and their grammars:

• L1 with G1= (V1,1,S1,R1) and

• L2 with G2 = (V2,2,S2,R2)

we construct their union by merging their grammars:

• G = (V1V2S, 12, S, R1R2{SS1| S2})

21 VV

Page 27: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 27

Union (cont.)

• The rule

SS1 | S2

• means that a string w in L(G) can be derived either from S1 or from S2.

Page 28: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 28

Concatenation

Given two languages and their grammars:

• L1 with G1= (V1,1,S1,R1) and

• L2 with G2 = (V2,2,S2,R2)

we construct their concatenation :

• G = (V1V2S, 12, S, R1R2{SS1S2})

21 VV

Page 29: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 29

Concatenation (cont.)

• The rule

SS1S2

• 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.

Page 30: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 30

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 {SS1S|})

Page 31: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 31

Kleene star (cont.)

• The rule SS1S

• 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.

Page 32: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 32

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: AB.

Page 33: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 33

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.

Page 34: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 34

How to find the set of all variables A such that A*?

VariablesNullableofsettheisN

NNUNTIL

Nofelementsofstringaisand

ruleproductionaisAANN

ii

REPEAT

i

ruleproductionaisAVAN

i

ii

i

ii

1

1

1

0

}

|{

1

0

}|{

Variable A is called nullable if *?

Page 35: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 35

Algorithm for removing rules

1. Find the set of all variables A such that A*.

2. For each rule of G of the form Bw where w(V)* create all possible new rules Bw' 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).

Page 36: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 36

Algorithm for removing rules

3. The resulting grammar consists of the original rules together with new one constructed at step 2 minus -rules.

Page 37: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 37

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 .

Page 38: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 38

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

Page 39: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 39

Example

S aBaC

B bB | C

C cC |

Variables C and B can derive .

• C• BC

Page 40: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 40

Example (cont.)• So we will delete B and C from the right side of

the rules:

Original grammar New rules

S aBaC SaaC | aBa | aa

B bB Bb

B C BC cC Cc

C none

Page 41: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 41

Example (cont.)

• The resulting grammar after removing -rules is:

S aBaC | aaC | aBa | aa

B bB | b | C

C cC | c

Page 42: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 42

Removing unit rules

• A context-free grammar that contains unit rules can be rewritten without unit rules.

• Unit rule: A B

Page 43: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 43

Algorithm for removing unit rules

1. For each unit rule AB , delete this rule from the grammar and add all productions of B to A.

( If Bw then after deleting AB a rule Aw should be added to A rules.)

2. Repeat step 1 until all unit rules are removed.

Page 44: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 44

ExampleSA | b

A B | b

B bB | a

First we will eliminate AB unit rule.• Ab (old rule)• AbB | a (rules of B)• AbB | a | b (union of old and new rules)

Page 45: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 45

Example (cont.)

• The resulting grammar is:

SA | b

A bB | a | b

B bB | a

Page 46: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 46

Example (cont.)• Now we will eliminate SA unit rule.Sb (old rule)S bB | a | b (rules of A)SbB | a | b (union of old and new rules) The resulting grammar is:S bB | a | b A bB | a | bB bB | a

Page 47: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 47

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

Page 48: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 48

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

Page 49: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 49

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:ABC orA

where and B,CV.• If a language contains then the S rule is

allowed.

Page 50: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 50

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.

Page 51: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 51

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.

Page 52: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 52

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 Aa to CFG.

4.2. For each rule of the form AB1B2..Bn where n3 replace it with the following rules:

AB1C and C B2..Bn

• Continue this step till all the rules have the proper form (right-hand side of length2).

Page 53: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 53

Example

Write the following grammar in Chomsky normal form.

S A | 0B0

A S | 1

B A | 0

Page 54: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 54

Example (cont.)• We will add the S'S rule.

• There is no -rules.

• Unit rules are SA, AS, BA, S'S.

• We will start from AS

A 1 (old rule)

A A | 0B0 (new rules : the rule AA has no meaning) so we leave A 0B0 | 1

Page 55: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 55

Example (cont.)

• Now we eliminate BA.

B 0 (old rule)

B 0B0 | 1 (new rule)

together B 0B0 | 1 | 0

Page 56: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 56

Example (cont.)• Now we eliminate SA.

S 0B0 (old rule)

S 0B0 | 1 (new rule)

together S 0B0 | 1

• Now we eliminate S'S.

S' 0B0 | 1 (new rule)

Page 57: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 57

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

Page 58: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 58

Example (cont.)

• Now we will write all rules in Chomsky form.

S'1 | S0BS0

B S0BS0 | 1 | 0

S0 0

Page 59: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 59

Example (cont.)

• We will replace the S0BS0 right side with S0C and CBS0

• The resulting grammar is:

S'1 | S0C

B S0C | 1 | 0

CBS0

S0 0

Page 60: Complexity and Computability Theory I

Rina Zviel-Girshin @ASC 60

Any Questions?