Download presentation
Presentation is loading. Please wait.
Published byAmi Foster Modified over 6 years ago
1
Formal Languages, Automata and Models of Computation
CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2005
2
Content Context-Free Languages Push-Down Automata, PDA NPDA: Non-Deterministic PDA Formal Definitions for NPDAs NPDAs Accept Context-Free Languages Converting NPDA to Context-Free Grammar
3
Non-regular languages
Context-Free Languages Regular Languages
4
Context-Free Languages
5
Context-Free Languages
Grammars Pushdown Automata stack automaton (CF grammars are defined as generalized Regular Grammars)
6
Definition: Context-Free Grammars
Variables Terminal symbols Start variables Productions of the form: is string of variables and terminals
7
Definition: Regular Grammars
Variables Terminal symbols Start variables Right or Left Linear Grammars. Productions of the form: or is string of terminals
8
Pushdown Automata PDAs
9
Pushdown Automaton - PDA
Input String Stack States
10
A PDA can write symbols on stack and read them later on.
The Stack A PDA can write symbols on stack and read them later on. POP reading symbol PUSH writing symbol All access to the stack only on the top! (Stack top is written leftmost in the string, e.g. yxz) A stack is valuable as it can hold an unlimited amount of information. The stack allows pushdown automata to recognize some non-regular languages.
11
The States Pop old reading Push new Input stack symbol writing symbol
12
input stack top Replace
(An alternative is to start and finish with empty stack)
13
input Push top stack
14
input Pop top stack
15
input No Change top stack
16
Example 3.7 Salling: Input Time 0
A PDA for simple nested parenthesis strings Time 0 Input Stack
17
Example 3.7 Time 1 Input Stack
18
Example 3.7 Time 2 Input Stack
19
Example 3.7 Time 3 Input Stack
20
Example 3.7 Time 4 Input Stack
21
Example 3.7 Time 5 Input Stack
22
Example 3.7 Time 6 Input Stack
23
Example 3.7 Time 7 Input Stack
24
NPDAs Non-deterministic Push-Down Automata
25
Non-Determinism
26
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)
27
Example NPDA is the language accepted by the NPDA:
28
Example NPDA NPDA (Even-length palindromes)
29
Pushing Strings Pop symbol Input symbol Push string
30
Example: input pushed string stack top Push
31
Another NPDA example NPDA
32
Execution Example: Time 0 Input Stack Current state
33
Time 1 Input Stack
34
Time 2 Input Stack
35
Time 3 Input Stack
36
Time 4 Input Stack
37
Time 5 Input Stack
38
Time 6 Input Stack
39
Time 7 Input Stack accept
40
Formal Definitions for NPDAs
41
Transition function:
42
Transition function: new state current state current stack top
new stack top current input symbol An unspecified transition function is to the null set and represents a dead configuration for the NPDA.
43
Formal Definition Non-Deterministic Pushdown Automaton NPDA Final
States Input alphabet Stack Transition function Final states start symbol
44
Instantaneous Description
Current state Current stack contents Remaining input
45
Instantaneous Description
Example Instantaneous Description Input Time 4: Stack
46
Instantaneous Description
Example Instantaneous Description Input Time 5: Stack
47
We write Time 4 Time 5
48
A computation
49
A computation
50
A computation
51
A computation
52
A computation
53
A computation
54
A computation
55
A computation
56
For convenience we write
57
Formal Definition Language of NPDA Initial state Final state
58
Example: NPDA :
59
NPDA :
60
Therefore: NPDA :
61
NPDAs Accept Context-Free Languages
62
Theorem Context-Free Languages (Grammars) Accepted by NPDAs
63
Proof - Step 1: Context-Free Languages (Grammars) Languages Accepted by NPDAs Convert any context-free grammar to a NPDA with
64
Proof - Step 2: Context-Free Languages (Grammars) Languages Accepted by NPDAs Convert any NPDA to a context-free grammar with:
65
Converting Context-Free Grammars to NPDAs
66
An example grammar: What is the equivalent NPDA?
67
Grammar NPDA
68
The NPDA simulates leftmost derivations of the grammar L(Grammar) = L(NPDA)
69
Grammar: A leftmost derivation:
70
NPDA execution: Time 0 Input Stack Start
71
Time 1 Input Stack
72
Time 2 Input Stack
73
Time 3 Input Stack
74
Time 4 Input Stack
75
Time 5 Input Stack
76
Time 6 Input Stack
77
Time 7 Input Stack
78
Time 8 Input Stack
79
Time 9 Input Stack
80
Time 10 Input Stack accept
81
In general Given any grammar We can construct a NPDA With
82
Constructing NPDA from grammar
Top-down parser For any production For any terminal
83
Grammar generates string
if and only if NPDA accepts
84
Therefore: For any context-free language there is an NPDA that accepts the same language
85
Which means Context-Free Languages (Grammars) Accepted by NPDAs
86
Converting NPDAs to Context-Free Grammars
87
For any NPDA we will construct a context-free grammar with
88
The grammar simulates the machine
A derivation in Grammar in NPDA Input processed Stack contents terminals variables
89
First we modify the NPDA so that It has a single final state
Some Simplifications First we modify the NPDA so that It has a single final state It empties the stack when it accepts the input Original NPDA Empty Stack
90
Second we modify the NPDA transitions. All transitions will have form:
which means that each move increases/decreases stack by a single symbol.
91
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.
92
Example of a NPDA in an appropriate form
93
The Grammar Construction
In grammar Stack symbol Variables: states Terminals: Input symbols of NPDA
94
For each transition: we add production:
95
For each transition: we add production: for all states
96
Stack bottom symbol Start Variable Start state (Single) Final state
97
Example Grammar production:
98
Grammar productions:
99
Grammar production:
100
Resulting Grammar
101
Resulting Grammar, cont.
102
Resulting Grammar, cont.
103
Derivation of string
104
In general, in grammar: if and only if is accepted by the NPDA
105
Explanation By construction of Grammar: if and only if in the NPDA going from to the stack doesn’t change below and is removed from stack
106
We have shown the procedure to convert
any NPDA to a context-free grammar with: which means Context-Free Languages (Grammars) Accepted by NPDAs
107
Therefore Context-Free Languages Languages Accepted by (Grammars)
NPDAs END OF PROOF
108
Example (Sudkamp 8.1.2) Language consisting solely of a’s or an equal number of a´s and b´s.
109
When scanning an a in state q0 two transitions are possible
When scanning an a in state q0 two transitions are possible. A string of the form aibi is accepted by a computation in states q0 and q1.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.