Lecture 17 - IITK - Indian Institute of Technology...

84
MTH401A Theory of Computation Lecture 17

Transcript of Lecture 17 - IITK - Indian Institute of Technology...

Page 1: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

MTH401ATheory of Computation

Lecture 17

Page 2: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Page 3: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

For every context free language, L, the language L – {ε} has a grammar in which every production looks like, either

1. S → AB , or2. B → a

Chomsky Normal Form for CFG’s

Akash Anand

Page 4: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

For every context free language, L, the language L – {ε} has a grammar in which every production looks like, either

1. S → AB , or2. B → a

Further, the grammar has no useless symbols.

Chomsky Normal Form for CFG’s

Akash Anand

Page 5: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Eliminating useless symbols :• A symbol is useful if it appears in some

derivation of some terminal string from the start symbol.

• It is useless, otherwise. Eliminate all useless symbols by :1. Eliminate symbols that derive no

terminal string.2. Eliminate unreachable symbols.

Page 6: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate VariablesS → AB | C A → 0A | 0 B → 1B C → 1

• Basis : A and C are identified because of A → 0 and C → 1.

• Induction : S is identified because of S →C.

• Nothing else can be identified.

• S → C A → 0A | 0 C → 1

Page 7: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Algorithm to eliminate unreachable symbols :

1. Basis : We can reach S (the start symbol).

2. Induction : If we can reach A and A → αthen we can reach all symbols in α.

3. Algorithm : Remove from the grammar all symbols not reachable from S and all productions that involve these symbols.

Page 8: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate Useless SymbolsS → AB | 0 A → C | 0 B → 0B C → 1

1. If we eliminate unreachable symbols first we would find that everything is reachable.

2. A, C, 1 would never get eliminated.

Page 9: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Discovering Nullable Symbols :1. To eliminate all ε- productions, we first

need to discover all nullable variables (variables A such that A⇒*ε).

2. Basis : If there is a production A → ε then A is nullable.

3. Induction : If there is a production A → αand all symbols in α are nullable then Ais nullable.

Page 10: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate ε- Productions S → ABC A → 0A |ε B → 1B |ε C →ε

1. A, B, C, S are nullable.2. New grammar

S → ABC | AB | AC | BC | A | B | CA → 0A | 0B → 1B | 1

Page 11: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate ε- Productions S → ABC A → 0A |ε B → 1B |ε C →ε

1. A, B, C, S are nullable.2. New grammar

S → ABC | AB | AC | BC | A | B | CA → 0A | 0B → 1B | 1

C is now useless. Eliminate its productions

Page 12: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Eliminate ε- Productions S → ABC A → 0A |ε B → 1B |ε C →ε

1. A, B, C, S are nullable.2. New grammar

S → ABC | AB | AC | BC | A | B | CA → 0A | 0B → 1B | 1

C is now useless. Eliminate its productions

Page 13: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Unit Productions :• A unit production is one whose right side

consists of exactly one variable.• Algorithm : Find all pairs (A,B) such that

A ⇒* B by a sequence of unit productions only.

• Basis : Pairs (A,A) are there.• Induction : If we have found (A,B) and B

→ C is a unit production then add (A,C).

Page 14: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Unit ProductionsS → …A → B | CC | DDB → BOZO | CC | ABC | 0

- - -

Page 15: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Unit ProductionsS → …A → B | CC | DDB → BOZO | CC | ABC | 0

- - -

Unit Productions sequences

S ⇒* B

Page 16: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Unit ProductionsS → …

A → B | CC | DD | BOZO | CC | ABC | 0B → BOZO | CC | ABC | 0

- - -S → …A → B | CC | DD

B → BOZO | CC | ABC | 0

- - -

Page 17: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Unit ProductionsS → A | 11A → B | 1B → S | 0

Page 18: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Unit ProductionsS → A | 11A → B | 1B → S | 0

Unit Productions sequences

S ⇒* A, S ⇒* B;

A ⇒* B, A ⇒* S;

B⇒* S, B ⇒* A;

Page 19: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example 5 : Useless SymbolsS → A | 11 | 1 | 0A → B | 1 | 0 | 11B → S | 0 | 11 | 1

S → A | 11A → B | 1B → S | 0

Page 20: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example 5 : Useless SymbolsS → A | 11 | 1 | 0A → B | 1 | 0 | 11B → S | 0 | 11 | 1

S → A | 11A → B | 1B → S | 0

A and B are useless symbols. No way to get

them from S.

Page 21: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Cleaning Up a Grammar :• Theorem : If L is a CFL, then there is a

CFG for L – {ε} that has

• No useless symbols.• No ε- productions.

• No unit productions.• In other words, every right side is either

a single terminal or has length ≥ 2.

Page 22: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Chomsky Normal Form Steps:Step 1 : Clean up the grammar.Step 2 : For each right side that is not a single

terminal, make it all variables.• For each terminal symbol a, create a new variable

Aa and a production Aa → a.

• Replace a by Aa in right sides of length ≥ 2.

Step 3 : Break right sides of length longer than 2 into a chain of productions with right sides of two variables.

Page 23: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : TerminalsS → 0A1B | DEFA → CC

Page 24: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : TerminalsS → 0A1B | DEFA → CCZ → 0O → 1

Page 25: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : TerminalsS → ZAOB | DEFA → CCZ → 0O → 1

Page 26: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZAOB | DEFA → CCZ → 0O → 1

Page 27: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZAOB | DEFA → CCZ → 0O → 1

Too long.

Page 28: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZAOB | DEFA → CCZ → 0O → 1

Too long.

New group of symbols.

Page 29: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZM | DEFA → CCZ → 0O → 1M → AOB

Page 30: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZM | DEFA → CCZ → 0O → 1M → AOB

New group of symbols.

Page 31: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Example : Long productionsS → ZM | DHA → CCZ → 0O → 1M → AN

N → OBH → EF

Page 32: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Chomsky Normal Form for CFG’s

Akash Anand

Chomsky Normal Form :• CFG is said to be in a Chomsky Normal Form if

the grammar has no useless symbols and every production is of one of these two forms :1. A → BC (right side is two variables),2. A → a (right side is a single terminal).

• Theorem : If L is a CFL, then L – {ε} has a CFG in Chomsky normal form.

Page 33: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Page 34: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

0 1 0 1 1 1 0 1 0 0 0 0 0 0 0

Page 35: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Finite State Machine

0 1 0 1 1 1 0 1 0 0 0 0 0 0 0

Page 36: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

0 1 0 1 1 1 0 1 0 0 0 0 0 0 0

Finite State Machine

Page 37: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

0 1 0 1 1 1 0 1 0 0 0 0 0 0 0

Page 38: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Finite State Machine

Pushdown Machine

0 1 0 1 1 1 0 1 0 0 0 0 0 0 0

Page 39: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Formalism :A pushdown machine is described by:

1. A finite set of states (Q, typically).2. An input alphabet (Σ, typically).3. A stack alphabet (Γ, typically).4. A transition function (δ, typically).5. A start state (q0, in Q, typically).6. A start symbol (Z0 in Γ, typically).7. A set of final states (F ⊆ Q, typically).

Page 40: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Convention :• a, b, c, … are input symbols.• Sometimes, we also allow ε as a possible

value.• …, X, Y, Z are stack symbols.• …, w, x, y, z are strings of input symbols.• α, β, γ, … are strings of stack symbols.

Page 41: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

The Transition Function :

Page 42: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

The Transition Function :• Takes three input arguments.

1. A state, in Q.2. An input, which is either a symbol in Σ or ε.

3. A stack symbol in Γ.

Page 43: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

The Transition Function :• Takes three input arguments.

1. A state, in Q.2. An input, which is either a symbol in Σ or ε.

3. A stack symbol in Γ.• δ(q, a, Z) is a set of zero or more actions

of the form (p, α).• p is a state and α is a string of stack

symbols.

Page 44: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Actions of The Pushdown Machine :• If δ(q, a, Z) contains (p, α) among its

actions, then one thing the pushdown machine can do in state q, with a at the front of the input, and Z on top of the stack is :1. Change the state to p.2. Remove a from the front of the input (a

may be ε).3. Replace Z on the top of the stack by α.

Page 45: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example :• Construct a pushdown machine to

accept {0n1n : n ≥ 1}.

Page 46: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example :• Construct a pushdown machine to

accept {0n1n : n ≥ 1}.• The states :

Page 47: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example :• Construct a pushdown machine to

accept {0n1n : n ≥ 1}.• The states :

1. q = start state. We are in state q if we have seen only 0’s so far.

Page 48: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example :• Construct a pushdown machine to

accept {0n1n : n ≥ 1}.• The states :

1. q = start state. We are in state q if we have seen only 0’s so far.

2. p = we have seen at least one 1 and may now proceed only if the inputs are 1’s.

Page 49: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example :• Construct a pushdown machine to

accept {0n1n : n ≥ 1}.• The states :

1. q = start state. We are in state q if we have seen only 0’s so far.

2. p = we have seen at least one 1 and may now proceed only if the inputs are 1’s.

3. f = final state; accept.

Page 50: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example (continued) :• The stack symbols :

Page 51: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example (continued) :• The stack symbols :

1. Z0 = start symbol. Also marks the bottom of the stack.

Page 52: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example (continued) :• The stack symbols :

1. Z0 = start symbol. Also marks the bottom of the stack.

2. X = marker, used to count the number of 0’s seen on the input.

Page 53: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example (continued) :• The transitions :

Page 54: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example (continued) :• The transitions :

1. δ(q, 0, Z0) = {(q, XZ0)}.

Page 55: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example (continued) :• The transitions :

1. δ(q, 0, Z0) = {(q, XZ0)}.2. δ(q, 0, X) = {(q, XX)}. These two rules

make sure that one X is pushed into the stack for each 0 read from the input.

Page 56: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example (continued) :• The transitions :

1. δ(q, 0, Z0) = {(q, XZ0)}.2. δ(q, 0, X) = {(q, XX)}. These two rules

make sure that one X is pushed into the stack for each 0 read from the input.

3. δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X.

Page 57: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example (continued) :• The transitions :

1. δ(q, 0, Z0) = {(q, XZ0)}.2. δ(q, 0, X) = {(q, XX)}. These two rules

make sure that one X is pushed into the stack for each 0 read from the input.

3. δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X.

4. δ(p, 1, X) = {(p, ε)}. Pop one X per 1.

Page 58: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Example (continued) :• The transitions :

1. δ(q, 0, Z0) = {(q, XZ0)}.2. δ(q, 0, X) = {(q, XX)}. These two rules

make sure that one X is pushed into the stack for each 0 read from the input.

3. δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X.

4. δ(p, 1, X) = {(p, ε)}. Pop one X per 1.5. δ(p, ε, Z0) = {(f, Z0)}. Accept at bottom.

Page 59: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

Example (continued) : In action

0 0 0 1 1 1

q

Page 60: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

Example (continued) : In action

0 0 0 1 1 1

q

δ(q, 0, Z0)

Page 61: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

Example (continued) : In action

0 0 0 1 1 1

q

δ(q, 0, Z0)δ(q, 0, Z0) = {(q, XZ0)}

Page 62: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

Example (continued) : In action

0 0 1 1 1

q

Page 63: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

Example (continued) : In action

0 0 1 1 1

q

δ(q, 0, X)

Page 64: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

Example (continued) : In action

0 0 1 1 1

q

δ(q, 0, X)δ(q, 0, X) = {(q, XX)}

Page 65: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

X

Example (continued) : In action

0 1 1 1

q

Page 66: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

X

Example (continued) : In action

0 1 1 1

q

δ(q, 0, X)

Page 67: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

X

Example (continued) : In action

0 1 1 1

q

δ(q, 0, X)δ(q, 0, X) = {(q, XX)}

Page 68: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

X

X

Example (continued) : In action

1 1 1

q

Page 69: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

X

X

Example (continued) : In action

1 1 1

q

δ(q, 1, X)

Page 70: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

X

X

Example (continued) : In action

1 1 1

q

δ(q, 1, X)δ(q, 1, X) = {(p, ε)}

Page 71: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

X

Example (continued) : In action

1 1

p

Page 72: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

X

Example (continued) : In action

1 1

p

δ(p, 1, X)

Page 73: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

X

Example (continued) : In action

1 1

p

δ(p, 1, X)δ(p, 1, X) = {(p, ε)}

Page 74: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

Example (continued) : In action

1

p

Page 75: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

Example (continued) : In action

1

p

δ(p, 1, X)

Page 76: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

X

Example (continued) : In action

1

p

δ(p, 1, X)δ(p, 1, X) = {(p, ε)}

Page 77: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

Example (continued) : In action

p

Page 78: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

Example (continued) : In action

p

δ(p, ε, Z0)

Page 79: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

Example (continued) : In action

p

δ(p, ε, Z0)δ(p, ε, Z0) = {(f, Z0)}

Page 80: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Z0

Example (continued) : In action

f

Page 81: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

Instantaneous Descriptions :• We can formalize the pictures just seen

with an instantaneous description (ID).• An ID is a 3-tuple (q, w, α), where:

1. q is the current state.2. w is the remaining input.3. α is the stack contents, top at the left.

Page 82: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

The “Goes-To” relation:• To say that ID I can become ID J in one

move of the pushdown machine, we write I ⊦ J.

Page 83: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

The “Goes-To” relation:• To say that ID I can become ID J in one

move of the pushdown machine, we write I ⊦ J.

• Formally, (q, aw, Xα) ⊦ (p, w, βα), for any w and α, if δ(q, a, X) contains (p, β).

Page 84: Lecture 17 - IITK - Indian Institute of Technology Kanpurhome.iitk.ac.in/~akasha/mth401/lectures/Lecture17.pdf ·  · 2016-11-11Theory of Computation Lecture 17. ... the grammar

Pushdown Machines

Akash Anand

The “Goes-To” relation:• To say that ID I can become ID J in one

move of the pushdown machine, we write I ⊦ J.

• Formally, (q, aw, Xα) ⊦ (p, w, βα), for any w and α, if δ(q, a, X) contains (p, β).

• Extend ⊦ to ⊦*, meaning “zero or more moves” by • Basis : I ⊦* I.• Induction : If I ⊦* J and J ⊦ K, then I ⊦* K.