FALLSEM2020-21 CSE2002 TH VL2020210106983 Reference Material III 27-Aug-2020 CFGPDANOTES PDF
FALLSEM2020-21 CSE2002 TH VL2020210106983 Reference Material III 27-Aug-2020 CFGPDANOTES PDF
FALLSEM2020-21 CSE2002 TH VL2020210106983 Reference Material III 27-Aug-2020 CFGPDANOTES PDF
The theory of formal language is used in the field of linguistics – to define valid
sentences and give structural descriptions of sentences. Linguistics wanted to define a formal
grammar (To describe the rules of grammar in the mathematical way) to describe English, would
make language translation using computers easy.
DEFINITION CFG:
S => 0S1/ ξ
S => 0S1
. .
=> 00S11 .
S=>0S1
=> 000S111
. .
=> 000ξ111 .
S=>ξ
∗
S=>000111
𝐺
If grammar G = (V,T,P,S) be a context free grammar then the language L(G) is a set of
a) S => aSb / ab
S=> aSb
. .
=> aaSbb .
S=>aSb
=> aaaSbbb
. .
=> aaaaSbbbb .
S=>ab
. ∗ .
. .
S=>aaaaaSbbbbb . .
L(G) = { 𝑎𝑛 𝑏𝑛 / n > 1}
𝐺
b) S => aCa
C => aCa / b
S => aCa
. .
=> aaCaa .
C=> aCa
. .
=> aaaCaaa .
C=> b
∗
S => aaabaaa
𝐺
. 𝑛
. .
L(G) = { 𝑎 𝑏 𝑎𝑛 / n > 1} \
DERIVATION:
∗
It is defined as α =>β where β is derived from the symbol ‘α’ with the grammar ‘G’.
𝐺
Here, we use the production from head to body (i.e.) from starting root node
expanding
Eg:
w=01C10
DEFINITION:
Here, we take strings from each variables (or) non-terminals concatenated them in a
proper order and infered that the resulting string in the language of the variable (i.e.) starting
1) E=> C
2) E => 0E0
3) E => 1E1
1 C E E => C -
REDUCTION METHOD:
w = 01C10
. .
= 01E10 .
E=>C
. .
= 0E0 .
E=>1E1
=E
1 a I I => a -
2 b I I => b -
3 b0 I I => I0
I => b0 (2)
4 b00 I I => I0
5 a E E => I
I => a (1)
6 b00 E E => I
I => I0
E => I+I
E => (E+E)
E => (I+I)
E => I*E
E => I*(E)
E => I*(E+E)
E => I*(I+I)
E => a*(a+b00)
REDUCTION:
w = a*(a+b00)
. .
= I*(I+I00) .
I=>a, I=>b
. .
= I*(I+I0) .
I=>I0
= I*(I+I)
. .
= E*(E+E) .
E=>I
. .
= E*(E) .
E=>E+E
. .
= E*E .
E=>(E)
. .
=E .
E=>E*E
1) Recursive Inference.
2) Derivation.
3) Parse Tree (or) Derivation Tree based on LMD & RMD.
DEFINTION:
If at each step in derivation, a production is applied to the left most variable (or) left
most non-terminal then the derivation method is called left most derivation.
Eg:
w = id+id*id
E => E+E
E => E*E
E => id
E => E+E
. .
E => id+E .
E=>id
. .
E => id+E*E .
E=>E*E
. .
E => id+id*E .
E=>id
∗
E => id+id*id
𝐺
DEFINITION:
A derivation in which the right most variable is replaced at each step then, the
derivation method is called right most derivation.
Eg:
E => E+E
. .
E => E+E*E .
E=>E*E
. .
E => E+E*id .
E=>id
E => E+id*id
. ∗
E id+id*id
. . =>
𝐺
DERIVATION:
E => 0E0
. .
=>01E10 .
E => 1E1
. .
=> 01C10 .
E => C
. ∗
. .
E ⇒ 01C10
PARSE TREE (OR) DERIVATION TREE:
E
0 0
E
1 1
AMBIGUOUS GRAMMAR:
A grammar that produces more than one parse tree (or) derivation tree for some
sentence, then the grammar is said to be an ambiguous grammar.An ambiguous grammar
produces more than one left most derivation (or) more than 1 RMD then, the given grammar is
set to be an ambiguous grammar.
Eg: L.M.D.
E => E+E
E => E*E
E => (E)
E => id
E => E+E E
. .
E => id+E .
E => id E E
+
E => id+E*E E * E
id
E => id+id*E
id id
E => id+id*id
∗
E => id+id*id
𝑙𝑚𝑑
R.M.D:
E => E+E
=> E+E*E
=> E+E*id
=> E+id*id
=> id+id*id
. ∗
E
. . =>
id+id*id
𝐺
PARSE TREE:
E + E
id E * E
id id
1) For the grammar defined by the productions recognize the following string and also
construct the parse tree.
S => A,B
A =>0A/ξ
B => 0B/1B/ξ
S => A1 S
. .
=>A10B .
B => 0B A 1 B
=> A100B ξ 0 B
. .
=> A1001B .
B => 1B 0 B
. .
=> ξ1001B .
A => ξ 1 B
. .
=> 1001ξ .
B => ξ ξ
.
. .
S => 1001
S => A1B S
. .
=>ξ1B .
A => ξ A 1 B
. .
=> 10B .
B => 0B ξ B
0
=> 100B B
0
. .
=> 1001B .
B => 1B
. . 1
=> 1001ξ .
B => ξ B
ξ
. ∗
. .
S =>1001
𝐺
ii) w = 00101
S => A1B
. .
=>0A1B .
A => 0A
. .
=> 00A1B .
A => 0A
. .
=> 00ξ1B .
A => ξ
=> 001B
. .
=> 0010B .
B => 0B
. .
=> 00101B .
B =>1B
. .
=> 00101ξ .
B => ξ
. ∗
. .
S => 1001
𝐺
PARSE TREE:
A 1 B
0 A 0 B
0 A 1 B
ξ ξ
iii) w = 00101
S => A1B
. .
=>0A1B .
A => 0A
. .
=> 00A1B .
A => 0A
. .
=> 000A1B .
A => 0A
. .
=> 000ξ1B .
A =>ξ
. .
=> 00011B .
B => 1B
. .
=> 00011ξ .
B => ξ
. ∗
. .
S =>00011
𝐺
PARSE TREE:
A 1 B
A 1 B
0
0 A ξ
0 A
ξ
1) Consider the context free grammar whose productions are:
S => aAS/a
∗
and show that S =>aabbaa and also construct the parse tree.
𝐺
S => aAS
. .
=>aSbAS .
A => SbA
. .
=> aabAS .
A => ba
. .
=> aabbaS .
S => a
. ∗
. .
S => aabbaa
𝐺
PARSE TREE:
S
a A S
S b A
a
b a
2) Find LMD & RMD, parse tree for the following grammar. MAY/JUNE 2007
w = 00110101
S => 0B / 1A
A => 0/0S/1AA
B => 1/1S/0BB
L.M.D:
sdfsadfdsa
S => 0B
. .
=> 0BB .
B => 0BB
. .
=> 001B .
B =>1
. .
=> 0011S .
B => 1S
. .
=> 00110B .
S => 0B
. .
=> 001101S .
B => 1S
. .
=> 0011010B .
S => 0B
. .
=> 00110101 .
B => 1
. ∗
. .
S => 00110101
𝑙𝑚𝑑
PARSE TREE:
0 B
0 B B
1 1 S
0 B
1 S
0 B
1
R.M.D:
S => 0B
. .
=>0BB .
B => 0BB
. .
=> 00B1 .
B => 1
. .
=> 001S1 .
B => 1S
. .
=> 0011A1 .
B => 1A
. .
=> 00110S1 .
A => 0S
. .
=> 001101A1 .
S => 1A
. .
=> 00110101 .
A => 0
. ∗
. .
S => 00110101
𝑟𝑚𝑑
PARSE TREE:
0 B
0 B B
1 S 1
1 A
0 S
1 A
0
3) Find the LMD & RMD for the following ‘G’
S => A1B
W=00101
A => 0A/ξ
B => 0B/1B/ξ
L.M.D.:
S => A1B
. .
=>0A1B .
A=> 0A
. .
=> 00A1B .
A => 0A
. .
=> 00ξ1B .
A => ξ
=> 001B
. .
=> 0010B .
B => 0B
. .
=> 00101B .
B => 1B
. .
=> 00101ξ .
B => ξ
=> 00101
. ∗
. .
S => 00101
𝑙𝑚𝑑
PARSE TREE:
A B
1
0 A 0 B
0 A 1 B
ξ ξ
R.M.D:
S => A1B
. .
=>A10B .
B => 0B
. .
=> A101B .
B => 1B
. .
=> A101ξ .
B => ξ
=> A101
. .
=> 0A101 .
A => 0A
. .
=> 00A101 .
A => 0A
. .
=> 00ξ101 .
A => ξ
. ∗
. .
S => 00101
𝑅𝑀𝐷
PARSE TREE:
A 1 B
0 A 0 B
0 A 1 B
ξ ξ
4) Find LMD, RMD and derivation tree for w = a*b+a*b where ‘G’ is given by
L.M.D:
S => S+S
. .
=>S*S+S .
S => S*S
. .
=> a*S+S .
S => a
. .
=> a*b+S .
S => b
. .
=> a*b+S*S .
S => S*S
. .
=> a*b+a*S .
S => a
. .
=> a*b+a*b .
S => b
. ∗
. .
S => a*b+a*b
𝐿𝑀𝐷
PARSE TREE:
S + S
S * S S * S
a b a b
R.M.D.:
S => S+S
. .
=>S*S+S .
S => S*S
. .
=> a*S+S .
S => a
. .
=> a*b+S .
S => b
. .
=> a*b+S*S .
S => S*S
. .
=> a*b+a*S .
S => a
. .
=> a*b+a*b .
S => b
. ∗
. .
S => a*b+a*b
𝑅𝑀𝐷
PARSE TREE:
S + S
S * S S * S
a b a b
A => bS / aAAb
L.M.D.:
S => abSb
. .
=>abab .
S => a
. ∗
. .
S => abab
𝐿𝑀𝐷
PARSE TREE:
a b S b
a
R.M.D.:
S => aAb
. .
=>abSb .
A => Bs
. .
=> abab .
S => a
. ∗
. .
S => abab
𝐿𝑀𝐷
1) Prove that the given ‘G’ is ambiguous for the string “ w = aababa”
S => aSX / b
X => Xb / a
L.M.D:
S => aSX
. .
=>aaSXX .
S => Asx
. .
=> aabXX .
S => b
. .
=> aabXbX .
X => Xb
. .
=> aababX .
X => a
. .
=> aababa .
X => a
. ∗
. .
S => aababa
𝐿𝑀𝐷
PARSE TREE:
S
a S X
a S X a
X
b b
a
R.M.D.:
S => aSX
. .
=>aSa .
X => a
. .
=> aaSXa .
S => Asx
. .
=> aaSXba .
X => Xb
. .
=> aaSaba .
X => a
. .
=> aababa .
X => a
. ∗
. .
S => aababa
𝑅𝑀𝐷
PARSE TREE:
S
a S X
a S X a
X
b b
a
2) Obtain the string “w = ibtibtaea” from the given production by using LMD.
S => iCtS / iCtSeS / a
C => b
S => iCtS
. .
=>ibtS .
C => b
. .
=> ibtiCtSeS .
S => iCtSeS
. .
=> ibtibtSeS .
C => b
. .
=> ibtibtaeS .
S => a
. .
=> ibtibtaea .
S => a
. ∗
. .
S => ibtibtaea
𝐿𝑀𝐷
PARSE TREE:
S
i C S
t
b i C t S e S
b a a
3) Write a grammar to recognize all prefix expressions involving all binary arithmetic
operators. Construct the parse tree for the sentence “-*+abc/dc” from your
grammar. NOV/DEC 2006
E => -EE
E => *EE
E => +EE
E => /EE
E => a / b / c / d / e
E => -EE
. .
=>-*EEE .
E => *EE
. .
=> -*+EEEE .
E => +EE
. .
=> -*+aEEE .
E => a
. .
=> -*+abEE .
E => b
. .
=> -*+abcE .
E => c
. .
=> -*+abc/EE .
E => /EE
. .
=> -*+abc/dE .
E => d
. .
=> -*+abc/de .
E => e
.
. .
E => -*+abc/de
PARSE TREE:
E
- E E
* E E E E
/
+ E E c d e
a b
4) Write a grammar to recognize all prefix expressions involving all binary arithmetic
operators “-*+xyxy”. NOV/DEC 2006
E => -EE
E => *EE
E => +EE
E => x / y
E => -EE
. .
=>-*EEE .
E => *EE
. .
=> -*+EEEE .
E => +EE
. .
=> -*+xEEE .
E => x
. .
=> -*+xyEE .
E => y
. .
=> -*+xbxE .
E => x
. .
=> -*+xyxy .
E => y
.
. .
E => -*+xyxy
PARSE TREE
E
- E E
* E E y
+ E E
x
x y
5) Show that grammar S => aSbS / bSaS / ξ is ambiguous and also write the
language generated by the given grammar.
S => aSbS
=>aξbS
=> abS
. ∗
. .
S ⇒ab
S => aSbS
=>aaSbSbS
=> aaξbSbS
=> aabξbS
=> aabbξ
. ∗
. .
S ⇒aabb
. 𝑛
. .
L(G) = { 𝑎 𝑏𝑛 / n > 1}
6) Write the CFG for the following CFL L(G) = {𝒂𝒎 𝒃𝒏 𝒄𝒑 / m+n=p, m&n>1}
NOV/DEC 2006
E => aEc / bTc / a /bc
T => bTc / bc
E => aEc
. .
=>aaEcc .
E => aEc
. .
=> aabTccc .
E => bTc
. .
=> aabbcccc .
T => bc
. ∗
. .
E => aabbcccc
𝐿𝑀𝐷
7) Consider the alphabet Σ = { a,b,(,),+,*,-, . ,ξ }. Construct a CFG that generate all the
strings in Σ* that are regular expression on the alphabet, Σ. NOV/DEC 2007
E => E+E
E => E*E
E => (E)
E => E.E
E => E-E
E => a / b / ξ
SENTENTIAL FORM:
DEFINITION:
The string’s are derived from the starting non-terminal is called sentential form.
If grammar G=(V,T,P,S) is a context free grammar, then α in (VUT)* such that
non-terminal δ derives α is a sentential form.
∗
S => α then α is left sentential form.
𝑟𝑚𝑑
∗
S => α then α is right sentential form.
𝑙𝑚𝑑
RECURSIVE INFERENCE
1) The recursive inference determines that terminal string ‘w’ is in the language of variable
‘A’.
∗
2) A ⇒ w
∗
3) A => w
𝑙𝑚𝑑
∗
4) A => 𝑤ξξ
𝑟𝑚𝑑
1) BASIS STEP: Let the number of steps used to infer the string is 1, then the tree has a single
leaf node it may be either A => 𝑎1 , 𝑎2 , 𝑎3 , … … … … … . 𝑎𝑛 (or) A => ξ.
PARSE TREE:
A A
ξ 𝑎1 𝑎2 … … … 𝑎𝑛
2) INDUCTION STEP:
Let the number of steps used to derive the step is n+1. Assume that there is a parse
tree for all the variables ‘B’ and string ‘x’ is inferred for the non-terminal ‘B’ using n+1 number
of steps. Consider, that the last step of inference that assumes w is the language of the variable
(or) else non-terminal ‘A’ then A => 𝑋1 ,𝑋2 ,……….𝑋𝑘 .. Then each 𝑋𝑖 is either a variable (or)
terminal string. Then, we have to break up the string w having k components such that w having
k components such that w = 𝑤1 ,𝑤2 ,………..𝑤𝑘.
TWO CASES:
𝑤1 𝑤2 ……….. 𝑤𝑘
PARSE TREE:
𝑥𝑖 => 𝑤𝑖
𝑥𝑖
𝛼1 𝛼2 ………. 𝛼𝑛
Then, the yield is obtained by concatenating the subtrees from left to right.
E => E*E
w = id+id*id E
= E+id*id E * E
= E+E*id E + E
= E+E*E
id id
=E
THEOREM:
PROOF :
∗
To prove that A => w
𝑙𝑚𝑑
1) Basis step.
2) Induction step.
1)BASIS STEP :
If the height of the parse tree is 1, then the parse tree has a root node ‘A’ and yield ‘w’.
A => id
A
w = id
2)INDUCTION STEP:
If the height of the parse tree is n+1. Assume that, there is left most derivative.
∗
A => w
𝑙𝑚𝑑
w
∗
For every parse tree, left most derivation will be A => 𝑥1, 𝑥2 ,………𝑥𝑛 .
𝑙𝑚𝑑
𝑥1 𝑥2 . ………𝑥𝑛 .
𝑤1 𝑤2 𝑤𝑛
TWO CASES:
1) If 𝑋𝑖 is a terminal, then 𝑋𝑖 = 𝑤𝑖 .
2) If 𝑋𝑖 is a variable, then there must be root of some subtrees with yield 𝑤𝑖.
By applying Induction hypothesis, there is a L.M.D.
∗
𝑋𝑖 => 𝑤𝑖
𝐿𝑀𝐷
A => 𝑋1 , 𝑋2 ,……..𝑋𝑘
A => 𝑤1 , 𝑤2 ,……..𝑤𝑖
1) If 𝑋𝑖, is a terminal then there is no need to change any thing.
∗
A => 𝑤1 , 𝑤2 ,……..𝑤𝑘
𝑅𝑀𝐷
2) If 𝑋𝑖, is a variable then derive the string for each 𝑋𝑖, 𝑡𝑜 𝑤𝑖,
=> => => =>
𝑋𝑖 𝐿𝑀𝐷 𝛼1 𝐿𝑀𝐷 𝛼2 ………….𝐿𝑀𝐷 𝛼𝑛 𝐿𝑀𝐷 𝑤𝑖
. ∗
. .
A => 𝑤1 , 𝑤2 ,……..𝑤𝑛
𝐿𝑀𝐷
. ∗
. .
A => w
𝐿𝑀𝐷
For eg:
E => E+E / E*E / id
L.M.D:
E => E+E
=>id+E
=> id+E*E
=> id+id*E
=> id+id*id
.
. .
E => id+id*id
PARSE TREE:
E + E
id E * E
id id
THEOREM:
PROOF :
2)INDUCTION STEP:
A => 𝑋1 , 𝑋2 ,……..𝑋𝑘
∗
⇒w
1) If 𝑋𝑖 is a terminal, then 𝑋𝑖 = 𝑤𝑖 .
2) If 𝑋𝑖 is a variable, then
∗
𝑋𝑖 ⇒ 𝑤𝑖.
A => 𝑋1 , 𝑋2 ,……..𝑋𝑘
∗
A ⇒ 𝑤1 , 𝑤2 ,……..𝑤𝑘
∗
A ⇒w
.
. .
Hence, the theorem is proved.
For eg:
w = 01c10
E => 0E0
=>01E10
=> 01c10
. ∗
. .
E ⇒01c10
APPLICATION OF CFG:
INHERENT AMBIGUOUS:
A context free language ‘L’ is said to be an inherendly ambiguous, if all its grammar are
ambiguous.
w=a A => a
B => a
1) S => A S
S => a A
2) S => B
S => a S
w=aabbccdd
S => AB/C B
A => aAb/ab
a
B => CBd/cd
SC=> AB
=> aCd/aDd
. .
D=>aAbB
=> bDc/bc .
A => Ab
. .
=> aabbB .
A => ab
. .
=> aabbcBd .
B =>cd
. ∗
. .
S ⇒aabbccdd
S => C
. .
=>aCd .
C => aCd
. .
=> aaDdd .
C => aDd
. .
=> aabDcdd .
D =>bDc
. .
=> aabbccdd .
D => bc
.
. .
S => aabbccdd
MODEL OF PDA:
o The PDA consists of a finite set of states, a finite set of input symbols and a finite
set of pushdown symbols.
o The finite control has a control of both the input tape and the pushdown store.
o In one transition of the pushdown automaton,
The control head reads the input symbol, then the go to the new state.
Replace the symbols at the top of the stack by any string.
P=(Q,∑, ├,δ,q0,z0,F)
Where,
Q – A finite non – empty set of states.
∑ - A finite set of inp0ut symbols.
├ – A finite non empty set of stack symbols.
Q0 – q0 in Q is the start state
Z0 – Initial start symbol of the stack;
where,
q,Pi – sates
a - input symbol
γ i – a symbol in ├*
It is the PDA enters state Pi, replaces the symbol z by the string γi and advances the input
head one symbol. The convention used here is the that the state leftmost symbol of γi will be
placed highest on the stack and the rightmost symbol lowest on the stack.
The implementation of
(q,a,Z)=(p1,γ1), (p2, γ2), .......... ,(pM, γ M)
is that independent of the input symbol being scanned, the pda can enter state pi and replaced Z
by γi for any i,i<=i<=M.Here the input head is not advanced.
δ(q0,100C001,z0) |―(q0,00C00,1z0)
|―(q0,0C001,01z0)
|―(q0,C001,001z0)
|―(q1,001,001z0)
|―(q1,01,01z0)
|―(q1,1,1z0)
|―(q1, ξ,z0)
δ(q0,100C001,z0) |* (q2, ξ, ξ)
w=01C10
δ(q0,00C00,z0) |* (q2, ξ, ξ)
w=11C11
δ(q0,11C11,z0) |―(q0,1C11,1z0)
|―(q0,C11,11z0)
|―(q1,11,11z0)
|―(q1,1,1z0)
|―(q1, ξ,z0)
δ(q0,11C11,z0) |* (q2, ξ, ξ)
Transition Diagram:
0,z0->oz0
1, z0->1 z0
0,0->00
1,1->11
0,1->01
0,0-> ξ
1,0->10 1,1-> ξ
ξ,z0 -> ξ
Q0 Q1 Q2
C,0 ->0
C,1 ->1
2. Construct the PDA for the language {WWR/(0+1)*} accepted by empty stack
Tuple Format:
PDA N(M)=({ q0,q1,q2},{0,1},δ, |―,q0,z0,q2)
Transition function:
δ(q0,0,z0) = (q0,0z0)
δ(q0,1,z0) = (q0,1z0)
δ(q0,0,0) = (q0,00) or (q1,ξ)
δ(q0,1,1) = (q0,11) or (q1,ξ) Pushing string into stack
δ(q0,0,1) = (q0,01)
δ(q0,1,0) = (q0,10)
δ(q1,0,0) = (q1,ξ)
δ(q1,1,1) = (q1,ξ) Poping string from stack
δ(q1,ξ,z0) = (q2,ξ)
δ(q1,ξ,R) = (q2,ξ)
Language accepted by empty stack
w=0000
δ(q0,0000,z0) |―(q0,000,0z0)
|―(q0,00,00z0)
|―(q1,0,0z0)
|―(q1,ξ,z0)
δ(q0,0000,z0) |* (q2,ξ,ξ)
W=1001
δ(q0,1001,z0) |―(q0,001,1z0)
|―(q0,01,01z0)
|―(q1,1,1z0)
|―(q1,ξ,z0)
δ(q0,1001,z0) |* (q2,ξ,ξ)
w=0110
1, z0->1 z0
0,0->00
1,1->11
0,1->01
1,0->10
0,0->ξ
1,1-> ξ
Q0 Q1 Q2
ξ,z0 -> ξ
ξ,R ->ξ
0,0 ->ξ
0,1 ->ξ
3.construct the PDA for the language L={anbn/n>=1} accepted by empty stack
Tuple Format:
Transition function:
δ(q0,aaaabbbb,z0) |―(q1,aaabbbb,az0)
|―(q1,aabbbb,aaz0)
|―(q1,abbbb,aaaz0)
|―(q1,bbbb,aaaaz0)
|―(q2,bbb,aaaz0)
|―(q2,bb,aaz0)
|―(q2,b,az0)
|―(q2,ξ,z0)
δ(q0,aaaabbbb,z0) |* (q3,ξ,ξ)
Transition diagram:
a,a->aa b,a ->ξ
4. construct the PDA for the language L={ an b2n/n>=1} accepted by empty stack
Tuple Format: NOV/DEC 2007
Transition function:
δ(q0,a,z0) = (q1,az0)
δ(q1,a,a) = (q1,aa) Pushing string into stack
δ(q1,b,a) = (q2,a)
δ(q2,b,a) = (q1,ξ)
δ(q1,ξ,z0) = (q3,ξ) Poping string from stack
δ(q0,aabbbb,z0) |―(q1,abbbb,az0)
|―(q1,bbbb,aaz0)
|―(q2,bbb,aaz0)
|―(q1,bb,az0)
|―(q2,b,az0)
|―(q1, ξ,z0)
δ(q0,aabbbb,z0) |* (q3,ξ,ξ)
Transition diagram:
a,a->aa
a,z0 ->az0 b,a ->a
Q0 Q1 Q2 Q3
b,a->Ę
Ę,z0 ->Ę
5.construct the PDA for the language L={anbman/n,m >=1} accepted by empty stack
Transition function:
δ(q0,a,z0) = (q1,az0)
δ(q1,a,a) = (q1,aa) Pushing string into stack
δ(q1,b,a) = (q2,a)
δ(q2,b,a)= (q2,,a) separator Just a transition in state
δ(q2,a,a) = (q3,ξ)
δ(q3,a,a) = (q3,ξ) Poping string from stack
δ(q3,ξ,z0) = (q4,ξ)
Transition diagram:
a,a->aa a,a->ξ
z0 ->az0 b,a ->a a,a ->ξ ξ,z0->ξ
Q0 Q1 Q2 Q3 Q4
6. construct the PDA for the language L={an bmcmdn / n,m >=1} accepted by empty stack
Tuple Format:
Transition function:
δ(q0,a,z0) = (q1,az0)
δ(q1,a,a) = (q1,aa)
δ(q1,b,a) = (q2,ba) Pushing string into stack
δ(q2,b,b) = (q2,bb)
δ(q2,c,b) = (q3,ξ)
δ(q3,c,b) = (q3,ξ)
δ(q3,d,a) = (q4,ξ) Poping string from stack
δ(q4,d,a) = (q4,ξ)
δ(q4,ξ,z0) = (q5,ξ)
w=aabbbcccdd
δ(q0,aabbbcccdd,z0) |―(q1,abbbcccdd,az0)
|―(q1,bbbcccdd,aaz0)
|―(q2,bbcccdd,baaz0)
|―(q2,bcccdd,bbaaz0)
|―(q2,cccdd,bbbaaz0)
|―(q3,ccdd,bbaaz0)
|―(q3,cdd,baaz0)
|―(q3,dd,aaz0)
|―(q4,d,az0)
|―(q4,ξ,z0)
δ(q0,aabbbcccdd,z0) |* (q5,ξ,ξ)
Transition diagram:
d, a-> ξ
7. construct the PDA for the language L={ambn c2(m+n)/ n,m >=1} accepted by empty stack
Transition function:
δ(q0,a,z0) = (q1,az0)
δ(q1,a,a) = (q1,aa)
δ(q1,b,a) = (q2,ba) Pushing string into stack
δ(q2,b,b) = (q2,bb)
δ(q2,c,b) = (q3,b)
δ(q2,c,a) = (q3,a)
δ(q3,c,a) = (q2,ξ) Poping string from stack
δ(q2,ξ,z0) = (q4,ξ)
δ(q3,c,b) = (q2,ξ)
Language acceptance by empty stack
m=2 n=3
w=aabbbcccccccccc
δ(q0,aabbbcccccccccc,z0) |―(q1,abbbcccccccccc,az0)
|―(q1,bbbcccccccccc,aaz0)
|―(q2,bbcccccccccc,baaz0)
|―(q2,bcccccccccc,bbaaz0)
|―(q2,cccccccccc,bbbaaz0)
|―(q3,ccccccccc,bbbaaz0)
|―(q2,cccccccc,baaz0)
|―(q3,ccccccc,bbaaz0)
|―(q2,cccccc,baaz0)
|―(q3,ccccc,baaz0)
|―(q2,cccc,aaz0)
|―(q3,ccc,aaz0)
|―(q2,cc,az0)
|―(q3,c,az0)
|―(q2,ξ,z0)
δ(q0,aabbbcccccccccc,z0) |* (q4,ξ,ξ)
Transition diagram:
a,a->aa b,b->bb
c,a ->a
a,z0 ->az0 b,a ->ba c,b ->b
Q0 Q1 Q2 Q3 Q4
c,a ->ξ
c,b->ξ
ξ,z0 ->ξ
8. construct the PDA for the language L={an ba2n / n >=1} accepted by empty stack
Tuple Format:
Transition function:
δ(q0,a,z0) = (q1,az0)
δ(q1,a,a) = (q1,aa) Pushing string into stack
δ(q2,a,a) = (q3,a)
δ(q1,b,a) = (q2,a) separator Just a transition in state
δ(q3,a,a) = (q2,ξ)
δ(q2,ξ,z0) = (q4,ξ) Poping string from stack
δ(q0,aabaaaa,z0) |―(q1,abaaaa,az0)
|―(q1,baaaa,aaz0)
|―(q2,aaaa,aaz0)
|―(q3,aaa,aaz0)
|―(q2,aa,az0)
|―(q3,a,az0)
|―(q2,ξ,z0)
δ(q0,aabaaaa,z0) |* (q4,ξ,ξ)
Transition diagram:
a,a->aa
a,a->ξ
ξ,z0->ξ
9. construct the PDA for the language L={an+1 b2n / n >=1} accepted by empty stack
Tuple Format:
PDA N(M)=({ q0,q1,q2,q3},{a,b},δ, |―,q0,z0,q3)
Transition function:
δ(q0,aaaabbbbbb,z0) |―(q1,aaabbbbbb,z0)
|―(q1,aabbbbbb,az0)
|―(q1,abbbbbb,aaz0)
|―(q1,bbbbbb,aaaz0)
|―(q2,bbbbb,aaaz0)
|―(q1,bbbb,aaz0)
|―(q2,bbb,aaz0)
|―(q1,bb,az0)
|―(q2,b,az0)
|―(q1,ξ,z0)
δ(q0,aaaabbbbbb,z0) |* (q3,ξ,ξ)
Transition diagram:
a,z0->az0
a,a ->aa
a,z0 ->az0 b,a->a
Q0 Q1 Q2 Q3
b,a->ξ
ξ,z0 ->ξ
10. construct the PDA for the language L={an+2 b2n / n >=1} accepted by empty stack
Tuple Format:
PDA N(M)=({ q0,q1,q2,q3},{a,b},δ, |―,q0,z0,q3)
Transition function:
δ(q0,a,z0) = (q1,z0)
δ(q1,a,z0) = (q2,z0) Just a transition of state
δ(q2,a,z0) = (q2,az0)
δ(q2,a,a) = (q2,aa) Pushing string into stack
δ(q2,b,a) = (q3,a)
δ(q3,b,a) = (q2,ξ)
δ(q2,ξ,z0) = (q4,ξ) Poping string from stack
δ(q0,aaaabbbbbb,z0) |―(q1,aaaabbbbbb,z0)
|―(q2,aaabbbbbb,z0)
|―(q2,aabbbbbb,az0)
|―(q2,abbbbbb,aaz0)
|―(q2,bbbbbb,aaaz0)
|―(q3,bbbbb,aaaz0)
|―(q2,bbbb,aaz0)
|―(q3,bbb,aaz0)
|―(q2,bb,az0)
|―(q3,b,az0)
|―(q2,ξ,z0)
δ(q0,aaaabbbbbb,z0) |* (q4,ξ,ξ)
Transition diagram:
a,z0->az0
a,a ->aa
a,z0 ->az0 a,z0 ->az0 Q2 b,a->a Q3 Q4
Q0 Q1
b,a->ξ
ξ,z0 ->ξ
11. construct the PDA for the language L={ambn c(m+n)/ n,m >=1} accepted by empty stack
Tuple Format:
Transition function:
δ(q0,a,z0) = (q1,az0)
δ(q1,a,a) = (q1,aa)
δ(q1,b,a) = (q2,ba) Pushing string into stack
δ(q2,b,b) = (q2,bb)
δ(q2,c,b) = (q3,b)
δ(q2,c,a) = (q3,a)
δ(q3,c,a) = (q2,ξ) Poping string from stack
δ(q3,c,b) = (q2,ξ)
δ(q2,ξ,z0) = (q4,ξ)
δ(q0,aabbbcccccccccc,z0) |* (q4,ξ,ξ)
Transition diagram:
a,a->aa b,b->bb
c,a ->a
a,z0 ->az0 b,a ->ba c,b ->b
Q0 Q1 Q2 Q3 Q4
c,a ->ξ
c,b->ξ
ξ,z0 ->ξ
Tuple Format:
Transition function:
δ(q0,aaaabbbb,z0) |―(q1,aaabbbb,az0)
|―(q1,aabbbb,aaz0)
|―(q1,abbbb,aaaz0)
|―(q1,bbbb,aaaaz0)
|―(q2,bbb,aaaz0)
|―(q2,bb,aaz0)
|―(q2,b,az0)
Transition diagram:
a,a->aa b,a ->ξ
13. construct the PDA for the language L={ an b2n/n>=1} accepted by final state
Tuple Format:
Transition function:
δ(q0,a,z0) = (q1,az0)
δ(q1,a,a) = (q1,aa) Pushing string into stack
δ(q1,b,a) = (q2,a)
δ(q2,b,a) = (q1,ξ)
Poping string from stack
δ(q0,aabbbb,z0) |* (q3,ξ,ξ)
Transition diagram:
a,a->aa
a,z0 ->az0 b,a ->a
Q0 Q1 Q2
b,a->Ę
EQUIVALENCE OF ACCEPTANCE
THEOREM
IF L=N(PN) for some PDA PN = (Q,∑, ├,δ,q0,z0,F)then there is a PDA Pf such that L=L(Pf).
Proof:
The Theorem states that is there is a PDA which has acceptance by empty stack, than there
should be a PDA which also has acceptance by final state.
PN = (Q,∑, ├,δ,q0,z0,F) be a PDA
It is planned to have P f stimulate Pn and find when Pf empties its stack,
ξ,x0|ξ
ξ,x0/zo, x0 PN ξ.x0|ξ Qf
Q0
ξ, x0|ξ
To Prove:
W is in L(Pf) if and only if w is in N(Pn).
If Part
Only if part
Rule(1) causes Pn to enter the initial ID of Pf except that Pf will have its own bottom – of-stack
marker Z0. Which is the symbol of Pf’s stack. It is prove that.
(q0,w,z0├*Pn(q,ξ,ξ) That is w is in N(Pn).
Proof:
Let us simulate Pn from Pf follows.
Pn = (Q,U{qe,qo| }∑,├U{x0},δ|,q0|,x0)
Where δ1 is given as follows
δ1(q01 , ξ , x0) ={(q0,z0,x0)}
δ1(q,a,z) = δ(q,a,z) for all q in q, a in ∑ and z in T
ξ,any| ξ
ξ,any| ξ
Let x ξ L(Pf). Then(q0,x,x0)├*Pf(q0,x,z0x0)
By rule(2) every move of Pn is a legal move Pf.
(q0,x,z0) ├*Pn(q,ξ,δ)
Since Pn simulates Pf and emptying its stack,
(q0,x,x0) ├*(q0,x,z0x0)
├*(qi,ξ, x0)
├*(qe,ξ)
There fore x ∑ L(Pn)
Next, let us show that if x ∑ L(Pf), then the derivation in Pn must be
(q0 1,x,x0) ├ (q0,x,z0x0)
├*Pn(qi,ξ,γx0)
┬*Pn(qe,ξ)
Since we are using x0 at the bottommost of the stack, we get the derivation by removing x0.
(q0,x,z0)┬(qi,ξ,γ)
And qi is the final state. That is, x ∑ L(Pf).
W=0101
PDA N(M)=({q},{0,1}, δ,{0,1,S,A, ξ},qIS,S,qFS}
For non terminals
δ (q, ξ,S)={(q,0S1),(q,A)}
δ (q, ξ,A)={(q,0A1),(q,S)(q, ξ )}
For terminals
δ (q,0,0)=(q, ξ)
δ (q,1,1)= (q, ξ)
δ(q,0101,S) ├ (q,0101,0S1)
├(q,101,S1)
├(q,101,A1)
├(q,101,1A01)
├(q,01,A01)
├(q,01, ξ 01)
├(q,01, 01)
├(q, 1, 1)
δ(q,0101,S) ├*(q, ξ, ξ) Accepted
2. Convert the following CFG into PDA using empty stack(Nov /Dec 07)
SaB/bA
AbAA/aS/a
BaBB/bS/b
For non terminals
δ (q, ξ,S)={(q,aB),(q,bA)}
δ (q, ξ,A)={(q,bAA),(q,aS)(q,a )}
δ (q, ξ,B)={(q,aBB),(q,bS)(q,b )}
For terminals
δ (q,a,a)=(q, ξ)
δ (q,b,b)= (q, ξ)
δ(q,aaabbabbba,S) ├ (q, aaabbabbba,aB)
├(q, aabbabbba,B)
├(q, aabbabbba,aBB)
├(q, abbabbba,BB)
├(q, abbabbba,aBBB)
├(q, bbabbba,BBB)
├(q, bbabbba,bBB)
├(q, babbba,BB)
├(q, babbba,bB)
├(q, abbba,B)
├(q, abbba,aBB)
├(q, bbba,BB)
├(q, bbba,bB)
├(q, bba,bS)
├(q, ba,S)
├(q, ba,bA)
├(q, a,A)
├(q, a,a)
δ(q, aaabbabbba,S) ├*(q, ξ, ξ) Accepted
3. Convert the following CFG into PDA using empty stack
1) E => I 6) I => b
2) E => E+E 7) I => Ia
3) E => E*E 8) I => Ib
4) E => (E) 9) I => I0
5) I => a 10) I => I1
δ (q,1,1)= (q, ξ)
δ(q,a*(a+b00),E) ├ (q, a*(a+b00),E*E)
├(q, a*(a+b00),I*E)
├(q, a*(a+b00),a*E)
├(q, *(a+b00), *E)
├(q, (a+b00),E)
├(q, (a+b00),(E))
├(q, a+b00), E))
├(q, a+b00), E+E))
├(q, a+b00), I+E))
├(q, a+b00), a+E))
├(q, +b00), +E))
├(q, b00), E))
├(q,),))
δ(q, a*(a+b00),E)├*(q, ξ, ξ) Accepted
δ (q,(,()= (q, ξ)
δ (q,),))= (q, ξ)
δ(q,(id+id),E) ├ (q, (id+id),(E))
├(q, id+id), E))
├(q,id+id),E+E))
├(q,id+id),id+E))
├(q,+id),+E))
├(q,id),E))
├(q,id),id))
├(q,),))
δ(q, (id+id),E)├*(q, ξ, ξ) Accepted
5. Convert the following CFG into PDA using empty stack
SSbS/a W=ababa
For non terminals
δ (q, ξ,S)={(q,SbS) (q, a) }
For terminals
δ (q,a,a)=(q, ξ)
δ (q,b,b)= (q, ξ)
δ(q,ababa,S) ├ (q,ababa,SbS)
├ (q,ababa,abS)
├ (q,baba,bS)
├ (q,aba,S)
├ (q,aba,SbS)
├ (q,aba,SbS)
├ (q,aba,abS)
├ (q,ba,bS)
├ (q,a,S)
├ (q,a,a)
δ(q, (id+id),E)├*(q, ξ, ξ) Accepted
6.
From CFG to PDA
Let G=(V,T,P,S) be a CFG
Construct the PDA P that accepts L(G) by empty stack as follows.
P=({q},T,VUT,δ,q,S}
WHERE ℓ is defined by
1. For each variable A,
δ(q,ξ,A)={{q,β)|A->B is a production in G}
2. For every terminal symbol a,
δ(q,aa)={(q, ξ)}
THEOREM
“IF PDA is construted from CFG g, then N(P)=L(G).” NOV/DEC 2004
Solution:
IF Part:
(q,w,S)├*(q,yi, αi)
Basis:
If i=1,γ1=S, then
x1=ξ
y1=w
(q,w,s)├*0mov(q,w,s)
From the rule(1) it is clear that the PDA POSs. The terminal symbols and replaces the variables
by one its productions in the stack. And rule(2) allows us to match any terminals on the top of
the stack with the next symbol. i.e.,δ(q,aa)={(a,ξ)}
Therefore we get the iD(q,yi+1,αi+1) which gives the value for γi+1
Only if part
Let w ∑ N(P). This implies
If (q,x,A)├*p(q,ξ,ξ) then A=>* x
This is proved by induction on the number of moves of P.
Basis:
If it happens in one or more, then there should be A->€ and w=€, So we have can
conclude
A=>* w.
Induction:
-----------------------------------------------------------------
B-----------------------------------------------------------------
a
-----------------------------------------------------------------
x1 x2 x3
In the first move, the PDA replaces the variable A by one of its production A->y1 y2.......yk in G.
In the next n-1 moves, the PDA consumes x by popping each of y1,y2,...from the stack one by
one.
Let x=x1,x2,x3....xk such that xi is the portion of the string consumed by p while popping yi.
(qi,xi xi+1.....xk,yi)├*(q,xi+1....xk,ξ)
Since the number of moves required to do it is less than n, we have
yi=>*xi
If, Yi is a terminal, then yi=xi.
A=y1 y2...yk=>*x1y2...yk=>x1x2....xk
Therefore A=>W,
Therfore N(P)CL(G)
Thus the theorem is proved.
Proof:
Let M be the PDA and G=(V,∑,P,S) be a context free grammar, where
1. S is the start symbool
2. V is the set of objects of the form[q,A,P]
Where q and P in Q
A in ├P is the set of productions which are defined as follows.
1.S->[q0,z0,q] for each q in Q
2. [q,A,qM+1]a[q1,B1,q2][q2,B2,q3]....]..[qM,BM,QM+1]
For each a in ∑Uξ
q1,q2......qM+1 in q
a,b1,b2....bM in ┬
Such that
δ(q,a,A) = (q1,B1B2....BM)
if M=0,δ(q,a,A)=(q1,ξ)then
[q,A,q1]a
The variables and productions of G have been defined in such a way that a leftmost
derivation of G have been defined in such a way that a leftmost derivation in G of a sentence x is
a simulation of PDA M when fed the input x.
The variable that appear in any step of a leftmost derivation in G correspond to the
symbol on the stack of M.
The intension is that [q,A,P]derive x if and only if x causes M to erase an A from its stack
by some sequence of moves beginning in state q and ending in state P
To show that L(G)=N(M)
If PART:
Basisi
If i=1, then
├(q1,y1..yn,B1B2….Bn)
├(q2,y2..yn,B1B2….Bn)
├.
.
.
├(p,ξ,ξ)
Therefore (qj,yj,Bj)├*(qj+1,ξ,ξ)
Thus it produces the production
[qj,Bj,qj+1]yjfor 1≤j≤n
The popping of Bj is shown
If(q,ay,A)├(q1,y,B1B2…Bn)then
[q,A,P]a[q1,B,q2][q2,B2,q3]……[qM,Bn,qn+1]
Therefore [q,A,P]ay1y2….yn=x
Only if part
Suppose [q,A,P]x
This is proved by induction on I the number os steps .
Basis
If i=1 then
[q,x,A]├*(P,ξ,ξ) for i<=j<=n
If we insert Bj+1....Bn at the bottom of each stack in the above sequence of ID’s
(qj,xj,BjBj+1.....Bn)├*(qj+1,ξ,Bj+1....Bn)
From[q,A,P] derivation, we know that
Where δ is given by
δ(q0,0,x) = (q0,xx)
[q0,x,q0]0[q0,x,q0] [q0,x,q0]
[q0,x,q0]0[q0,x,q1] [q1,x,q0]
[q0,x,q1]0[q0,x,q0] [q0,x,q1]
[q0,x,q1]0[q0,x,q1] [q1,x,q1]
δ(q0,1,x) = (q1, ξ)
[q0,x,q1]1
[q1,x,q0]No Production
[q1,x,q1] ξ
[q1,x,q1]1
[q1, z0,q0]No Production
[q1, z0,q1] ξ
Eliminating No Production
[q1,x,q0]No Production
[q0,x,q0]0[q0,x,q0] [q0,x,q0]
[q0,x,q1]0[q0,x,q0] [q0,x,q1]
[q0,x,q1]0[q0,x,q1] [q1,x,q1]
[q0,x,q1]1
[q1,x,q1]ξ
[q1,x,q1]1
[q1, z0,q1] ξ
S[q0, z0,q0]
[q0,x,q0]0[q0,x,q0] [q0,x,q0]
[q0,x,q1]0[q0,x,q0] [q0,x,q1]
[q0,x,q1]0[q0,x,q0] [q0,x,q1]
[q0,x,q1]1
[q1, z0,q1] ξ
[q1,x,q1]ξ
[q1,x,q1]1
[q0,x,q1]0[q0,x,q1][q1,x,q1]
[q0,x,q1]0(1).(1) or 0(1)(ξ)
S [q0, z0,q1]
S 0(1)(ξ) or 0(1)(1)(ξ)
δ is given by
T={0,1}
[q,x,q] ξ
δ(q,1,x) = (q,xx)
[q,x,q]1[q,x,q] [q,x,q]
[q,x,q]1[q,x,p] [p,x,q]
[q,x,p]1[q,x,q] [q,x,p]
[q,x,p]1[q,x,p] [p,x,p]
δ(q,1,x) = (p, ξ)
[q,x,p] 0[p,x,p]
[q,x,q] 0[p,x,q]
[p,x,q]No Production
[p,x,p]1
δ(p,0,z0)=(q,z0)
[p,z0,q]0[q,z0,q]
[p, z0,p][q,z0,p]
Eliminating No Productions:
[p,x,q]No Production
[q,z0,p]1[q,x,q][q, z0,p]
[q, z0,p]1[q,x,p][q, z0,p]
[q,x,q]1[q,x,q][q,x,q]
[q,x,p]1[q,x,q][q,x,p]
[q,x,p]1[q,x,p][p,x,p]
[q,x,p]0[p,x,p]
[q, z0,q]ξ
[p,x,p]1
S[q, z0,q]
[q, z0,q]ξ
[q,x,p]1[q,x,p][p,x,p]
[q,x,p]0[p,x,p]
[p,x,p]1
[p, z0,q]0(ξ)
[q,x,p]0[p,xp]
[q,x,p]0(1)
[q, z0,q]1(0)(1)(0)(ξ)
δ(q0,b,z0) = (q0,0z0)
δ(q0,b,z) = (q0,zz)
δ(q0,b,z) = (q1, ξ)
T={a,b}
S[q0,z0,q0]/[q0,z0,q1]
δ(q0,b, z0)=(q0,zz0)
δ(q0,ξ,z0)={(q0,ξ)}
[q0,z0,q0]ξ
δ(q0,b,z)={(q0,zz)}
[q0,z,q0]b[q0,z,q0][q0,z,q0]
[q0,z,q0]b[q0,z,q1][q1,z,q0]
[q0,z,q1]b[q0,z,q0][q0,z,q1]
[q0,z,q1]b[q0,z,q1][q1,z,q1]
δ(q0,a,z)={(q1,z)}
[q0,z,q1]a[q1,z,q1]
[q0,z,q0]a[q1,z,q0]
[q1,z,q0]No Production
δ(q1,b,z)={(q1,ξ)}
[q1,z,q1]b
δ(q1,a,z0)={(q0,z0)}
[q1,z0,q0]a[q0,z0,q0]
[q1,z0,q1a[q0,z0,q1]
Eliminating No Production
[q1,z,q0]No Production
S[q0,z0,q0]/[q0,z0,q1]
[q0,z0,q0]b[q0,z,q0][q0,z0,q0]
[q0,z0,q0]b[q0,z,q1][q1,z0,q0]
[q0,z0,q1]b[q0,z,q0][q0,z0,q1]
[q0,z0,q1]b[q0,z,q1][q1,z0,q1]
[q0,z0,q0]ξ
[q0,z,q0]b[q0,z,q0][q0,z,q0]
[q0,z,q1]b[q0,z,q0][q0,z,q1]
[q0,z,q1]b[q0,z,q1][q1,z,q1]
[q0,z,q1]a[q1,z,q1]
[q1,z,q1]b
[q1,z0,q0]a[q0,z0,q0]
[q1,z0,q1]a[q0,z0,q1]
After eliminating unwanted variables
S[q0,z0,q0]
[q0,z0,q0]b[q0,z,q1][q1,z0,q0]
[q0,z0,q0]ξ
[q0,z,q1]b[q0,z,q1][q1,z,q1]
[q0,z,q1]a[q1,z,q1]
[q1,z0,q0]a[q0,z0,q0]
[q1,z,q1]b
[q1,z0,q0]a[q0,z0,q0]
[q1,z0,q0]a[ξ]
[q0,z0,q1]a[q1,z,q1]
[q0,z,q1]a[b]
[q0,z,q1]b[q0,z,q1][q1,z,q1]
[q0,z,q1]b.a[b][b]
[q0,z0,q0]b[q0,z,q1][q1,z0,q0]
[q0,z0,q0]b.b.a[b][b] a[ξ]
S[q0,z0,q0]
Sξ orb.b.a.[b][b] a[ξ]
CONTEXT FREE LANGUAGE PDAGRAMMAR
NOV/DEC 2006
Construct a PDA accepting {anbman/m,n≥1} by empty stack. Also construct the corresponding
context free grammar accepting the same set.
L={anbman/ m,n≥1}
a a B b b a a ξ
Stack
a
a
Finite Control
Z0
PDA M=({q0,q1},{a,b},{a,z0},δ,q0,z0,ф)
Where δ is given by
δ(q0,a,z0) = (q1,az0)
δ(q1,a,a) = (q1,aa) Push a’s onto stack
δ(q1,b,a) = (q2,a)
δ(q2,a,a) = (q3,ξ) Read b’s and stack will remain unchanged
δ(q3,a,a) = (q3, ξ)
δ(q3, ξ,z0) = (q4, ξ) Remaining a’s are erased and stack symbol z0 is erased
G=(V,T,P,S) where
V={S,[q0,a,q0],[q0,a,q1],[q1,a,q0],[q1,a,q1],[q0,z0,q0],[q0,z0,q1][q1,z0,q0],[q1,z0,q1]}
T={a,b}
s[q0,z0,q0]/[q0,z0,q1]
δ(q0,a, z0)=(q0,az0)
δ(q0,a, a)=(q0,aa)
δ(q0,b, a)=(q0,a)
[q0, a,q1]b[q1,a,q1]
[q0, a,q0]b[q1,a,q0]
[q1, a,q1]a
[q1, a,q1]b[q1,a,q1]
[q1, a,q0]b[q1,a,q0]
δ(q!,ξ, z0)={(q1,ξ)}
[q1,z0,q1]ξ
Eliminating No Production
[q1,z0,q0]No Production
S[q0,z0,q0]/[q0,z0,q1]
[q0,z0,q0]a[q0,a,q0][q0,z0,q0]
[q0,z0,q1]a[q0,a,q0][q0,z0,q1]
[q0,z0,q1]a[q0,a,q0][q0,z0,q1]
[q0,z0,q1]a[q0,a,q1][q1,z0,q1]
[q0,a,q0]a[q0,a,q0][q0,a,q0]
[q0,a,q0]a[q0,a,q1][q1,a,q0]
[q0,a,q1]a[q0,a,q0][q0,a,q1]
[q0,a,q1]a[q0,a,q1][q1,a,q1]
[q0,a,q1]b[q1,a,q1]
[q0,a,q0]b[q1,a,q0]
[q1,a,q1b[q1,a,q1]
[q1,a,q0]b[q1,a,q0]
[q1,z0,q1]ξ
S[q0,z0,q1]
[q0,z0,q1]a[q0,a,q1][q1,z0,q1]
[q0,a,q1]a[q0,a,q1][q1,a,q1]
[q0,a,q1]b[q1,a,q1]
[q1,a,q1]b[q1,a,q1]
[q1,a,q1]a
[q1,z0,q1]ξ
[q1,a,q1]b[a]
[q0,a,q1]b[a] or b.b[a]
[q0,a,q1]a[q0,a,q1][q1,a,q1]
[q0,a,q1]a.b[a][a]
[q0,z0,q1]a[q0,a,q1][q1,z0,q1]
[q0,z0,q1]ab[a][ξ]
S[q0,z0,q1]
Sa.b[a][ξ]
n=2,aabb
δ(q0,aabb,z0) |―(q1,abb,az0)
|―(q1,bb,aaz0)
|―(q2,b,az0)
|―(q2, ξ,z0)
δ(q0,aabb,z0) |* (q3, ξ, ξ)
Deterministic Push Down Automata:
A PDA is deterministic if it never has a choice of move for
given state, input symbol(including ξ) and a stack symbol(├).Also it never has choice
between making a move using a true input and a move using ξ input.
Language Accepted by DPDA’s:
All the regular languages are accepted (by final state) by
DPDA’s and a there are non regular languages accepted by DPDA’s.
The DPDA languages are CFL and infact languages that
have unambiguous CFG’s .Thus , the DPDA languages lie strictly between the regular
languages and the CFL’s.
TWO MARKS QUESTION AND ANSWERS
1.Let G=({S,C},{a,b},P,S} where P consist of S -> aCa,C->aCa|b.Find L(G).
Solution:
S → aCa
→ aiCai
→ an ban since C → b
solution:
S → aAS
→ aabAS Since S → a a A S
→ aabbaS Since A → ba S b A
→ aabbaa Since S → a a b a a
S → 0S1
→ 00S11
.
→ 0n S 1n
S → 0n 1n
If vertex n has label λ,then n is a leaf and is the only son of its father.
Solution:
APR/MAY’08,MAY/JUNE’ 07
Solution:
S → aB
S → aaBB
S → aaaBBB
S → aaabbB
S → aaabbaBB
S → aaabbabB
S → aaabbabbS
S → aaabbabbbA
s *→ aaabbabbba
LMD
S → aSb
→ aaSbb
→ aiSbi
→ ai abbi
→ an bn
L(G) = { an bn | n≥ 1}
S → aCa
→ aaCaa
→ an C an
S → anban
9.Show that id+id*id can be generated by two distinct leftmost derivation in the grammer
-E → E+E|E*E|(E)|id.
→ id +E → E +E*E
→ id + E*E → id + E * E
→ id + id *E → id + id *E
→ id + id*id → id + id * id
10.Define PDA.
P = (Q, ∑ , ┌ , δ ,q0,z0,F)
Where
δ - Transition States.
q0 – Initial state.
F – Final state.
11.What are the different ways of language acceptance by a PDA and defin them?
A → a | aaA | abBc
B → abbA | b
Using the suggested substitution fro the variable B,we get production G^ with
productions.
A → a | aaA | ababbAc | abbc
B → abbA | b
Solution:
(i) using G.
A → aaA
→ aabBc
→ aaabbc
(ii) using G^
A → aaA
→ aaabbc
13.Write a CFG for the set of strings which does not produce any palindromes.
Solution:
S → aSa | bSb
S→C
C → aAb | bAa A → aA | bA | ε
S → aSb | aAb
A → bAa
A → ba
Solution:
S → aSb
→ aaSbb
→ anSbn
→ an aAbbn
→ anabAabbn
→ anbmambn
L = { anbmambn , n,m ≥ 1}
G = ({S,A,B},{a,b},P,S}
Where P is given by
S → aA | bB
A → ab
B → aBb | a
Solutions:
b A
a b
S → aSbb
S → aab
Solution:
The PDA is given by
A = ({q},{a,b},{S,A,B,Z,a,b},δ,q,S}
Where δ is given by
δ(q,z,S)={(q,aABB)}
δ(q,z,A)={(q,aB),{q,a}}
δ(q,z,B)={(q,bA),(a,b)}
δ(q,a,a)={(q,ε)}
δ(q,b,b)={(q,ε)}
A data structure to represents the source program in a compiler is called Parse Tree.A
parse tree can have nodes and edges.
Eg: a: = b + c
:=
a +
b c
P = ({q},T ,V U T ,δ , q, S)
where δ is given by
22.Is it true that non – deterministic PDA is more powerful thatn deterministic
PDA?Justify?
No ,NPDA is not more powerful than DPDA .Because,NPDA may produce ambiguous
grammer by reaching its final state or by emptying its stack.But DPDA produces only
unambiguous grammer.
23.What is the additional feature PDA has when compared with NFA?Is PDA superior
over NFA in the sense of language acceptance? justify?
Stack which is used to store the necessary tape symbols and use the state to
remember the conditions.
Two ways of language acceptances,one by reaching its final state and another by
emptying its stack.