Download presentation
Presentation is loading. Please wait.
Published bySusanna Rodgers Modified over 8 years ago
1
Theory of Computation Automata Theory Dr. Ayman Srour
2
4. Introduction We had two ways to describe regular languages How about context-free languages? regular expression DFANFA syntactic computational CFGpushdown automaton syntactic computational
3
4.1 Introduction Pushdown automata versus NFA Since context-free is more powerful than regular, pushdown automata must generalize NFAs state control 0100 input NFA
4
4.1 Introduction Pushdown automata A pushdown automaton has access to a stack, which is a potentially infinite supply of memory state control 0100 input pushdown automaton (PDA) … stack
5
4.1 Introduction Pushdown automata As the PDA is reading the input, it can push / pop symbols in / out of the stack state control 0100 input pushdown automaton (PDA) Z0Z0 01 stack … 1
6
4.2 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
7
4.3 Formal definition
8
4.3 Example 1
11
We write "a,b -> c" to signify that when the machine is reading an a from the input it may replace the symbol b on the top of the stack with a c. Any of a, b, and c may be ε. If a is ε, the machine may make this transition without reading any symbol from the input. If b is ε, the machine may make this transition without reading and popping any symbol from the stack. If c is ε, the machine does not write any symbol on the stack when going along this transition.
12
4.3 Example 2 Describe PDAs for the following languages: L = {w#w R : w ∈ *}, = {0, 1, #} L = {ww R : w ∈ *}, = {0, 1} L = {w: w has same number of 0s and 1s}, = {0, 1} L = {0 i 1 j : i ≤ j ≤ 2i}, = {0, 1}
13
4.4 Main theorem A language L is context-free if and only if it is accepted by some pushdown automaton. context-free grammarpushdown automaton
14
4.4.1 From CFGs to PDAs Step 1: e, e->$ : push the stack symbol at the start state q start. Step 2; push the stating variable at the top of the stack. Step 3: at q loop push the variable and terminals at the right hand side of all production starting from the rightmost symbols. First replace the start symbol with the rightmost symbol and keep pusing the rset of the symbols from right to left. Step 4: keep poping terminals from the productions. Step 5: at q accept pub the stack symbol e,$->e.
15
4.4.1 From CFGs to PDAs/Example construct a PDA PI from the following CFG G. S - >aTb | b T – >Ta | E
16
4.4.1 From CFGs to PDAs/Example construct a PDA PI from the following CFG G. S - >aTb | b T – >Ta | e
17
4.4.1 From CFGs to PDAs Idea: Use PDA to simulate (rightmost) derivations A → 0A1 A → B B → # A 0A1 00A11 00B11 00#11 PDA control: CFG: write start variable stack: Z0AZ0A replace production in reverse Z 0 1A0 pop terminals and match Z 0 1A e, e / A 0, 0 / e e, A / 1A0 input: 00#11 0#11 replace production in reverse Z 0 11A0e, A / 1A0 0#11 pop terminals and match Z 0 11A0, 0 / e #11 replace production in reverse Z 0 11Be, A / B #11
18
4.4.1 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
19
4.4.1 From CFGs to PDAs Repeat the following steps: If the top of the stack is a variable A : Choose a rule A → and substitute A with If the top of the stack is a terminal a : Read next input symbol and compare to a If they don’t match, reject (die) If top of stack is Z 0, go to accept state
20
4.4.1 From CFGs to PDAs q0q0 q1q1 q2q2 , / S a, a / for every terminal a , A / k... 1 for every production A → 1... k , Z 0 /
21
4.4.2 From PDAs to CFGs First, we simplify the PDA: It has a single accept state q f Z 0 is always popped exactly before accepting Each transition is either a push, or a pop, but not both context-free grammarpushdown automaton ✓
22
4.4.2 From PDAs to CFGs a Z0Z0 Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 Z0Z0 baccc a q0q0 q1q1 q3q3 q1q1 q7q7 q1q1 q2q2 q1q1 q7q7 11 01 0 00 input state stack A 11 A 03 0 A 11 → 0A 03 q0q0 q3q3 qfqf
23
4.4.2 From PDAs to CFGs a Z0Z0 Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 a Z0Z0 Z0Z0 baccc a q0q0 q1q1 q3q3 q1q1 q7q7 q1q1 q2q2 q1q1 q7q7 11 01 0 00 input state stack A 03 A 13 A 13 → A 10 A 03 q0q0 q3q3 A 10 qfqf
24
4.4.2 From PDAs to CFGs qiqi qjqj a, / t b, t / q i’ q j’ A ij → aA i’j’ b qiqi qjqj qkqk A ik → A ij A jk qiqi A ii → variables: A ij qfqf a, Z 0 / qiqi A 0f → A 0i a start variable: A0fA0f
25
4.4.2 From PDAs to CFGs 0, / a #, / , Z 0 / 1, a / q0q0 q1q1 q2q2 start variable: A 02 productions: A 00 → A 00 A 00 A 00 → A 01 A 10 A 00 → A 03 A 30 A 01 → A 01 A 11 A 01 → A 02 A 21 A 00 → ... A 11 → A 22 → A 01 → 0A 01 1 A 01 → #A 33 A 33 → 0, / a #, / $ , Z 0 / 1, a / q0q0 q1q1 q2q2 q3q3 , $ / A 02 → A 01
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.