230 likes | 240 Views
Fall 2008. The Chinese University of Hong Kong. CSC 3130: Automata theory and formal languages. Pushdown automata. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. Motivation. We had two ways to describe regular languages How about context-free languages?. regular expression.
E N D
Fall 2008 The Chinese University of Hong Kong CSC 3130: Automata theory and formal languages Pushdown automata Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130
Motivation • We had two ways to describe regular languages • How about context-free languages? regularexpression NFA DFA syntactic computational CFG pushdown automaton syntactic computational
Pushdown automata versus NFA • Since context-free is more powerful than regular, pushdown automata must generalize NFAs state control input 0 1 0 0 NFA
Pushdown automata • A pushdown automaton has access to a stack, which is a potentially infinite supply of memory state control input 0 1 0 0 … stack pushdown automaton (PDA)
Pushdown automata • As the PDA is reading the input, it can push / pop symbols in / out of the stack state control input 0 1 0 0 … Z0 0 1 1 stack pushdown automaton (PDA)
Rules for pushdown automata • The transitions are nondeterministic • Stack is always accessed from the top • Each transition can pop a symbol from the stack and / or push another symbol onto the stack • Transitions depend on input symbol and on last symbol popped from stack • Automaton accepts if after reading whole input, it can reach an accepting state
Example state control L = {0n#1n: n ≥ 0} read 0 input push a 0 0 0 # 1 1 1 read # read 1 … Z0 a a a pop a stack pop Z0
Shorthand notation 0, e / a read 0 push a #, e / e read # 1, a / e read 1 pop a e, Z0 / e pop Z0 read, pop / push
Formal definition A pushdown automaton is (Q, ,, , q0, Z0, F): • Q is a finite set of states; • is the input alphabet; • is the stack alphabet, including a special symbol Z0; • q0in Q is the initial state; • Z0in is the start symbol; • F Q is a set of final states; • is the transition function d: Q ( {}) ( {}) → subsets of Q (G {}) state input symbol pop symbol state push symbol
Notes on definition • We use slightly different definition than textbook • Example d(q0, 0, e) = {(q0, a)} 0, e / a 1, a / e d(q0, 1, e) = ∅ d(q0, #, e) = {(q1, e)} q0 q1 q2 d(q0, 0, a) = ∅ #, e / e e, Z0 / e ... d: Q ( {}) ( {}) → subsets of Q (G {})
A convention • Sometimes we denote “transitions” by: • This will mean: • Intuitively, pop b, then push c1, c2, and c3 q0 q1 a, b/ c1c2c3 q0 q1 a, b/ c1 e, e/ c2 e, e/ c3 intermediatestates
Examples • Describe PDAs for the following languages: • L = {w#wR: w ∈ {0, 1}*}, S = {0, 1, #} • L = {wwR: w ∈ S*}, S = {0, 1} • L = {w: w has same number of 0s and 1s}, S = {0, 1} • L = {0i1j: i ≤ j ≤ 2i}, S = {0, 1}
Main theorem A language L is context-free if and only if it is accepted by some pushdown automaton. context-free grammar pushdown automaton
From CFGs to PDAs • Idea: Use PDA to simulate (rightmost) derivations CFG: A → 0A1A → BB → # A 0A1 00A11 00B11 00#11 input: PDA control: stack: 00#11 write start variable e, e / A Z0A 00#11 e, A / 1A0 Z01A0 replace production in reverse 0#11 pop terminals and match 0, 0 / e Z01A 0#11 e, A / 1A0 Z011A0 replace production in reverse #11 pop terminals and match 0, 0 / e Z011A #11 e, A / B Z011B replace production in reverse
From CFGs to PDAs • If, after reading whole input, PDA ends up with an empty stack, derivation must be valid • Conversely, if there is no valid derivation, PDA will get stuck somewhere • Either unable to match next input symbol, • Or match whole input but stack non empty
Description of PDA for CFGs • Repeat the following steps: • If the top of the stack is a variable A:Choose a rule A → a and substitute A with a • If the top of the stack is a terminal a:Read next input symbol and compare to aIf they don’t match, reject (die) • If top of stack is Z0, go to accept state
Description of PDA for CFGs a, a / e e, A / ak...a1 for every terminal a for every production A → a1...ak e, e / S e, Z0 / e q2 q1 q0
From PDAs to CFGs • First, we simplify the PDA: • It has a single accept stateqf • Z0 is always popped exactly before accepting • Each transition is either a push, or a pop, but not both ✓ context-free grammar pushdown automaton
From PDAs to CFGs • We look at the stack in an accepting computation: input 0 0 e 1 1 e 0 1 e e 0 q0 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7 qf state a b a c c c a a a a a a a a a stack Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 portions that preserve the stack A03 = {x: x leads from q0 to q3 and preserves stack}
From PDAs to CFGs input 0 0 e 1 1 e 0 1 e e 0 q0 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7 qf state a b a c c c a a a a a a a a a stack Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 A11 0 e A11 → 0A03e A03
From PDAs to CFGs input 0 0 e 1 1 e 0 1 e e 0 q0 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7 qf state a b a c c c a a a a a a a a a stack Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 A10 A03 A13 A13 → A10A03
From PDAs to CFGs Aij variables: qi’ a, e / t A0f start variable: qi qj b, t /e Aij → aAi’j’b qj’ Aik → AijAjk qi qj qk Aii → e qi a, Z0 /e A0f → A0ia qf qi
Example 0, e / a 0, e / a 1, a / e 1, a / e q0 q1 q2 q0 q1 q2 q3 #, e / e e, Z0 / e #, e / $ e, Z0 / e e, $ /e start variable: A02 productions: A00 → e A01 → 0A011 A02 → A01 A00 → A00A00 A11 → e A01 → #A33 A00 → A01A10 A22 → e A00 → A03A30 A33 → e A01 → A01A11 A01 → A02A21 ...