18
CSCI 2670 Introduction to Theory of Computing September 20, 2005

CSCI 2670 Introduction to Theory of Computing September 20, 2005

Embed Size (px)

Citation preview

Page 1: CSCI 2670 Introduction to Theory of Computing September 20, 2005

CSCI 2670Introduction to Theory of

Computing

September 20, 2005

Page 2: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Agenda

• Last week– Context-free grammars

• Examples, definition, strategies for building

• This week– More on CFG’s

• Chomsky normal form, Pushdown automata, pumping lemma for CFG’s

Page 3: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Announcement

• Recommended problems to do prior to next Tuesday (9/27)– 2.8, 2.9, 2.12, 2.13, 2.15, 2.16, 2.30 d

• I will post solutions to these problems on Friday

• Midterm next Tuesday– Chapters 1 & 2

• I will hand out practice midterm tomorrow and solutions the next day

• I will hold extra office hours on Friday 11:00 – 12:30

Page 4: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Chomsky normal form

• Method of simplifying a CFGDefinition: A context-free grammar is in

Chomsky normal form if every rule is of one of the following forms

A BCA a

where a is any terminal and A is any variable, and B, and C are any variables or terminals other than the start variable

if S is the start variable thenthe rule S ε is the only permitted rule

Page 5: CSCI 2670 Introduction to Theory of Computing September 20, 2005

CFG’s and Chomsky normal form

Theorem: Any context-free language is generated by a context-free grammar in Chomsky normal form.

Proof idea: Convert any CFG to one in Chomsky normal form by removing or replacing all rules in the wrong form

1. Add a new start symbol2. Eliminate ε rules of the form A ε3. Eliminate unit rules of the form A B4. Convert remaining rules into proper form

Page 6: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Convert a CFG to Chomsky normal form

1. Add a new start symbol- Create the following new rule

S0 S

where S is the start symbol and S0 is not used in the CFG

Page 7: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Convert a CFG to Chomsky normal form

2. Eliminate all ε rules A ε, where A is not the start variable

- For each rule with an occurrence of A on the right-hand side, add a new rule with the A deleted

R uAv becomes R uAv | uvR uAvAw becomes R uAvAw | uvAw | uAvw |

uvw

- If we have R A, replace it with R ε unless we had already removed R ε

Page 8: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Convert a CFG to Chomsky normal form

3. Eliminate all unit rules of the form A B

- For each rule B u, add a new rule A u, where u is a string of terminals and variables, unless this rule had already been removed

- Repeat until all unit rules have been replaced

Page 9: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Convert a CFG to Chomsky normal form

4. Convert remaining rules into proper form

- What’s left?

- Replace each rule A u1u2…uk, where k 3 and ui is a variable or a terminal with k-1 rules

A u1A1 A1 u2A2 … Ak-2 uk-1uk

Page 10: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Example

S S1 | S2

S1 S1b | Ab

A aAb | ab | εS2 S2a | Ba

B bBa | ba| ε

Step 1: Add a new start symbol

Page 11: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Example

S0 S

S S1 | S2

S1 S1b | Ab

A aAb | ab | εS2 S2a | Ba

B bBa | ba | ε

Step 2: Eliminate ε rules

Page 12: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Example

S0 S

S S1 | S2

S1 S1b | Ab | b

A aAb | abS2 S2a | Ba | a

B bBa | ba

Step 3: Eliminate all unit rules

Page 13: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Example

S0 S1b | Ab | b | S2a | Ba | a

S S1b | Ab | b | S2a | Ba | a

S1 S1b | Ab | b

A aAb | abS2 S2a | Ba | a

B bBa | ba

Step 4: Convert remaining rules to proper form

Page 14: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Example

S0 S1b | Ab | b | S2a | Ba | a

S S1b | Ab | b | S2a | Ba | a

S1 S1b | Ab | b

A aA1 | ab

A1 Ab

S2 S2a | Ba | a

B bB1 | ba

B1 Ba

Page 15: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Pushdown automata

• Similar to finite automata, but for CFG’s

• Finite automata are not adequate for CFG’s because we cannot keep track of what we’ve done– At any point, we only know the

current state, not previous states

• Need memory– PDA’s are finite automata with a stack

Page 16: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Finite automata and PDA schematics

State control

a a b b

State control

a a b b

xyz

FA

PDA

Stack: Infinite LIFO (last in first out) device

Page 17: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Example

read 0 & push 0 on stack

read ε & push ε on stack

read ε & push $ on stack

read 1 & pop 0 off stack

read ε & pop $ off stack

Language accepted: {0n1n | n 0}

Page 18: CSCI 2670 Introduction to Theory of Computing September 20, 2005

Differences between PDA’s and NFA’s

• Transitions read a symbol of the string and push a symbol onto or pop a symbol off of the stack

• Stack alphabet is not necessarily the same as the alphabet for the language– e.g., $ marks bottom of stack in

previous (0n1n) example