automata theory and formal languages 2015 … · 2015-11-27 · automata theory and formal...
TRANSCRIPT
AUTOMATA THEORY
AND FORMAL LANGUAGES
2015-16
David Griol Barres Universidad Carlos III de Madrid
UNIT 6: PUSH-DOWN AUTOMATA
Bibliography
Enrique Alfonseca Cubero, Manuel Alfonseca Cubero, Roberto
Moriyón Salomón. Teoría de autómatas y lenguajes formales.
McGraw-Hill (2007). Chapter 4 and Section 8.1.
John E. Hopcroft, Rajeev Motwani, Jeffrey D.Ullman.
Introduction to Automata Theory, Languages, and Computation
(3rd edition). Ed, Pearson Addison Wesley. Chapter 6.
Manuel Alfonseca, Justo Sancho, Miguel Martínez Orga. Teoría
de lenguajes, gramáticas y autómatas. Publicaciones R.A.E.C.
1997. Chapter 10.
Outline
Introduction
Definition of Push-Down Automata
Acceptance in final states or when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a Push-Down Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
Introduction. Language Hierarchy 4
General
Languages
Context-Dependent
Languages Context-Free
Languages
Non-deterministic
Regular
Languages Languages
accepted by
deterministic
push-down automata
which empty the stack
Introduction 5
Introduction 6
Limitations of FA’s:
Only repetition sentences can be recognized.
E. g. anbn, anbncn
It is not possible to determine if a program is correct.
It is not possible to determine syntax errors present in
natural language.
Introduction 7
Limitations of FA’s: Explanation.
Lack of Memory
Mathematical expressions cannot be recognized,
e.g. “(2x+(2+n/25))”, nested paired brackets, language XnYn ( (
) )
q r s ....
Push-Down Automata and Languages 8
Function: Analyze words to know if they belong to
Type-2 languages: Accept or not accept.
Same structure that a finite automata adding a stack
(auxiliary memory).
Push-Down Automata and Languages 9
Theorems:
For each context-free grammar G, there is a push-down
automaton M that fulfills L(G)=L(M)
For each push-down automata M, there is a context-free
grammar G that fulfills L(M)=L(G)
There are context-free languages that cannot be
recognized by any deterministic push-down automaton.
Outline
Introduction
Definition of Push-Down Automata
Acceptance in final states
Acceptance when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a Push-Down Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
Definition of Push-Down Automaton 11
Ao
...
...
B
/ 2 ) 5 )
Q
CONTROL
OF STATES
Input tape
Movement of the tape
Transitions are based on:
• Current state.
• Symbol in the input tape;
• Symbol on the top of the stack.
Stack
Definition of Push-Down Automaton 12
Ao
...
...
B
Stack
/ 2 ) 5 )
Q
CONTROL
OF STATES
Movement of the tape
It is also possible to change
the state and modify the
content of the stack without
reading an input symbol, nor
to advance the reading head
of the tape.
Input tape
Definition of Push-Down Automaton 13
Ao
...
...
B
/ 2 ) 5 )
Q
CONTROL
OF STATES
Movement of the tape
The PD automaton can
select between several
possible actions in each one
of the corresponding steps.
Input tape
Stack
Outline
Introduction
Definition of Push-Down Automata
Acceptance in final states or when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a Push-Down Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
Acceptation in final states 15
1 0 1 1 0
Ao
B
... Qf
INPUT TAPE
Stack
Movement of the tape Stack NOT necessarily
empty
Word read
Acceptation when the stack is empty 16
1 0 1 1 0
Qj
INPUT TAPE
Stack
Movement of the tape
Word read
Stack necessarily empty
Outline
Introduction
Definition of Push-Down Automata
Acceptance in final states or when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a Push-Down Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
Formal definition of Push-Down Automaton 18
FA qo F Q f
PDA Q qo f F Ao
Stack
Management
19
Input Words: x, y, z, ax, ay... *.
Words in the stack: X, Y, Z, AX, AY... *
Q = {p,q,r,...}
: input alphabet (tape)
: stack alphabet
Q : finite set of states
Ao : initial symbol in the stack
qo Q : initial state of the automaton
f : transition function
F Q : set of final states
PDA: (, , Q, Ao, qo, f, F)
Formal definition of Push-Down Automaton
Outline
Introduction
Definition of Push-Down Automata
Acceptance in final states or when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a Push-Down Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
Push-Down Automaton. Transitions 21
Transition function:
ƒ : Q x (Σ {λ}) x t P (Q x t*)
For each state, input symbol in the tape or empty
word, and symbol on the top of the stack the
automaton determines the transition to another state
and decides the symbols to be written in the stack.
Push-Down Automaton. Transitions 22
Transitions in a push-down automaton follow the
following sequence:
Read an input symbol.
Extract a symbol from the stack.
Insert a word in the stack.
Transit to a new state.
Definition:
f(q,a,A)={(q1,Z1),(q2, Z2),...,(qn, Zn)}
Another notation: (q,a,A;qn,Yn)
where q, qi Q, a , A , Zi *
q qn
a,A;Yn
Push-Down Automaton. Transitions 23
f : Q x ( {} ) x P(Q x *)
Q x x
Q x x
Transitions that depend on
the input
Transitions that do not
depend on the input
Q x *
P (Q x *)
Deterministic
Push-Down
Automata
Non-
Deterministic
Push-Down
Automata
24
Given the transition:
f(q, ,A) = {(q1 ,Z1), (q2, Z2),...,(qn, Zn)}
where:
• q, qi Q
• A
• Zi *
Push-Down Automaton. Transitions
Transitions that do not depend on the input
25
q
.. .. .. a ..
...
...
A
z
X
Example of transition:
(q, az, AX) (p, az, X)
f(q,,A) = (p, )
Transitions that do not depend on the input
p
Push-Down Automaton. Transitions
26
Given the transition:
f(q,a,A) = {(q1,Z1), (q2,Z2),...,(qn,Zn)}
where:
• q, qi Q
• a
• A
• Zi *
Push-Down Automaton. Transitions
Transitions that depend on the input
27
.. .. .. a ..
...
...
A
q X
Example:
(q, az, AX) (p, z, X)
f(q,a,A) = (p, )
p
a
z
Push-Down Automaton. Transitions
Transitions that depend on the input
Outline
Introduction.
Definition of Push-Down Automata
Acceptance in Final States or when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a PD Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
Instantaneous description 29
It is used to easily describe the configuration of a
Push-Down automaton in each moment.
Group of three (q,x,z)
where qQ, x*, z*
It contains:
the current state (q);
the part of the input word that is still to be read (x);
the symbol on the top of the stack (z).
Instantaneous description 30
Instantaneous description (q,x,z) where qQ, x*, z*
Movement (q,ay,AX) (p,y,YX) describes the transition from
an instantaneous description to another.
(p,y,YX) precedes (q,ay,AX) if (p,Y) f(q,a,A)
Succession of movements: (q,ay,AX) * (p,y,YX) represents
that the second instantaneous description can be reached
from the first one.
Outline
Introduction
Definition of Push-Down automata
Acceptance in Final States or when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a PD Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
32
(,,Q,A0,q0,f,F) is deterministic if verifies:
qQ, A, |f (q,,A)| >0 f (q,a,A)= a
If there is a -transition, given a state q and a stack symbol A,
then there is not any -transition with any other input symbol
and state.
qQ, A, a {}, | f (q,a,A) | <2
There is only one transition given a state and a symbol on the
top of the stack: f (q,a,A) = (p,X)
If (p, x, y; q, z) and (p, x, y; r, w) are transitions of a
deterministic push-down automaton, then:
qr, z=w
Deterministic Push-Down Automaton
Outline
Introduction
Definition of Push-Down Automata
Acceptance in final states or when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a Push-Down Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
Language accepted by a Push-Down Automaton
34
Given that the stack is empty:
LEPDA={x|(q0,x,A0) * (p,,), pQ, x*}
When the acceptance is when the stack is empty, the set of final states
is irrelevant, and usually it is empty (F=Ø).
Given an acceptance state:
LFPDA={x|(q0,x,A0) * (p,,X), pF, x*,X*}
Outline
Introduction
Definition of Push-Down Automata
Acceptance in final states or when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a Push-Down Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
Definition of Push-Down Automaton. Example
36
LANGUAGE: set of sentences
var ::= num;
if cond
then
Assignation or IF
if cond
then
Assignation or IF
else
Assignation or IF
Definition of Push-Down Automaton. Example
37
AP= ({if, then, else, ::=, var, num, cond, ;},
{S, B, C, F, N, P, T, E}, {q}, q, S, f, )
f(q, var, S) = {(q, FNP)}
f(q, if, S) = {(q, CTBP), (q, CTBEBP)}
f(q, if, B) = {(q, CTB), (q, CTBEB)}
f(q, var, B) = {(q, FN)}
f(q, cond, C) = {(q, )}
f(q, ::=, F) = {(q, )}
f(q, num, N) = {(q, )}
f(q, ;, P) = {(q, )}
f(q, then, T) = {(q, )}
f(q, else, E) = {(q, )}
f(q,var,S) = {(q,FNP)}
f(q,if,S) = {(q,CTBP),(q,CTBEBP)}
f(q,if,B) = {(q,CTB),(q,CTBEB)}
f(q,var,B) = {(q,FN)}
f(q,cond,C) = {(q,)}
f(q,::=,F) = {(q,)}
f(q,num,N) = {(q,)}
f(q,;,P) = {(q,)}
f(q,then,T) = {(q,)}
f(q,else,E) = {(q,)}
if cond var then ::= num ;
S
q
38
Definition of Push-Down Automaton. Example
P
T
B
C
if cond var then ::= num ;
q
f(q,var,S) = {(q,FNP)}
f(q,if,S) = {(q,CTBP),(q,CTBEBP)}
f(q,if,B) = {(q,CTB),(q,CTBEB)}
f(q,var,B) = {(q,FN)}
f(q,cond,C) = {(q,)}
f(q,::=,F) = {(q,)}
f(q,num,N) = {(q,)}
f(q,;,P) = {(q,)}
f(q,then,T) = {(q,)}
f(q,else,E) = {(q,)}
39
Definition of Push-Down Automaton. Example
P
T
B
if cond var then ::= num ;
q
f(q,var,S) = {(q,FNP)}
f(q,if,S) = {(q,CTBP),(q,CTBEBP)}
f(q,if,B) = {(q,CTB),(q,CTBEB)}
f(q,var,B) = {(q,FN)}
f(q,cond,C) = {(q,)}
f(q,::=,F) = {(q,)}
f(q,num,N) = {(q,)}
f(q,;,P) = {(q,)}
f(q,then,T) = {(q,)}
f(q,else,E) = {(q,)}
40
Definition of Push-Down Automaton. Example
P
B
if cond var then ::= num ;
q
f(q,var,S) = {(q,FNP)}
f(q,if,S) = {(q,CTBP),(q,CTBEBP)}
f(q,if,B) = {(q,CTB),(q,CTBEB)}
f(q,var,B) = {(q,FN)}
f(q,cond,C) = {(q,)}
f(q,::=,F) = {(q,)}
f(q,num,N) = {(q,)}
f(q,;,P) = {(q,)}
f(q,then,T) = {(q,)}
f(q,else,E) = {(q,)}
41
Definition of Push-Down Automaton. Example
P
F
N
if cond var then ::= num ;
q
f(q,var,S) = {(q,FNP)}
f(q,if,S) = {(q,CTBP),(q,CTBEBP)}
f(q,if,B) = {(q,CTB),(q,CTBEB)}
f(q,var,B) = {(q,FN)}
f(q,cond,C) = {(q,)}
f(q,::=,F) = {(q,)}
f(q,num,N) = {(q,)}
f(q,;,P) = {(q,)}
f(q,then,T) = {(q,)}
f(q,else,E) = {(q,)}
42
Definition of Push-Down Automaton. Example
P
N
if cond var then ::= num ;
q
f(q,var,S) = {(q,FNP)}
f(q,if,S) = {(q,CTBP),(q,CTBEBP)}
f(q,if,B) = {(q,CTB),(q,CTBEB)}
f(q,var,B) = {(q,FN)}
f(q,cond,C) = {(q,)}
f(q,::=,F) = {(q,)}
f(q,num,N) = {(q,)}
f(q,;,P) = {(q,)}
f(q,then,T) = {(q,)}
f(q,else,E) = {(q,)}
43
Definition of Push-Down Automaton. Example
P
if cond var then ::= num ;
q
f(q,var,S) = {(q,FNP)}
f(q,if,S) = {(q,CTBP),(q,CTBEBP)}
f(q,if,B) = {(q,CTB),(q,CTBEB)}
f(q,var,B) = {(q,FN)}
f(q,cond,C) = {(q,)}
f(q,::=,F) = {(q,)}
f(q,num,N) = {(q,)}
f(q,;,P) = {(q,)}
f(q,then,T) = {(q,)}
f(q,else,E) = {(q,)}
44
Definition of Push-Down Automaton. Example
if cond var then ::= num ;
q Word read
Empty stack
Sentence recognized
45
Definition of Push-Down Automaton. Example
Outline
Introduction
Definition of Push-Down Automata
Acceptance in final states or when the stack is empty
Formal definition
Transitions
Instantaneous Description, Movement
Deterministic Push-Down Automata
Language Accepted by a Push-Down Automaton
Examples
Equivalence between PD Automata and Context-Free Languages
Push-Down Automaton. Theorem 47
For each push-down automaton accepting strings
without emptying the stack (PDAF), there is an
equivalent automaton that empties the stack before
an accepting state (PDAE).
L(PDAF) = L(PDAE)
Equivalence PDAE and PDAF 48
PDAF = (, , Q, A0, q0, f, F)
PDAE =(, B, Q p,r, B, p, f’, )
From PDAF to PDAE
New symbol for the stack
Two new states Initial value on the stack
New initial state
WITHOUT FINAL STATES
49
p q0
,B; A0B
PDAF = (, , Q, A0, q0, f, F) PDAE=(, B, Q p,r, B, p, f’, )
f’ is defined as following:
qi qj
a,A; Z
qi, qj Q, a Σ {λ} , A , Z *,
Transition independent of the input of the PDAE with the first symbol of the stack transiting to the state q0 of the PDAF and putting A0 on the stack.
qf r
,A;
qf F, A B
qf r
,A;
A B
The transitions in the PDAF
are kept. The characteristics of acceptance of this state are removed.
Equivalence PDAE and PDAF 49
PDAF = (, , Q, A0, q0, f, F)
PDAE =(, B, Q p,r, B, p, f’, ) f’ is defined as following:
f‘(p, ,B) = (q0, A0·B)
A new initial state is incorporated and a transition from this new state to the original initial state of the PDAF, the transition inserts A to which already existed: A0B
f‘(q, a, A) = f(q, a, A) q Q, a Σ {λ} , A
Eliminate the characteristics of acceptance of each state.
(r, ) f‘(q, a, A) q F, A {B}
A new state p is added along with the transitions to acceptance states of acceptance to qf, without reading, extracting or inserting symbols.
(r, ) f‘(r, , A) A {B}
For each A, add the transition (r,,A;r,)
50
Equivalence PDAE and PDAF
51
1 2
a,; X
a,A;
1 2
a,A;
a,; X a,A;
p q
,A; , ;
, A;
a,A;
Equivalence PDAE and PDAF
From PDAF to PDAE
PDAE=(, , Q, A0, q0 , f, ) PDAF=(, B, Q p, r, B, p, f’, {r})
f' is defined as following:
f‘(p, ,B) = (q0, A0·B)
f(q, a, A) = f‘(q, a, A) q Q, a Σ {λ} , A
(r, ) f‘(q, , B) q Q,
52
p q0
,B; A0B
q r*
,B;
Equivalence PDAE and PDAF
PDAE=(, , Q, A0, q0 , f, ) PDAF=(, B, Q p, r, B, p, f’, {r})
f' is defined as following:
f‘(p, ,B) = (q0, A0·B)
The first transition of the PDAF is to go to q0 of the PDAV and write A0B on the
stack, verifying that B is down on the stack.
f(q, a, A) = f‘(q, a, A) q Q, a Σ {λ} , A
The transitions of the PDAE are kept (the original PDA)
(r, ) f‘(q, , B) q Q,
When there is no input, it goes to the final state of the PDAF: in the stack only
remains B (that was introduced at the beginning) . Therefore, the word x is
accepted and it goes to the final state.
53
Equivalence PDAE and PDAF
Push-Down Automaton and Type-2 Languages
54
Given a G2 in GNF, construct a PDAE: G = (T, N, S, P)
• PDAE = (T, N, q, S, q, f, ) We obtain an PDAE with only one state.
(q, Z) f(q, a, A) i.e., f(q, a, A) = (q, Z) if there is a production with the form A ::= aZ.
f(q, a, A) = (q, ) if there is a production with the form A ::= a
f(q, a, A) = {(q, Z), (q, )}
Given a production A::= aZ aD b f(q, a, A)= {(q, Z), (q, D)} f(q, b, A) = (q, )
input stack Initial symbol in the stack
• If S::= (q, ) f(q, ,S)
Given a G2, construct a PDAF:
G = (T, N, S, P)
PDAE = (T, , Q, A0, q0, f, {q2})
Where:
• = T N {A0}, where A0 T N
• Q={q0, q1, q2}, q0 is the initial state, q1 is the state from which transitions
are carried out and q2 is the final state.
• f is defined as follows:
• f (q0, , A0)={q1, SA0}
• AN, if A ::= P, (*) (q1, ) f (q1, , A)
• a T, (q1, ) f (q1,a,a)
• f (q1, , A0) = {q2,A}
55
Push-Down Automata and Type-2 Languages
Given a PDAE, construct a G2 that fulfills L(G2) = L(PDAE)
PDAE = (, , Q, A0, qo, f, )
G = (T, N, S, P)
N= {S} U { (p,A,q) | p,q Q, A }
To construct P:
1. S::= (q0, A0 ,q) q Q (select those that begins with q0A0)
2. From each transition f(p,a,A) = (q, BB’B’’....B’’’) where
A,B,B’,B’’,…,B’’’ ; a Σ U {λ}
(p A z ) ::= a ( q B r ) ( r B’ s ) s ... y ( y B’’’ z )
3. From each transition f( p, a, A) = (q, ), we obtain: (p,A,q) ::= a
56
Push-Down Automata and Type-2 Languages
Push-Down Automaton and Type-2 Languages
Determine if it is valid and propose other valid PDAs
with acceptance when empty stack or final states.
57
anbn
G2 in GNF PDAE
G2 = ({a,b,c,d,e,0,1}, {S,A,B,C,D,E,F,G,H}, S, P)
P = { S::= bDG/ cDG / bDH/ cDH/ bG/ cG/ bH/ cH
G::= aB
H::= aC
A::= bD/ cD/ b/ c
D::= bD/ cD/ b/ c
B::= 0E/ 0
E::= dCE/ dC
C::= 1F/ 1
F::= eBF/ eB}
58
Push-Down Automaton and Type-2 Languages
G2 in GNF PDAE
WELL-FORMED
G2 = ({a,b,c,d,e,0,1}, {S,A,B,C,D,E,F,G,H}, S, P)
P = { S::= bDG/ cDG / bDH/ cDH/ bG/ cG/ bH/ cH
G::= aB
H::= aC
A::= bD/ cD/ b/ c
D::= bD/ cD/ b/ c
B::= 0E/ 0
E::= dCE/ dC
C::= 1F/ 1
F::= eBF/ eB}
59
Push-Down Automaton and Type-2 Languages
Given a PDAE, construct a G2 grammar that describes the same recognized
language
Example (Alfonseca _1997 Page 230 )
Given the Apv = ({a,b}, {A,B}, {p,q}, A, p, f, Ø), where f
f(p,a,A) = { (p,BA)}
f(p,a,B) = { (p,BB)}
f(p,b,B) = { (q, λ)}
f(q,b,B) = { (q, λ)}
f(q, λ,B) = { (q,λ)}
f(q, λ,A) = { (q,λ)}
Calculate the G2 grammar that describes the language recogniced by the
PDAE
60
Push-Down Automaton and Type-2 Languages
Limitations of Push-Down Automata
Is there a Context-Free Grammar which is able to
recognize the language anbncn?
Is there a Push-Down automaton which is able to
recognize the language anbncn?
61