Lecture 9 - CFG-PDA-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 107

Theory of Computation

Lecture 9
CFG & PDA
Dr. Samar Hesham
Department of Computer Science
Faculty of Computers and AI
Cairo University
Egypt

1
Syllabus and Terminologies

 Regular Languages .. Regular Sets


 REs (Regular Expressions)
 FSMs (or FSA/FA) … Finite State Machines/Automata
 DFSM/DFA vs. NDFSM/NFA … Deterministic vs. Non-deterministic FSM
 Comparison and conversion
 Examples & Closure Operations
 Context Free Languages
 CFGs … Context Free Grammars
 PDA … Push Down Automata
 Some applications
 Turing Machine

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

Definition. Given a context-free grammar


G = (, NT, R, S), the language generated or
derived from G is the set:

L(G) = {w : S * w }

Definition. A language L is context-free if there is a


context-free grammar G = (, NT, R, S), such that L is
generated from G
CFGs & CFLs: Example 1

{an bn | n0}

One of our canonical non-RLs.


Se|aSb

Formally:
G = ({S}, {a,b}, {S  e, S  a S b}, S)
CFG -1 : Balanced-parentheses
Prod1: S → (S)
Prod2: S → ()
 Derive the string ((()))

S → (S) …..[by prod1]


→ ((S)) …..[by prod1]
→ ((())) …..[by prod2]

7
CFG -2 : Palindrome
 Describe palindrome of a’s and b’s using CFG
 1] S → aSa 2] S → bSb
 3] S→Λ

 Derive “baab” from the above grammar.


 S → bSb [by 2]
→ baSab [by 1]
→ ba ab [by 3]

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

 Derive “aab” from the above grammar.


 S →Y [by 1]
Y → aY [by 3]
Y → aaY [by 3]
Y → aab [by 6]
10
CFGs & CFLs: Example 3

{am bn cm+n | m,n0}


? ?
Rewrite as {am bn cn cm | m,n0}:

S  S’ | a S c
S’  e | b S’ c
CFGs & CFLs: Non-Example

{an bn cn | n0}

Can’t be done!

Intuition: Can count to n, then can count down from n,


but forgetting n after that.
 i.e., a stack as a counter.
Parse Tree

A parse tree of a derivation is a tree in which:


S
• Each internal node is labeled with a
nonterminal a S
a S
•If a rule A  A1A2…An occurs in the b
derivation then A is a parent node of nodes S
labeled A1, A2, …, An e
Parse Trees
S A|AB Sample derivations:
A  e | a | A b | AA S  AB  AAB  aAB  aaB  aabB  aabb
B b|bc|Bc|bB S  AB  AbB  Abb  AAbb  Aabb  aabb

These two derivations use same productions, but in different orders.


This ordering difference is often uninteresting.
Derivation trees give way to abstract away ordering differences.
S Root label = start node.

A B Each interior label = variable.

A A b B Each parent/child relation = derivation step.

Each leaf label = terminal or e.


a a b
All leaf labels together = derived string = yield.
Leftmost vs. Rightmost Derivations

Definition. A left-most derivation of a sentential form is one


in which rules transforming the left-most nonterminal are
always applied

Definition. A right-most derivation of a sentential form is


one in which rules transforming the right-most nonterminal
are always applied
Leftmost vs. Rightmost Derivations

S A|AB Sample derivations:


A  e | a | A b | AA S  AB  AAB  aAB  aaB  aabB  aabb
B b|bc|Bc|bB S  AB  AbB  Abb  AAbb  Aabb  aabb

S These two derivations are special:

A B 1st derivation is leftmost.


Always picks leftmost variable to expand.
A A b B
2nd derivation is rightmost.
a a b Always picks rightmost variable to expand.
Derivation Order
1. S  AB 2. A  aaA 4. B  Bb
3. A   5. B  
Leftmost derivation:

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:

S  aAB  abBbB  abAbB  abbBbbB


 abbbbB  abbbb
Rightmost derivation:

S  aAB  aA  abBb  abAb


 abbBbb  abbbb 18
Derivation Trees
Example

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 | 

S  AB  aaAB  aaABb  aaBb


S

A B

a a A B b

 23
S  AB A  aaA |  B  Bb | 

S  AB  aaAB  aaABb  aaBb  aab


Derivation Tree S

A B

a a A B b

  24
S  AB A  aaA |  B  Bb | 

S  AB  aaAB  aaABb  aaBb  aab


Derivation Tree S

A B
yield

a a A B b aab
 aab
  25
Partial Derivation Trees
S  AB A  aaA |  B  Bb | 

S  AB
Partial derivation tree S

A B

26
S  AB  aaAB

Partial derivation tree


S

A B

a a A

27
sentential
S  AB  aaAB form

Partial derivation tree


S

A B

yield
a a A
aaAB
28
Sometimes, derivation order doesn’t matter

Leftmost:

S

AB  aaAB  aaB  aaBb  aab
Rightmost:

S  AB  ABb  Ab  aaAb  aab


S
Same derivation tree
A B

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

SA
SB
S  AB
A  aA
B  bB
Ae
Be

Notice that this grammar has at least two left-most derivations


E  E  E | E  E | (E) | a
a  aa

E E  E  E  a E  a EE
 a  a E  a  a*a
E  E leftmost derivation

a E  E

a a 33
E  E  E | E  E | (E) | a
a  aa

E  EE  E  EE  a EE E


 a  aE  a  aa
leftmost derivation E  E

E  E a

a a 34
E  E  E | E  E | (E) | a
a  aa
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  aa 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  aa has two leftmost derivations

E  E  E  a E  a EE
 a  a E  a  a*a

E  EE  E  EE  a EE


 a  aE  a  aa 37
Definition:
G is ambiguous
A context-free grammar

if some string w L (G ) has:

two or more derivation trees

38
In other words:

G is ambiguous
A context-free grammar

if some string w L (G ) has:

two or more leftmost (or rightmost) derivations

39
Why do we care about ambiguity?

a  aa
take a2
E E

E  E E  E

a E  E E  E a

a a a a 40
2  22

E E

E  E E  E

2 E  E E  E 2

2 2 2 2 41
2  22  6 2  22  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  22  6

6
E
2 4
E  E
2 2
2 E  E

2 2
43
Ambiguity

CFG ambiguous  any of the following equivalent


statements:
  string w with multiple derivation trees.
  string w with multiple leftmost derivations.
  string w with multiple rightmost derivations.

Defining ambiguity of grammar, not language.


Ambiguity & Disambiguation

Given an ambiguous grammar, would like an equivalent


unambiguous grammar.
 Allows you to know more about structure of a given
derivation.
 Simplifies inductive proofs on derivations.
 Can lead to more efficient parsing algorithms.
 In programming languages, want to impose a canonical
structure on derivations. E.g., for 1+23.

Strategy: Force an ordering on all derivations.


Disambiguation: Example 1

Exp  n Exp  Term


| Exp + Exp | Term + Exp
| Exp  Exp Term  n
| n  Term

What is the equivalent


Uses
unambiguous
 operator precedence
grammar?  left-associativity
We fix the ambiguous grammar:

E  E  E | E  E | (E) | a

New non-ambiguous grammar: E  E T


E T
T T F
T F
F  (E)
F a 47
E  E T T T  F T  a T  a T F
 a  F F  a  aF  a  aa
E a  aa
E  E T
E  T
E T
T T F T T  F
T F
F F a
F  (E)
F a a a
48
Unique derivation tree

E a  aa
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:

Every string w L(G ) has a unique derivation tree


50
Inherent Ambiguity

 Some context free languages are inherently


ambiguous
 have only ambiguous grammars
Example: L  {a b c }  {a b c }
n n m n m m

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

What is a general algorithm?


?
None exists!
?
There are CFLs that are inherently ambiguous
Every CFG for this language is ambiguous.

E.g., {anbncmdm | n1, m1}  {anbmcmdn | n1, m1}.

So, it is not necessarily eliminate ambiguity!


CFG Simplification

Can’t always eliminate ambiguity.

But, CFG simplification & restriction still useful


theoretically & pragmatically.
 Simpler grammars are easier to understand.
 Simpler grammars can lead to faster parsing.
 Restricted forms useful for some parsing algorithms.
 Restricted forms can give you more knowledge about
derivations.
CFG Simplification: Example
How can the following Grammar be simplified?
SAB
SACD 1) Delete: B useless because nothing derivable from B.
AAa
Aa
AaA
2) Delete either AAa or AaA.
Aa 3) Delete one of the idential productions.
Ce 4) Delete & also replace SACD with SAD.
DdD
DE 5) Replace with DeAe.
6) Delete: E useless after change #5.
EeAe
7) Delete: F useless because it is not derivable from S.
Fff
CFG Simplification

Eliminate ambiguity.
Eliminate “useless” variables.
Eliminate e-productions: Ae.
Eliminate unit productions: AB.
Eliminate redundant productions.
Trade left- & right-recursion.
Trading Left- & Right-Recursion

Left recursion: A  A a
Right recursion: A  a A

Most parsing algorithms have trouble with one,

In recursive descent, avoid left recursion.


CFG & PDA

58
Hierarchy of languages

Non-Recursively Enumerable Languages

Recursively Enumerable 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

Fall 2006 Costas Busch - RPI 61


Pushdown Automata (PDA)

 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?

 A FA can “remember” only a finite amount of information,


whereas a PDA can “remember” an infinite amount of
(certain types of) information, in one memory-stack

62
Example of weakness of FA

{0n1n | 0=<n} is not regular, but

{0n1n | 0nk, for some fixed k} is regular, for any fixed k.


 For k=3:
L = {ε, 01, 0011, 000111}
0 0 0
q0 q 1 q 2 q3

1 1 1 1
0/1 1 1
0/1 q7 q6 q5 q4
0
0
0

63
FA vs. PDA

 In a FA, each state remembers a finite amount of information.

 To get {0n1n | 0n} with a DFA would require an infinite number of


states using the preceding technique.

 An infinite stack solves the problem for {0n1n | 0  n} as follows:


 Read all 0’s and place them on a stack
 Read all 1’s and match with the corresponding 0’s on the stack

 Only need two states to do this in a PDA

 Similarly for {0n1m0n+m | n,m0}

64
The States

Input Pop Push


symbol symbol symbol

q1 a, b  c q2

Fall 2006 Costas Busch - RPI 65


CFG to PDA

 A PDA for AnBn = {anbn: n  0}

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

1. Push the a’s 2. Match the b’s on input


on the stack with a’s on stack
3. Match
found

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

The string aab is rejected by the PDA

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

Input There is no possible transition.

a b b b Input is not consumed


a
$
Stack
a,   a a, a  
b,   b b, b  

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy