Download presentation
Presentation is loading. Please wait.
Published byBonnie Cannon Modified over 8 years ago
1
Formal Languages, Automata and Models of Computation
CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2012
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
Based on C Busch, RPI, Models of Computation
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
Pushdown Automata PDAs
8
Pushdown Automaton - PDA
Input String Stack States
9
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 is 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 (but it is not random access!). The stack allows pushdown automata to recognize some non-regular languages.
10
The States Pop old - reading Push new Input symbol stack symbol
- writing stack symbol Input symbol
11
input stack top Replace
(An alternative is to either start and finish with empty stack or with a stack bottom symbol such as $)
12
input Push top stack
13
input Pop top stack
14
input No Change top stack
15
NPDAs Non-deterministic Push-Down Automata
16
Non-Determinism
17
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, depending on convention)
18
Example NPDA is the language accepted by the NPDA:
19
Example NPDA NPDA M (Even-length palindromes)
Example : aabaaabbblbbbaaabaa
20
Pushing Strings Pop symbol Input symbol Push string
21
Example input pushed string stack top Push
22
Another NPDA example NPDA M
23
Execution Example Time 0 Input Stack Current state
24
Time 1 Input Stack
25
Time 2 Input Stack
26
Time 3 Input Stack
27
Time 4 Input Stack
28
Time 5 Input Stack
29
Time 6 Input Stack
30
Time 7 Input Stack accept
31
Formal Definitions for NPDAs
32
Transition function
33
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.
34
Formal Definition Non-Deterministic Pushdown Automaton NPDA Final
States Input alphabet Stack Transition function Final states start symbol
35
Instantaneous Description
Current stack contents Current state Remaining input
36
Instantaneous Description
Example Instantaneous Description Input Time 4: Stack
37
Instantaneous Description
Example Instantaneous Description Input Time 5: Stack
38
We write Time 4 Time 5
39
A computation example
40
A computation example
41
A computation example
42
A computation example
43
A computation example
44
A computation example
45
A computation example
46
A computation example
47
For convenience we write
A computation example For convenience we write
48
Formal Definition Language of NPDA M Initial state Final state
49
Example NPDA M
50
NPDA M
51
Therefore: NPDA M
52
NPDAs Accept Context-Free Languages
53
Theorem Context-Free Languages (Grammars) Accepted by NPDAs
54
Proof - Step 1: Context-Free Languages Languages Accepted by
(Grammars) Languages Accepted by NPDAs Convert any context-free grammar G to a NPDA M with L(G) = L(M)
55
Proof - Step 2: Context-Free Languages Languages Accepted by
(Grammars) Languages Accepted by NPDAs Convert any NPDA M to a context-free grammar G with L(M) = L(G)
56
Converting Context-Free Grammars to NPDAs
57
An example grammar: What is the equivalent NPDA?
58
Grammar NPDA For each production add transition: For each terminal
59
L(Grammar) = L(NPDA) The NPDA simulates
the leftmost derivations of the grammar L(Grammar) = L(NPDA)
60
Grammar: A leftmost derivation:
61
NPDA execution: Time 0 Input Stack Start
62
Time 1 Input Stack
63
Time 2 Input Stack
64
Time 3 Input Stack
65
Time 4 Input Stack
66
Time 5 Input Stack
67
Time 6 Input Stack
68
Time 7 Input Stack
69
Time 8 Input Stack
70
Time 9 Input Stack
71
Time 10 Input Stack accept
72
In general Given any grammar G we can construct a NPDA M with
73
Constructing NPDA M from grammar G
Top-down parser For any production For any terminal
74
Grammar G generates string w
if and only if NPDA M accepts w
75
For any context-free language
there is an NPDA that accepts the same language
76
Context-Free Languages (Grammars)
Which means Languages Accepted by NPDAs Context-Free Languages (Grammars)
77
Converting NPDAs to Context-Free Grammars
78
For any NPDA M we will construct a context-free grammar G with
79
The grammar simulates the machine
A derivation in Grammar terminals variables Input processed Stack contents in NPDA M
80
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
81
Second we modify the NPDA transitions. All transitions will have form:
which means that each move increases/decreases stack by a single symbol.
82
Those simplifications do not affect generality of our argument.
It can be shown that for any NPDA there exists an equivalent one having the 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.
83
The Grammar Construction
In grammar G Terminals: Input symbols of NPDA states Stack symbol Variables:
84
For each transition: we add production:
85
For each transition: we add production: for all states qk , ql
86
Stack bottom symbol Start Variable Start state (Single) Final state
87
From NPDA to CFG, in short:
When we write a grammar, we can use any variable names we choose. As in programming languages, we like to use "meaningful" variable names. Translating an NPDA into a CFG, we will use variable names that encode information about both the state of the NPDA and the stack contents. Variable names will have the form [qiAqj], where qi and qj are states and A is a variable. The "meaning" of the variable [qiAqj] is that the NPDA can go from state qi with Ax on the stack to state qj with x on the stack. Each transition of the form (qi, a, A) = (qj,l ) results in a single grammar rule.
88
From NPDA to CFG Each transition of the form (qi, a, A) = (qj, BC) results in a multitude of grammar rules, one for each pair of states qx and qy in the NPDA. This algorithm results in a lot of useless (unreachable) productions, but the useful productions define the context-free grammar recognized by the NPDA. using JFLAP
89
For any NPDA there is an context-free grammar that generates the same language.
90
We have the procedure to convert
any NPDA M to a context-free grammar G with L(M) = L(G) which means: Context-Free Languages (Grammars) Accepted by NPDAs
91
Context-Free Languages (Grammars)
We have already shown that for any context-free language there is an NPDA that accepts the same language. That is: Languages Accepted by NPDAs Context-Free Languages (Grammars)
92
Therefore: Context-Free Languages (Grammars) Accepted by NPDAs END OF PROOF
93
An example of a NPDA in an appropriate form
94
Example Grammar production:
95
Grammar productions:
96
Grammar production:
97
Resulting Grammar
98
Resulting Grammar, cont.
99
Resulting Grammar, cont.
100
Derivation of string
101
In general, in grammar: if and only if is accepted by the NPDA
102
Explanation By construction of Grammar: if and only if
in the NPDA going from qi to qj the stack doesn’t change below and A is removed from stack
103
Example (Sudkamp 8.1.2) Language consisting solely of a’s or an equal number of a´s and b´s.
104
Concerning examination in the course:
Exercises are voluntary Labs are voluntary Midterms are voluntary Lectures are voluntary… All of them are recommended! JFLAP demo
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.