Context-Free Languages

advertisement
CD5560
FABER
Formal Languages, Automata
and Models of Computation
Lecture 8
Mälardalen University
2010
1
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
2
n l nl
{a b c
: n, l  0}
{a : n  0}
n!
Non-regular languages
Context-Free Languages
{a nb n } {ww R }
Regular Languages
3
Context-Free Languages
4
Context-Free Languages
Context-Free
Grammars
Pushdown
Automata
stack
(CF grammars are
defined as generalized
Regular Grammars)
automaton
5
Definition: Context-Free Grammars
Grammar
Variables
G  (V , T , S , P)
Terminal
symbols
Start
variables
Productions of the form:
A x
x is string of variables and terminals
6
Definition: Regular Grammars
Grammar
Variables
G  (V , T , S , P)
Terminal
symbols
Start
variables
Right or Left Linear Grammars. Productions of the form:
A  xB
A  Bx
or
Cx
x is string of 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

y
x
z
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
(certain kind of) information.
The stack allows pushdown automata to recognize
some non-regular languages.
10
The States
Pop old
Input
symbol
q1
Push new
- reading
stack symbol
a, b / c
- writing
stack symbol
q2
11
q1
a, b / c
q2
input

a


a

stack
b
h
e
$
top
Replace
c
h
e
$
(An alternative is to start and finish with empty stack
or a stack bottom symbol such as $)
12
q1 a ,  / c
q2
input

a


stack
b
h
e
$
top
Push
a

c
b
h
e
$
13
q1
a, b / 
q2
input

a


a

stack
b
h
e
$
top
Pop
h
e
$
14
a
,

/

q1
q2
input

a


a

stack
b
h
e
$
top
No Change
b
h
e
$
15
Time 0
Example 3.7 Salling:
A PDA for simple nested parenthesis strings
(
(
(
)
)

)
Input
(, / (
start
s
Stack
), ( / 
), (/ 
q
end
16
Example 3.7
Time 1
Input
(
(
(
)
)
(, / (
start
s
(
)

Stack
), ( / 
), (/ 
q
end
17
Example 3.7
Time 2
Input
(
(
(
(
)
)
)
(

(, / (
start
s
Stack
), ( / 
), (/ 
q
end
18
Example 3.7
Time 3
Input
(
(
(
)
)
(
(
)
(

(, / (
start
s
), ( / 
), (/ 
q
Stack
end
19
Example 3.7
Time 4
Input
(
(
(
)
)
(
(
)
(
(, / (

), ( / 
Stack
start
s
), (/ 
q
end
20
Example 3.7
Time 5
Input
(
(
(
)
)
(
)
(

(, / (
start
s
), ( / 
), (/ 
q
Stack
end
21
Example 3.7
Time 6
Input
(
(
(
)
)
(
)

), ( / 
(, / (
start
s
Stack
), (/ 
q
end
22
Example 3.7
Time 7
Input
(
(
(
)
)

)
Stack
(, / (
start
s
), ( / 
), (/ 
q
end
23
NPDAs
Non-deterministic Push-Down Automata
24
Non-Determinism
a, b / c
q2
, b / c
q1
q1
a, b / c
q3
q2
  transition
25
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)
26
  {a, b}
Example NPDA
L  {a b : n  0}
n n
is the language accepted by the NPDA:
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
27
Example NPDA
NPDA
  {a, b}
M
L( M )  {ww }
R
a,  / a
a, a / 
b,  / b
b, b / 
q0
,  / 
q1
(Even-length palindromes)
, $ / $
q2
28
Pushing Strings
Pop
symbol
Input
symbol
q1
a, b / w
Push
string
q2
29
Example
q1
a, b / cdf
q2
input
a
a
stack
b
h
e
$
top
Push
c
d
f
h
e
$
pushed
string
30
Another NPDA example
NPDA
  {a, b}
M
L(M )  {w : na  nb}
a, $ / 0$
a, 0 / 00
a ,1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
q1
, $ / $
q2
31
Execution Example
Time 0
Input
a b
b a a b
$
a, $ / 0$
a, 0 / 00
a ,1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
Current state
q1
, $ / $
Stack
q2
32
Time 1
Input
a b
b a a b
0
$
a, $ / 0$
a, 0 / 00
a ,1 / 
q1
b, $ / 1$
b, 1 / 11
b, 0 / 
, $ / $
Stack
q2
33
Time 2
Input
a b
b b a a
a, $ / 0$
a, 0 / 00
a ,1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
0
$
Stack
, $ / $
q1
q2
34
Time 3
Input
a b
b b a a
1
$
a, $ / 0$
a, 0 / 00
a ,1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
, $ / $
q1
Stack
q2
35
Time 4
Input
a b
b b a a
a, $ / 0$
a, 0 / 00
a ,1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
1
1
$
Stack
, $ / $
q1
q2
36
Time 5
Input
a b
b b a a
a, $ / 0$
a, 0 / 00
a ,1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
1
1
$
Stack
, $ / $
q1
q2
37
Time 6
Input
a b
b b a a
a, $ / 0$
a, 0 / 00
a ,1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
1
$
Stack
, $ / $
q1
q2
38
Time 7
Input
a b
b b a a
$
a, $ / 0$
a, 0 / 00
a ,1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
q1
, $ / $
Stack
accept
q2
39
Formal Definitions for NPDAs
40
q1
a, b / w
q2
Transition function
 (q1, a, b)  {(q2 , w)}
41
Transition function
q2
a, b / w
q1
a, b / w
q3
current state
new state
 (q1, a, b)  {(q2 , w), (q3 , w)}
current stack top
current input symbol
new stack top
An unspecified transition function is to the null set and represents a dead configuration for the NPDA.
42
Formal Definition
Non-Deterministic Pushdown Automaton NPDA
M  (Q, , ,  , z, F )
States
Input
alphabet
Stack
alphabet
Transition
function
Final
states
Stack
start
symbol
43
Instantaneous Description
( q, u , s )
Current
state
Remaining
input
Current
stack
contents
44
Example
Instantaneous Description
(q1, bbb, aaa$)
(current state, remaininginput, stack)
Time 4:
a
a
a
$
Input
a a a b b b
a,  / a
q0
,  / 
q1
b, a / 
b, a /  q
2
Stack
, $ / $ q
3
45
Example
Instantaneous Description
(q2 , bb, aa$)
(current state, remaininginput, stack)
Time 5:
a
a
a
$
Input
a a a b b b
a,  / a
q0
,  / 
q1
b, a / 
b, a /  q
2
Stack
, $ / $ q
3
46
We write
Time 4
Time 5
(q1, bbb, aaa$)  (q2 , bb, aa$)
( current state , remaining input , current stack content)
47
( current state , remaining input , current stack content)
A computation example
( q0 , aaabbb, $)  ( q1, aaabbb, $) 
( q1, aabbb, a$)  (q1, abbb, aa$)  ( q1, bbb, aaa$) 
( q2 , bb, aa$)  ( q2 , b, a$)  ( q2 , , $)  (q3 , , $)
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
48
( current state , remaining input , current stack content)
A computation example
( q0 , aaabbb, $)  ( q1, aaabbb, $) 
( q1, aabbb, a$)  (q1, abbb, aa$)  ( q1, bbb, aaa$) 
( q2 , bb, aa$)  ( q2 , b, a$)  ( q2 , , $)  (q3 , , $)
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
49
( current state , remaining input , current stack content)
A computation example
( q0 , aaabbb, $)  ( q1, aaabbb, $) 
( q1, aabbb, a$)  (q1, abbb, aa$)  ( q1, bbb, aaa$) 
( q2 , bb, aa$)  ( q2 , b, a$)  ( q2 , , $)  (q3 , , $)
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
50
( current state , remaining input , current stack content)
A computation example
( q0 , aaabbb, $)  ( q1, aaabbb, $) 
( q1, aabbb, a$)  (q1, abbb, aa$)  ( q1, bbb, aaa$) 
( q2 , bb, aa$)  ( q2 , b, a$)  ( q2 , , $)  (q3 , , $)
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
51
( current state , remaining input , current stack content)
A computation example
( q0 , aaabbb, $)  ( q1, aaabbb, $) 
( q1, aabbb, a$)  (q1, abbb, aa$)  ( q1, bbb, aaa$) 
( q2 , bb, aa$)  ( q2 , b, a$)  ( q2 , , $)  (q3 , , $)
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
52
( current state , remaining input , current stack content)
A computation example
( q0 , aaabbb, $)  ( q1, aaabbb, $) 
( q1, aabbb, a$)  (q1, abbb, aa$)  ( q1, bbb, aaa$) 
( q2 , bb, aa$)  ( q2 , b, a$)  ( q2 , , $)  (q3 , , $)
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
53
( current state , remaining input , current stack content)
A computation example
( q0 , aaabbb, $)  ( q1, aaabbb, $) 
( q1, aabbb, a$)  (q1, abbb, aa$)  ( q1, bbb, aaa$) 
( q2 , bb, aa$)  ( q2 , b, a$)  ( q2 , , $)  (q3 , , $)
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
54
( current state , remaining input , current stack content)
A computation example
( q0 , aaabbb, $)  ( q1, aaabbb, $) 
( q1, aabbb, a$)  (q1, abbb, aa$)  ( q1, bbb, aaa$) 
( q2 , bb, aa$)  ( q2 , b, a$)  ( q2 , , $)  (q3 , , $)
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
55
( current state , remaining input , current stack content)
( q0 , aaabbb, $)  ( q1, aaabbb, $) 
( q1, aabbb, a$)  (q1, abbb, aa$)  ( q1, bbb, aaa$) 
( q2 , bb, aa$)  ( q2 , b, a$)  ( q2 , , $)  (q3 , , $)
For convenience we write

( q0 , aaabbb, $)  ( q3 ,  , $)
56
Formal Definition
Language of NPDA
M

L( M )  {w : ( q0 , w, s)  ( q f ,  , s' )}
Initial state
Final state
57

( q0 , aaabbb,$)  ( q3 ,  ,$)
Example
aaabbb L(M )
NPDA
M
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
58

( q0 , a b ,$)  ( q3 ,  ,$)
n n
a b  L(M )
n n
NPDA
q0
M
a,  / a
,  / 
q1
b, a / 
b, a /  q
2
, $ / $ q
3
59
Therefore:
NPDA
L( M )  {a b : n  0}
n n
M
a,  / a
q0
,  / 
q1
b, a / 
b, a / 
q2
, $ / $
q3
60
NPDAs Accept
Context-Free Languages
61
Theorem
Context-Free
Languages
(Grammars)

Languages
Accepted by
NPDAs
62
Proof - Step 1:
Context-Free
Languages
(Grammars)

Convert any context-free grammar
with
Languages
Accepted by
NPDAs
G to a NPDA M
L(G) = L(M)
63
Proof - Step 2:
Context-Free
Languages
(Grammars)

Languages
Accepted by
NPDAs
Convert any NPDA M to a context-free grammar
with
G
L(M) = L(G)
64
Converting
Context-Free Grammars
to NPDAs
65
An example grammar:
S  aSTb
S b
T  Ta
T 
What is the equivalent NPDA?
66
Grammar
S  aSTb
S b
T  Ta
T 
q0
,  / S
NPDA
 , S / aSTb
, S / b
a, a / 
 , T / Ta
b, b / 
, T / 
q1
, $ / $
q2
67
The NPDA simulates
leftmost derivations of the grammar
L(Grammar) = L(NPDA)
68
Grammar:
S  aSTb
S b
T  Ta
T 
A leftmost derivation:
S  aSTb  abTb  abT ab  abab
69
NPDA execution:
Time 0
a b a b
Input
 , S / aSTb
, S / b
a, a / 
 , T / Ta
b, b / 
, T / 
Start
q0
,  / S
q1
, $ / $
$
Stack
q2
70
Time 1
a b a b
Input
S
$
 , S / aSTb
, S / b
 , T / Ta
, T / 
q0
,  / S
q1
Stack
a, a / 
b, b / 
, $ / $
q2
71
Time 2
Input
a b a b
a
S
T
b
$
 , S / aSTb
, S / b
a
,
a
/

 , T / Ta
b
,
b
/

, T / 
q0
,  / S
q1
Stack
, $ / $
q2
72
Time 3
Input
a b a b
 , S / aSTb
, S / b
 , T / Ta
, T / 
q0
a
S
T
b
$
,  / S
q1
a, a / 
b, b / 
, $ / $
Stack
q2
73
Time 4
Input
b
T
b
$
a b a b
 , S / aSTb
, S / b
a, a / 
 , T / Ta
b, b / 
, T / 
q0
,  / S
q1
Stack
, $ / $
q2
74
Time 5
Input
a b a b
 , S / aSTb
, S / b
 , T / Ta
, T / 
q0
b
T
b
$
,  / S
q1
a, a / 
Stack
b, b / 
, $ / $
q2
75
Time 6
Input
T
a
b
$
a b a b
 , S  aSTb
, S  b
 , T  Ta
, T  
q0
,   S
Stack
a, a  
b, b  
q1
, $  $
q2
76
Time 7
Input
T
a
b
$
a b a b
 , S / aSTb
, S / b
 , T / Ta
, T / 
q0
,  / S
a, a / 
Stack
b, b / 
q1
, $ / $
q2
77
Time 8
Input
a b a b
a
b
$
 , S / aSTb
, S / b
 , T / Ta
, T / 
q0
,  / S
Stack
a, a / 
b, b / 
q1
, $ / $
q2
78
Time 9
Input
a b a b
 , S / aSTb
, S / b
 , T / Ta
, T / 
q0
,  / S
q1
b
$
a, a / 
Stack
b, b / 
, $ / $
q2
79
Time 10
Input
a b a b
$
 , S / aSTb
, S / b
 , T / Ta
, T / 
q0
,  / S
Stack
a, a / 
b, b / 
q1
, $ / $
accept
q2
80
In general
Given any grammar
G
we can construct a NPDA M with
L(G)  L( M )
81
Constructing NPDA
M from grammar G
Top-down parser
For any production
For any terminal
Aw
, A / w
q0
,  / S
a
a, a / 
q1
, $ / $
q2
82
Grammar G generates string w
if and only if
NPDA M accepts w
L(G)  L( M )
83
For any context-free language
there is an NPDA
that accepts the same language
84
Which means
Context-Free
Languages
(Grammars)

Languages
Accepted by
NPDAs
85
Converting NPDAs
to
Context-Free Grammars
86
For any NPDA M
we will construct
a context-free grammar G with
L( M )  L(G)
87
The grammar simulates the machine
A derivation in Grammar
terminals
G
variables
S    abc ABC     abc
Input processed
Stack contents
in NPDA M
88
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
qf
89
Second we modify the NPDA transitions.
All transitions will have form:
qi
a, B / 
qj
or
qi
a, B / CD
qj
which means that each move
increases/decreases stack by a single symbol.
90
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.
91
Example of a NPDA in an appropriate form
L(M )  {w : na  nb}
a, $ / 0$
a, 0 / 00
a, 1 / 
q0
b, $ / 1$
b, 1 / 11
b, 0 / 
, $ / 
  {a, b}
($ : initialstack symbol)
qf
92
The Grammar Construction
In grammar
G
Stack symbol
Variables:
(qi Bq j )
states
Terminals:
Input symbols of NPDA
93
For each transition:
we add production:
qi
a, B / 
qj
(qi Bq j )  a
94
For each transition:
we add production:
qi
a, B / CD
qj
(qi Bqk )  a(qiCql )(ql Dqk )
for all states
q k , ql
95
Stack bottom symbol
Start Variable
Start state
(qo $q f )
(Single) Final state
96
L(M )  {w : na  nb}
Example
a, $ / 0$
a, 0 / 00
a, 1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
q0
qi
a, B / 
qj
(qi Bq j )  a
, $ / 
Grammar production:
  {a, b}
qf
(q01q0 )  a
97
a, $ / 0$
a, 0 / 00
a, 1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
q0
, $ / 
qi
a, B / CD
qj
(qi Bqk )  a (q j Cql )(ql Dqk )
for all states qk , ql
qf
Grammar productions:
(q0 $q0 )  b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 )
(q0 $q f )  b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f )
98
a, $ / 0$
a, 0 / 00
a, 1 / 
b, $ / 1$
b, 1 / 11
b, 0 / 
q0
Grammar production:
, $ / 
qi
a, B / 
qj
(qi Bq j )  a
qf
(q0 $q f )  
99
Resulting Grammar
a, $ / 0$ b, $ / 1$
a, 0 / 00 b, 1 / 11
a, 1 / 
b, 0 / 
q0
, $ / 
L(M )  {w : na  nb}   {a, b}
(q0 $q f ) : start variable
qf
(qi Bqk )  a (q j Cql )(ql Dqk )
for all states qk , ql
(q0 $q0 )  b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 )
(q0 $q f )  b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f )
(q01q0 )  b(q01q0 )(q01q0 ) | b(q01q f )(q f 1q0 )
(q01q f )  b(q01q0 )(q01q f ) | b(q01q f )(q f 1q f )
100
Resulting Grammar, cont.
L(M )  {w : na  nb}
a, $ / 0$ b, $ / 1$
a, 0 / 00 b, 1 / 11
a, 1 / 
b, 0 / 
q0
, $ / 
qf
(q0 $q0 )  a(q0 0q0 )(q0 $q0 ) | a(q0 0q f )(q f $q0 )
(q0 $q f )  a(q0 0q0 )(q0 $q f ) | a(q0 0q f )(q f $q f )
(q0 0q0 )  a(q0 0q0 )(q0 0q0 ) | a(q0 0q f )(q f 0q0 )
(q0 0q f )  a(q0 0q0 )(q0 0q f ) | a(q0 0q f )(q f 0q f )
101
Resulting Grammar, cont.
a, $ / 0$
a, 0 / 00
a, 1 / 
q0
qi
L(M )  {w : na  nb}
b, $ / 1$
b, 1 / 11
b, 0 / 
(q01q0 )  a
(q0 0q0 )  b
, $ / 
a, B / 
qf
(q0 $q f )  
qj
(qi Bq j )  a
102
Derivation of string
abba
(q0 $q f )  a (q0 0q0 )(q0 $q f ) 
ab(q0 $q f ) 
a, $ / 0$ b, $ / 1$
a, 0 / 00 b, 1 / 11
a, 1 / 
b, 0 / 
q0
qi
abb(q01q0 )(q0 $q f ) 
, $ / 
a, B / 
(qi Bq j )  a
qf
abba(q0 $q f ) 
qj
abba
103
In general, in grammar:

(q0 $q f )  w
if and only if
w is accepted by the NPDA
104
qi
Explanation
a, B / 
qj
(qi Bq j )  a
By construction of Grammar:

(qi Aq j )  w
if and only if
in the NPDA going from qi to qj
the stack doesn’t change below
and A is removed from stack
105
We have shown the procedure to convert
any NPDA M to a context-free
grammar G with L(M)
= L(G)
which means
Context-Free
Languages
(Grammars)

Languages
Accepted by
NPDAs
106
Therefore
Context-Free
Languages
(Grammars)

Languages
Accepted by
NPDAs
END OF PROOF
107
Example (Sudkamp 8.1.2)
L(M )  {a : i  0} {a b : i  0}
i
i i
Language consisting solely of a’s or an equal number of a´s and b´s.
a,  / A
q0
b, A / 
b, A / 
q1
,  / 
q2
, A / 
108
Download