Download - CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University
![Page 1: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/1.jpg)
1
CDT314
FABER
Formal Languages, Automata and Models of Computation
Lecture 6
Mälardalen University
2010
![Page 2: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/2.jpg)
2
Content
Midterm againContext-Free LanguagesPush-Down Automata, PDANPDA: Non-Deterministic PDAFormal Definitions for NPDAs NPDAs Accept Context-Free LanguagesConverting NPDA to Context-Free Grammar
![Page 3: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/3.jpg)
Midterm
• 5 out of 13 passed…• Many failed on basic definitions
• Is something wrong with the course format?• Scheduled exercises• Don’t hesitate to ask for help!• Do labs, exercises etc.• Review lecture material before lectures• Strongly recommended to get a book if you
don’t have one• It doesn’t get easier…
3
![Page 4: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/4.jpg)
4
JFLAP demo
Concerning examination in the course:Exercises are voluntaryLabs are voluntaryMidterms are voluntaryLectures are voluntary…All of them are recommended!
http://www.cs.duke.edu/csed/jflap/movies
![Page 5: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/5.jpg)
5
Regular Languages
}{ nnba }{ Rww
Context-Free Languages
Non-regular languages
}0:{ ! nan}0,:{ lncba lnln
![Page 6: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/6.jpg)
6
Context-Free Languages
![Page 7: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/7.jpg)
7
Context-Free Languages
Pushdown
Automata
Context-Free
Grammars
stack
automaton
(CF grammars are
defined as generalized Regular Grammars)
![Page 8: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/8.jpg)
8
Definition: Context-Free Grammars
Grammar
Productions of the form:xA
x is string of variables and terminals
),,,( PSTVG
Variables Terminal
symbols
Start
variables
![Page 9: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/9.jpg)
9
Pushdown AutomataPDAs
![Page 10: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/10.jpg)
10
Pushdown Automaton - PDA
Input String
Stack
States
![Page 11: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/11.jpg)
11
The Stack
The stack allows pushdown automata to recognize some non-regular languages.
All access to the stack only on the top!
(Stack top is written leftmost in the string, e.g. yxz)
A PDA can write symbols on stack and read them later on.
A stack is valuable as it can hold an unlimited amount of (certain kind of) information.
zxy
POP reading symbol PUSH writing symbol
![Page 12: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/12.jpg)
12
The States
Input
symbol
Pop old- reading
stack symbol
Push new- writing
stack symbol
q1 q2cba /,
![Page 13: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/13.jpg)
13
a
b top
input
stack
a
Replace
eh
$eh
$
c
q1 q2cba /,
(An alternative is to start and finish with empty stack or a stack bottom symbol such as $)
![Page 14: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/14.jpg)
14
a a
input
Pushb
eh
$eh
$
bc
top
stack
q1 q2ca /,
![Page 15: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/15.jpg)
15
a a input
Popb
eh
$eh
$
top
stack
q1 q2/,ba
![Page 16: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/16.jpg)
16
a a input
No Changeb
eh
$eh
$
btop
stack
q1 q2 /,a
![Page 17: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/17.jpg)
17
NPDAsNon-deterministic Push-Down Automata
![Page 18: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/18.jpg)
18
Non-Determinism
q1
q2cba /,
q3
q1 q2
transitioncba /,
cb /,
![Page 19: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/19.jpg)
19
A string is accepted if:
• All the input is consumed• The last state is a final state• Stack is in the initial condition (either: empty (when we started with empty stack), or: bottom symbol reached, or similar)
![Page 20: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/20.jpg)
20
}0:{ nbaL nn
is the language accepted by the NPDA:
Example NPDA
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
},{ ba
symbol)stack initial :($
![Page 21: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/21.jpg)
21
/,
/,
bb
aa
q1 q2 /,q0
NPDA M
}{)( RwwML
bb
aa
/,
/,
$/$,
(Even-length palindromes)
},{ ba
Example NPDA
![Page 22: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/22.jpg)
22
Pushing Strings
q1 q2wba /,
Input
symbol
Pop
symbolPush
string
![Page 23: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/23.jpg)
23
q1 q2cdfba /,
a
b top
input
stack
a
Push
eh h
e
cdf
pushed
string
Example
$ $
![Page 24: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/24.jpg)
24
Another NPDA example
$0/$,a00/0,a/1,a
$1/$,b11/1,b/0,b
$/$,q1 q2
NPDA M
}:{)( ba nnwML
},{ ba
![Page 25: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/25.jpg)
25
Time 0
Input
a ab b ba
$
Stack
Current state
q1 q2
Execution Example
$0/$,a00/0,a/1,a
$1/$,b11/1,b/0,b
$/$,
![Page 26: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/26.jpg)
26
Input
a ab b ba
Stack
$0
q1 q2
$0/$,a00/0,a/1,a
$1/$,b11/1,b/0,b
$/$,
Time 1
![Page 27: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/27.jpg)
27
Input
a bb b a
Stack
a
q1 q2
$0
$0/$,a00/0,a/1,a
$1/$,b11/1,b/0,b
$/$,
Time 2
![Page 28: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/28.jpg)
28
Input
a bb b a
Stack
a
$1
q1 q2
$0/$,a00/0,a/1,a
$1/$,b11/1,b/0,b
$/$,
Time 3
![Page 29: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/29.jpg)
29
Input
a bb b a
Stack
a
$11
q1 q2
$0/$,a00/0,a/1,a
$1/$,b11/1,b/0,b
$/$,
Time 4
![Page 30: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/30.jpg)
30
Input
a bb b a
Stack
a
q1 q2
$11
$0/$,a00/0,a/1,a
$1/$,b11/1,b/0,b
$/$,
Time 5
![Page 31: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/31.jpg)
31
Input
a bb b a
Stack
a
q1 q2
$1
$0/$,a00/0,a/1,a
$1/$,b11/1,b/0,b
$/$,
Time 6
![Page 32: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/32.jpg)
32
Input
a bb b a a
$
Stack
q1 q2
accept
$0/$,a00/0,a/1,a
$1/$,b11/1,b/0,b
$/$,
Time 7
![Page 33: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/33.jpg)
33
Formal Definitions for NPDAs
![Page 34: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/34.jpg)
34
q1 q2wba /,
)},{(),,( 21 wqbaq
Transition function
![Page 35: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/35.jpg)
35
Transition function
q1
q2wba /,
q3
wba /,
current state
)},(),,{(),,( 321 wqwqbaq
current input symbol
current stack top
new state
new stack top
An unspecified transition function is to the null set and represents a dead configuration for the NPDA.
![Page 36: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/36.jpg)
36
Formal Definition
Non-Deterministic Pushdown Automaton NPDA
),,,,,( FzQM States
Input
alphabetStack
alphabet
Transition
function
Final
states
Stack
start
symbol
![Page 37: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/37.jpg)
37
Instantaneous Description
),,( suq
Current
state Remaining
input
Current
stack
contents
![Page 38: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/38.jpg)
38q0 q1 q2 q3
Input
Stack
a a a b b b$
aaTime 4:
Example Instantaneous Description
$),,( 1 aaabbbq
a
/, /, ab $/$,
/, abaa /,
),,( stackinputremainingstateurrentc
![Page 39: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/39.jpg)
39q0 q1 q2 q3
Input
Stack
a a a b b b$
aaTime 5:
Example Instantaneous Description
$),,( 2 aabbq
a
/, /, ab $/$,
/, abaa /,
),,( stackinputremainingstateurrentc
![Page 40: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/40.jpg)
40
We write
$),,($),,( 21 aabbqaaabbbq Time 4 Time 5
),,( contentstackcurrentinputremainingstatecurrent
![Page 41: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/41.jpg)
41
$),,($),,($),,($),,(
$),,($),,($),,(
$),,($),,(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
A computation example
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
),,( contentstackcurrentinputremainingstatecurrent
![Page 42: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/42.jpg)
42
$),,($),,($),,($),,(
$),,($),,($),,(
$),,($),,(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
),,( contentstackcurrentinputremainingstatecurrentA computation example
![Page 43: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/43.jpg)
43
$),,($),,($),,($),,(
$),,($),,($),,(
$),,($),,(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
),,( contentstackcurrentinputremainingstatecurrentA computation example
![Page 44: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/44.jpg)
44
$),,($),,($),,($),,(
$),,($),,($),,(
$),,($),,(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
),,( contentstackcurrentinputremainingstatecurrentA computation example
![Page 45: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/45.jpg)
45
$),,($),,($),,($),,(
$),,($),,($),,(
$),,($),,(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
),,( contentstackcurrentinputremainingstatecurrentA computation example
![Page 46: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/46.jpg)
46
$),,($),,($),,($),,(
$),,($),,($),,(
$),,($),,(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
),,( contentstackcurrentinputremainingstatecurrentA computation example
![Page 47: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/47.jpg)
47
$),,($),,($),,($),,(
$),,($),,($),,(
$),,($),,(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
),,( contentstackcurrentinputremainingstatecurrentA computation example
![Page 48: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/48.jpg)
48
$),,($),,($),,($),,(
$),,($),,($),,(
$),,($),,(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
),,( contentstackcurrentinputremainingstatecurrentA computation example
![Page 49: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/49.jpg)
49
For convenience we write
$),,($),,( 30 qaaabbbq
$),,($),,($),,($),,(
$),,($),,($),,(
$),,($),,(
3222
111
10
qqabqaabbq
aaabbbqaaabbbqaaabbbq
aaabbbqaaabbbq
),,( contentstackcurrentinputremainingstatecurrent
![Page 50: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/50.jpg)
50
Formal Definition
Language of NPDA M
)}',,(),,(:{)( 0 sqswqwML f
Initial state Final state
![Page 51: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/51.jpg)
51
Example ,$),(,$),( 30 qaaabbbq
q0 q1 q2 q3
NPDA M
)(MLaaabbb
/, /, ab $/$,
/, abaa /,
![Page 52: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/52.jpg)
52
,$),(,$),( 30 qbaq nn
)(MLba nn
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,NPDA M
![Page 53: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/53.jpg)
53
}0:{)( nbaML nnTherefore:
q0 q1 q2 q3 /, /, ab $/$,
/, abaa /,
NPDA M
![Page 54: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/54.jpg)
54
NPDAs Accept Context-Free Languages
![Page 55: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/55.jpg)
55
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Theorem
![Page 56: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/56.jpg)
56
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Proof - Step 1:
Convert any context-free grammar G to a NPDA M
with L(G) = L(M)
![Page 57: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/57.jpg)
57
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Proof - Step 2:
Convert any NPDA M to a context-free grammar G with L(M) = L(G)
![Page 58: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/58.jpg)
58
Converting Context-Free Grammars
to NPDAs
![Page 59: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/59.jpg)
59
An example grammar:
T
TaT
bS
aSTbS
What is the equivalent NPDA?
![Page 60: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/60.jpg)
60
Grammar NPDA
/,
/,
/,
/,
T
TaT
bS
aSTbS
T
TaT
bS
aSTbS
/,
/,
bb
aa
q0 q1 2qS/, $/$,
![Page 61: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/61.jpg)
61
The NPDA simulates
leftmost derivations of the grammar
L(Grammar) = L(NPDA)
![Page 62: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/62.jpg)
62
Grammar:
T
TaT
bS
aSTbS
A leftmost derivation:
abababTabbTabTbSaS
![Page 63: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/63.jpg)
63
NPDA execution:
0q q1 2qS/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
Input
Stack
$
a ab
Time 0
b
Start
![Page 64: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/64.jpg)
64
q0 q1 2q
Input
Stack
a ab
Time 1
b
$S
S/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
![Page 65: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/65.jpg)
65
q0 2q
Input
Stack
a ab
Time 2
b
$
a
b
ST
q1S/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
![Page 66: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/66.jpg)
66
q0 2q
Input
Stack
a ab
Time 3
b
$
a
b
ST
q1S/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
![Page 67: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/67.jpg)
67
q0 2q
Input
Stack
a ab
Time 4
b
$bTb
q1S/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
![Page 68: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/68.jpg)
68
q0 2q
Input
Stack
a ab
Time 5
b
$bTb
q1S/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
![Page 69: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/69.jpg)
69
q0 2qS, , $ $
bb
aa
,
,
Input
Stack
a ab
Time 6
b
T
TaT
bS
aSTbS
,
,
,
, $b
Ta
q1
![Page 70: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/70.jpg)
70
q0 2q
Input
Stack
a ab
Time 7
b
$b
Ta
q1S/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
![Page 71: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/71.jpg)
71
q0 2q
Input
Stack
a ab
Time 8
b
$ba
q1S/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
![Page 72: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/72.jpg)
72
q0 2q
Input
Stack
a ab
Time 9
b
$b
q1S/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
![Page 73: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/73.jpg)
73
q0 q1 2q
Input
Stack
$
a ab
Time 10
b
acceptS/, $/$,
/,
/,
/,
/,
T
TaT
bS
aSTbS
/,
/,
bb
aa
![Page 74: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/74.jpg)
74
In general
Given any grammar Gwe can construct a NPDA M with
)()( MLGL
![Page 75: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/75.jpg)
75
q0 q1 2qS/, $/$,
wA /, /, aa
For any productionwA
For any terminala
Constructing NPDA M from grammar GTop-down parser
![Page 76: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/76.jpg)
76
Grammar G generates string w
if and only if
NPDA M accepts w
)()( MLGL
![Page 77: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/77.jpg)
77
For any context-free language
there is an NPDA
that accepts the same language
![Page 78: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/78.jpg)
78
Which means
Languages
Accepted by
NPDAs
Context-
FreeLanguages(Grammars)
![Page 79: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/79.jpg)
79
Converting NPDAsto
Context-Free Grammars
![Page 80: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/80.jpg)
80
For any NPDA M
we will construct
a context-free grammar G with
)()( GLML
![Page 81: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/81.jpg)
81
in NPDA M
abcABCabcS
Input processed Stack contents
terminals variables
GA derivation in Grammar
The grammar simulates the machine
![Page 82: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/82.jpg)
82
Some Simplifications
First we modify the NPDA so that
• It has a single final state qf and
• It empties the stack
when it accepts the input
Original NPDA Empty Stack
fq
![Page 83: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/83.jpg)
83
Second we modify the NPDA transitions.
All transitions will have form:
iq jq/, Ba
or
iq jqCDBa /,
which means that each move
increases/decreases stack by a single symbol.
![Page 84: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/84.jpg)
84
Those simplifications do not affect generality of our argument.
It can be shown that for any NPDA there exists an equivalent one having above two properties
i.e.the equivalent NPDA with a single final state which empties
its stack when it accepts the input, and which for each move increases/decreases stack by a single symbol.
![Page 85: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/85.jpg)
85
/1,
00/0,
$0/$,
a
a
a
/$,0q fq
}:{)( ba nnwML
Example of a NPDA in an appropriate form
symbol)stack initial :($/0,
11/1,
$1/$,
b
b
b},{ ba
![Page 86: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/86.jpg)
86
The Grammar Construction
)( jiBqq
In grammar G
Terminals:
Input symbols of NPDA
states
Stack symbol
Variables:
![Page 87: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/87.jpg)
87
iq jq/, BaFor each transition:
we add production: aBqq ji )(
![Page 88: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/88.jpg)
88
For each transition:
we add production: ))(()( klljki DqqCqqaBqq
iq jqCDBa /,
for all states qk , ql
![Page 89: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/89.jpg)
89
Start Variable )$( fo qq
Stack bottom symbol
Start state (Single) Final state
![Page 90: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/90.jpg)
90
Example
aqq )1( 00Grammar production:
0q fq
/1,
00/0,
$0/$,
a
a
a
/$,
/0,
11/1,
$1/$,
b
b
biq jq/, Ba
aBqq ji )(
}:{)( ba nnwML },{ ba
![Page 91: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/91.jpg)
91
)$)(1(|)$)(1()$(
)$)(1(|)$)(1()$(
00000
00000000
fffff
ff
qqqqbqqqqbqq
qqqqbqqqqbqq
Grammar productions:
0q fq
/1,
00/0,
$0/$,
a
a
a
/$,
/0,
11/1,
$1/$,
b
b
b iq jqCDBa /,
))(()( klljki DqqCqqaBqq
lk qqstatesallfor ,
![Page 92: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/92.jpg)
92
Grammar production: )$( 0 fqq
0q fq
/1,
00/0,
$0/$,
a
a
a
/$,
/0,
11/1,
$1/$,
b
b
biq jq/, Ba
aBqq ji )(
![Page 93: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/93.jpg)
93
0q fq
/1,
00/0,
$0/$,
a
a
a
/$,
/0,
11/1,
$1/$,
b
b
b
Resulting Grammar
ablestart vari:)$( 0 fqq
)$)(1(|)$)(1()$(
)$)(1(|)$)(1()$(
00000
00000000
fffff
ff
qqqqbqqqqbqq
qqqqbqqqqbqq
)1)(1(|)1)(1()1(
)1)(1(|)1)(1()1(
00000
00000000
fffff
ff
qqqqbqqqqbqq
qqqqbqqqqbqq
}:{)( ba nnwML },{ ba
))(()( klljki DqqCqqaBqq
lk qqstatesallfor ,
![Page 94: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/94.jpg)
94
)$)(0(|)$)(0()$(
)$)(0(|)$)(0()$(
00000
00000000
fffff
ff
qqqqaqqqqaqq
qqqqaqqqqaqq
Resulting Grammar, cont.
)0)(0(|)0)(0()0(
)0)(0(|)0)(0()0(
00000
00000000
fffff
ff
qqqqaqqqqaqq
qqqqaqqqqaqq
0q fq
/1,
00/0,
$0/$,
a
a
a
/$,
/0,
11/1,
$1/$,
b
b
b }:{)( ba nnwML
![Page 95: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/95.jpg)
95
bqq
aqq
)0(
)1(
00
00
)$( 0 fqq0q fq
/1,
00/0,
$0/$,
a
a
a
/$,
/0,
11/1,
$1/$,
b
b
b
Resulting Grammar, cont.
iq jq/, Ba
aBqq ji )(
}:{)( ba nnwML
![Page 96: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/96.jpg)
96
Derivation of string abba
)$( 0 fqq )$)(0( 000 fqqqqa
)$( 0 fqqab
)$)(1( 000 fqqqqabb
)$( 0 fqqabba
abba
0q fq
/1,
00/0,
$0/$,
a
a
a
/$,
/0,
11/1,
$1/$,
b
b
b
iq jq/, Ba
aBqq ji )(
![Page 97: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/97.jpg)
97
In general, in grammar:
wqq f
)$( 0
if and only if
w is accepted by the NPDA
![Page 98: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/98.jpg)
98
Explanation
By construction of Grammar:
wAqq ji
)(
if and only if
in the NPDA going from qi to qj
the stack doesn’t change below
and A is removed from stack
iq jq/, Ba
aBqq ji )(
![Page 99: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/99.jpg)
99
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
We have shown the procedure to convert
any NPDA M to a context-free
grammar G with L(M) = L(G)which means
![Page 100: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/100.jpg)
100
Context-Free
Languages
(Grammars)
Languages
Accepted by
NPDAs
Therefore
END OF PROOF
![Page 101: CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University](https://reader034.vdocuments.us/reader034/viewer/2022050719/56814ee4550346895dbc75ab/html5/thumbnails/101.jpg)
101
q0 q1
Example (Sudkamp 8.1.2)
/, Ab
/,
/, AbAa /,
}0:{}0:{)( ibaiaML iii
Language consisting solely of a’s or an equal number of a´s and b´s.
q2 /, A