MTH401A Theory of Computation Lecture 17
Chomsky Normal Form for CFG s
Chomsky Normal Form for CFG s For every context free language, L, the language L {ε} has a grammar in which every production looks like, either 1. S AB, or 2. B a
Chomsky Normal Form for CFG s For every context free language, L, the language L {ε} has a grammar in which every production looks like, either 1. S AB, or 2. B a Further, the grammar has no useless symbols.
Chomsky Normal Form for CFG s 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.
Chomsky Normal Form for CFG s Example : Eliminate Variables S 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
Chomsky Normal Form for CFG s 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.
Chomsky Normal Form for CFG s Example : Eliminate Useless Symbols S 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.
Chomsky Normal Form for CFG s 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 A is nullable.
Chomsky Normal Form for CFG s 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 C A 0A 0 B 1B 1
Chomsky Normal Form for CFG s 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 C A 0A 0 B 1B 1 C is now useless. Eliminate its productions
Chomsky Normal Form for CFG s 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 C A 0A 0 B 1B 1 C is now useless. Eliminate its productions
Chomsky Normal Form for CFG s 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).
Chomsky Normal Form for CFG s Example : Unit Productions S A B CC DD B BOZO CC ABC 0 - - -
Chomsky Normal Form for CFG s Example : Unit Productions S A B CC DD B BOZO CC ABC 0 - - - Unit Productions sequences S * B
Chomsky Normal Form for CFG s Example : Unit Productions S A B CC DD BOZO CC ABC 0 B BOZO CC ABC 0 - - - S A B CC DD B BOZO CC ABC 0 - - -
Chomsky Normal Form for CFG s Example : Unit Productions S A 11 A B 1 B S 0
Chomsky Normal Form for CFG s Example : Unit Productions S A 11 A B 1 B S 0 Unit Productions sequences S * A, S * B; A * B, A * S; B * S, B * A;
Chomsky Normal Form for CFG s Example 5 : Useless Symbols S A 11 1 0 A B 1 0 11 B S 0 11 1 S A 11 A B 1 B S 0
Chomsky Normal Form for CFG s Example 5 : Useless Symbols S A 11 1 0 A B 1 0 11 B S 0 11 1 A and B are useless symbols. No way to get them from S. S A 11 A B 1 B S 0
Chomsky Normal Form for CFG s 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.
Chomsky Normal Form for CFG s 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 A a and a production A a a. Replace a by A a 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.
Chomsky Normal Form for CFG s Example : Terminals S 0A1B DEF A CC
Chomsky Normal Form for CFG s Example : Terminals S 0A1B DEF A CC Z 0 O 1
Chomsky Normal Form for CFG s Example : Terminals S ZAOB DEF A CC Z 0 O 1
Chomsky Normal Form for CFG s Example : Long productions S ZAOB DEF A CC Z 0 O 1
Chomsky Normal Form for CFG s Example : Long productions S ZAOB DEF A CC Z 0 O 1 Too long.
Chomsky Normal Form for CFG s Example : Long productions S ZAOB DEF A CC Z 0 O 1 Too long. New group of symbols.
Chomsky Normal Form for CFG s Example : Long productions S ZM DEF A CC Z 0 O 1 M AOB
Chomsky Normal Form for CFG s Example : Long productions S ZM DEF A CC Z 0 O 1 M AOB New group of symbols.
Chomsky Normal Form for CFG s Example : Long productions S ZM DH A CC Z 0 O 1 M AN N OB H EF
Chomsky Normal Form for CFG s 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.
Pushdown Machines
Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0
Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 Finite State Machine
Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 Finite State Machine
Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0
Pushdown Machines 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 Finite Pushdown State Machine
Formalism : Pushdown Machines 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 (q 0, in Q, typically). 6. A start symbol (Z 0 in Γ, typically). 7. A set of final states (F Q, typically).
Convention : Pushdown Machines 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.
Pushdown Machines The Transition Function :
Pushdown Machines 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 Γ.
Pushdown Machines 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.
Pushdown Machines 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 α.
Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : n 1}.
Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : n 1}. The states :
Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : n 1}. The states : 1. q = start state. We are in state q if we have seen only 0 s so far.
Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : 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.
Example : Pushdown Machines Construct a pushdown machine to accept {0 n 1 n : 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.
Pushdown Machines Example (continued) : The stack symbols :
Pushdown Machines Example (continued) : The stack symbols : 1. Z 0 = start symbol. Also marks the bottom of the stack.
Pushdown Machines Example (continued) : The stack symbols : 1. Z 0 = start symbol. Also marks the bottom of the stack. 2. X = marker, used to count the number of 0 s seen on the input.
Pushdown Machines Example (continued) : The transitions :
Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}.
Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}. 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.
Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}. 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.
Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}. 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.
Pushdown Machines Example (continued) : The transitions : 1. δ(q, 0, Z 0 ) = {(q, XZ 0 )}. 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, ε, Z 0 ) = {(f, Z 0 )}. Accept at bottom.
Pushdown Machines Example (continued) : In action Z 0 0 0 0 1 1 1 q
Pushdown Machines Example (continued) : In action Z 0 0 0 0 1 1 1 q δ(q, 0, Z 0 )
Pushdown Machines Example (continued) : In action Z 0 0 0 0 1 1 1 q δ(q, 0, Z 0 ) = {(q, XZ 0 )}
Pushdown Machines Example (continued) : In action X 0 0 1 1 1 Z 0 q
Pushdown Machines Example (continued) : In action X 0 0 1 1 1 Z 0 q δ(q, 0, X)
Pushdown Machines Example (continued) : In action X 0 0 1 1 1 Z 0 q δ(q, 0, X) = {(q, XX)}
Pushdown Machines Example (continued) : In action X X 0 1 1 1 Z 0 q
Pushdown Machines Example (continued) : In action X X 0 1 1 1 Z 0 q δ(q, 0, X)
Pushdown Machines Example (continued) : In action X X 0 1 1 1 Z 0 q δ(q, 0, X) = {(q, XX)}
Pushdown Machines Example (continued) : In action X X X 1 1 1 Z 0 q
Pushdown Machines Example (continued) : In action X X X 1 1 1 Z 0 q δ(q, 1, X)
Pushdown Machines Example (continued) : In action X X X 1 1 1 Z 0 q δ(q, 1, X) = {(p, ε)}
Pushdown Machines Example (continued) : In action X X 1 1 Z 0 p
Pushdown Machines Example (continued) : In action X X 1 1 Z 0 p δ(p, 1, X)
Pushdown Machines Example (continued) : In action X X 1 1 Z 0 p δ(p, 1, X) = {(p, ε)}
Pushdown Machines Example (continued) : In action X 1 Z 0 p
Pushdown Machines Example (continued) : In action X 1 Z 0 p δ(p, 1, X)
Pushdown Machines Example (continued) : In action X 1 Z 0 p δ(p, 1, X) = {(p, ε)}
Pushdown Machines Example (continued) : In action Z 0 p
Pushdown Machines Example (continued) : In action Z 0 p δ(p, ε, Z 0 )
Pushdown Machines Example (continued) : In action Z 0 p δ(p, ε, Z 0 ) = {(f, Z 0 )}
Pushdown Machines Example (continued) : In action Z 0 f
Pushdown Machines 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.
Pushdown Machines The Goes-To relation: To say that ID I can become ID J in one move of the pushdown machine, we write I J.
Pushdown Machines 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, β).
Pushdown Machines 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.