Lecture 9 - CFG-PDA-1
Lecture 9 - CFG-PDA-1
Lecture 9 - CFG-PDA-1
Lecture 9
CFG & PDA
Dr. Samar Hesham
Department of Computer Science
Faculty of Computers and AI
Cairo University
Egypt
1
Syllabus and Terminologies
5/31/2024 FCI-CU-EG 2
Derivation
We derive strings in the language of a CFG by
starting with the start symbol, and repeatedly
replacing some variable A by the right side of one
of its productions.
Example:
S → aSb
S → ab
Same grammar using (or ‘|’)
S → aSb | ab
3
Derivation
CFG:
S → aSb
S → ab
Derivation example for “aabb”
Using S → aSb
generates uncompleted string that still has a non- terminal S.
Then using S → ab to replace the inner S
Generates “aabb”
S aSb aabb ……[Successful derivation of
aabb]
4
Context-Free Languages
L(G) = {w : S * w }
{an bn | n0}
Formally:
G = ({S}, {a,b}, {S e, S a S b}, S)
CFG -1 : Balanced-parentheses
Prod1: S → (S)
Prod2: S → ()
Derive the string ((()))
7
CFG -2 : Palindrome
Describe palindrome of a’s and b’s using CFG
1] S → aSa 2] S → bSb
3] S→Λ
8
CFG -3 : Even-Palindrome
i.e. {Λ, ab, abbaabba,… }
S → aSa| bSb| Λ Derive abaaba
S
a S a
b S b
a S a
Λ
Can you modify this grammar to
accept odd-length palindromes?
9
CFG – 4
Describe anything (a+b)* using CGF
1] S → Λ 2] S → Y 3] Y→ aY
4] Y → bY 5] Y →a 6] Y→ b
S S’ | a S c
S’ e | b S’ c
CFGs & CFLs: Non-Example
{an bn cn | n0}
Can’t be done!
1 2 3 4 5
S AB aaAB aaB aaBb aab
Rightmost derivation:
1 4 5 2 3
S AB ABb Ab aaAb aab
17
S aAB
A bBb
B A|
Leftmost derivation:
19
S AB A aaA | B Bb |
S AB
S
A B
20
S AB A aaA | B Bb |
S AB aaAB
S
A B
a a A
21
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
22
S AB A aaA | B Bb |
A B
a a A B b
23
S AB A aaA | B Bb |
A B
a a A B b
24
S AB A aaA | B Bb |
A B
yield
a a A B b aab
aab
25
Partial Derivation Trees
S AB A aaA | B Bb |
S AB
Partial derivation tree S
A B
26
S AB aaAB
A B
a a A
27
sentential
S AB aaAB form
A B
yield
a a A
aaAB
28
Sometimes, derivation order doesn’t matter
Leftmost:
S
AB aaAB aaB aaBb aab
Rightmost:
a a A B b
29
Different Derivation Trees?
S A|AB
Other derivation trees
A e | a | A b | AA w = aabb
B b|bc|Bc|bB
for this string?
S S
S ? ?
A
A B A B
A A Infinitely
A A b B A A b many others
A A A b possible.
a a b a A b
a e A b
a
a
Ambiguity
31
Ambiguous Grammar
Definition. A grammar G is ambiguous if there is a word
w L(G) having at least two different parse trees
SA
SB
S AB
A aA
B bB
Ae
Be
E E E E a E a EE
a a E a a*a
E E leftmost derivation
a E E
a a 33
E E E | E E | (E) | a
a aa
E E a
a a 34
E E E | E E | (E) | a
a aa
Two derivation trees
E E
E E E E
a E E E E a
a a a a 35
The grammar E E E | E E | (E) | a
is ambiguous:
string a aa has two derivation trees
E E
E E E E
a E E E E a
a a a a
36
The grammar E E E | E E | (E) | a
is ambiguous:
string a aa has two leftmost derivations
E E E a E a EE
a a E a a*a
38
In other words:
G is ambiguous
A context-free grammar
39
Why do we care about ambiguity?
a aa
take a2
E E
E E E E
a E E E E a
a a a a 40
2 22
E E
E E E E
2 E E E E 2
2 2 2 2 41
2 22 6 2 22 8
6 8
E E
2 4 4 2
E E E E
2 2 2 2
2 E E E E 2
2 2 2 2 42
Correct result: 2 22 6
6
E
2 4
E E
2 2
2 E E
2 2
43
Ambiguity
E E E | E E | (E) | a
E a aa
E T
T T F
F F a
a a
49
The grammar : G E E T
E T
T T F
T F
F (E)
F a
is non-ambiguous:
S S1 | S 2 S1 S1c | A S 2 aS 2 | B
A aAb | B bBc |
51
n n n
The string a b c
has two derivation trees
S S
S1 S2
S1 c a S2
52
Disambiguation
Eliminate ambiguity.
Eliminate “useless” variables.
Eliminate e-productions: Ae.
Eliminate unit productions: AB.
Eliminate redundant productions.
Trade left- & right-recursion.
Trading Left- & Right-Recursion
Left recursion: A A a
Right recursion: A a A
58
Hierarchy of languages
PDA
Recursive Languages
Context-Free Languages
Regular Languages
FSA
59
PUSHDOWN AUTOMATON -- PDA
tape
tape head
stack head
finite
stack
control
Pushdown Automaton -- PDA
Input String
Stack
States
Informally:
A PDA is an NFA with a stack.
Transitions are modified to accommodate stack operations.
Questions:
What is a stack?
How does a stack help?
62
Example of weakness of FA
1 1 1 1
0/1 1 1
0/1 q7 q6 q5 q4
0
0
0
63
FA vs. PDA
64
The States
q1 a, b c q2
66
Same Language: but with Stack Check
PDA M
: L( M ) {a b : n 0} n n
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 67
Basic Idea: L( M ) {a b : n 0}
n n
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Costas Busch - RPI 68
Execution Example: Time 0
Input
a a a b b b
$
Stack
current a, a b, a
state
q0 , q b, a q , $ $ q
1 2 3
Fall 2006 Costas Busch - RPI 69
Time 1
Input
a a a b b b
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 70
Time 2
Input
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 71
Time 3
Input
a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 72
Time 4
Input
a
a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 73
Time 5
a
Input
a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 74
Time 6
Input
a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 75
Time 7
Input
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 76
Time 8
Input
a a a b b b
$
Stack
a, a b, a
accept
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 77
Rejection Example: Time 0
Input
a a b
$
Stack
current a, a b, a
state
q0 , q b, a q , $ $ q
1 2 3
Fall 2006 Costas Busch - RPI 78
Rejection Example: Time 1
Input
a a b
$
Stack
current a, a b, a
state
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 79
Rejection Example: Time 2
Input
a
a a b
$
Stack
current a, a b, a
state
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 80
Rejection Example: Time 3
Input a
a
a a b
$
Stack
current a, a b, a
state
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 81
Rejection Example: Time 4
Input a
a
a a b
$
Stack
current a, a b, a
state
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 82
Rejection Example: Time 4
Input a
a
a a b
$
Stack
reject
current a, a b, a
state
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 83
There is no accepting computation for aab
a, a b, a
q0 , q1 b, a q2 , $ $ q3
Fall 2006 Costas Busch - RPI 84
Another PDA example: Palindromes
86
Palindromes with stack checking
PDA M
: L( M ) {vv : v {a, b} } R
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 87
Basic Idea: L( M ) {vv : v {a, b} } R
R
1. Push v v
3. Match on input
2. Guess with v on stack
on stack middle
of input
a, a a, a 4. Match
found
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 88
Execution Example: Time 0
Input
a b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 89
Time 1
Input
a b b a
a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 90
Time 2
Input
b
a b b a
a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 91
Time 3
Input
b
a b b a
Guess the middle a
of string
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 92
Time 4
Input
b
a b b a
a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 93
Time 5
Input
a b b a
a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 94
Time 6
Input
a b b a
$
Stack
a, a a, a
b, b b, b
accept
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 95
Rejection Example: Time 0
Input
a b b b
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 96
Time 1
Input
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 97
Time 2
Input
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 98
Time 3
Input
b
a b b b
Guess the middle a
of string
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 99
Time 4
Input
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 100
Time 5
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 101
Another computation on same string:
Input Time 0
a b b b
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 102
Time 1
Input
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 103
Time 2
Input
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 104
Time 3
Input b
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 105
Time 4 b
Input b
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 106
Time 5 b
Input b
No accept state
is reached b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 107
There is no computation
that accepts stringabbb
abbb L(M )
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
Fall 2006 Costas Busch - RPI 108