0% found this document useful (0 votes)
303 views23 pages

Unit III 1.CFG Introduction Veltech

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
303 views23 pages

Unit III 1.CFG Introduction Veltech

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Unit III

CFG and PDA


Introduction
•Finite Automata accept all regular languages and only regular
languages

•Many simple languages are non regular:

- {anbn : n = 0, 1, 2, …}
- {w : w a is palindrome}

and there is no finite automata that accepts them.


• context-free languages are a larger class of languages that encompasses all regular
languages and many others, including the two above.
Context-Free Grammars
• Languages that are generated by context-free grammars are
context-free languages

• Context-free grammars are more expressive than finite


automata: if a language L is accepted by a finite automata
then L can be generated by a context-free grammar
• Beware: The converse is NOT true
Context-Free Grammar
Definition. A context-free grammar is a 4-tuple
G = (V, S, P, S)
where:
V = a finite set of variables or Non terminals
 = a finite set of terminals (S)

( is an alphabet (each character in  is called terminal))

P = a finite set of productions


S = start variable SV
Representation of symbols
1.V--Variables(Non Terminals)- Uppercase letters
EX : A ,B C,….
X ,Y, Z….
2.T- alphabet(Terminals)- lowercase letters
Ex : digits, letters, operators, special symbols
3.P – Productions rules
A-> 
A is a variable or Nonterminal
Where ,,γ – string of terminals and/or nonterminals
4. S- start symbol
Belongs to variable
CFG example 1
1. SABC
2. Aa
3. B1|0
4. C->b
V = Variables (NT)= {S,A,B,C}
T = Terminals ={a,b,1,0}
P = Productions={SABC,Aa, B1|0, C->b }
S = Start symbol= {S}
CFG example 2
1. EABC
2. Aa|b
3. B+|*
4. C1|2
V = Variables (NT)= {E,A,B,C}
T = Terminals ={a,b,+,*,1,2}
P = Productions={EABC, Aa|b, B+|*, C1|2}
S = Start symbol= {E}
Context-Free Languages (CFL)
Definition. Given a context-free grammar
G = (V,T,P,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 = (V,T,P,S), such that L is
generated from G
CFG example 1
1. SABC
2. Aa
3. B1|0
4. C->b
V = Variables (NT)= {S,A,B,C}
T = Terminals ={a,b,1,0}
P = Productions={SABC,Aa, B1|0, C->b }
S = Start symbol= {S}
CFL example 1
G:
L(A)={a}
SABC L(B)={1,0}
Aa L{C}={b}
B1|0 L(S)={a1b,a0b}
C->b
Language of CFG is
L(S)={a1b,a0b}
CFL example 2
G: L(A)={a,b}
EABC L(B)={+,*}
Aa|b L{C}={1,2}
B+|*
L(E)={a+1,a+2,
C1|2
b+1,b+2, a*1,a*2,
Language of CFG is
b*1,b*2}
L(E)={a+1,a+2, b+1,b+2, a*1,a*2, b*1,b*2}
CFL example 3
G:
AaA AaA AaA
EA Aab AaaA AaaA
Aaab AaaaA
AaA|b Aaaab

L(A)={b,ab,aab,aaab,aaaab…}
L(E)={b,ab,aab,aaab,aaaab…}
Language of CFG is
L(E)={b,ab,aab,aaab,aaaab…}
CFL example 4
G: E
E E
EE+E  E+E  E+E  E+E
Ea  a+a  E+E+E  E+E+E
 a+a+a  E+E+E+E
 a+a+a+a
Language of CFG is
L(E)={a, a+a,a+a+a,a+a+a+a,….}
CFG example 5
G:
E1|11A
L(A)={1}
A1
L(E)={1,111,11111,1111111,…}

Language of CFG is
L(E)={1,111,11111,1111111,…}
Odd no. of 1’s Odd no. of 1’s
E1|11E
CFL example 6
G:
E11E| e
L(A)={1}
A1
L(E)={e ,11,1111,111111,…}
e

Language of CFG is
L(E)={e ,11,1111,111111,…}
Even no. of 1’s E e |11E
Construction of CFG
L={an|n>=1} String : a
Sa
CFG S->a|aS
String : aaaa
SaS
SaaS
SaaaS
Saaaa
Construction of CFG
L={an|n>=0} String : a
Sa
CFG S->aS|
String : aaaa
SaS
SaaS
SaaaS
SaaaaS
Saaaae
Saaaa
String : aaa
SaaS
Saaa

String :
aaaa
SaaS
SaaaaS
Saaaae
String :
aaabbb

SaSb
SaaSbb
Saaabbb
Odd:
String : aabaa

SaSa
SaaSaa
Saabaa
Find CFG?
L={am bn cm+n | m,n0}
String : abcc

S  a S c|A SaSc
SabAcc
A  b A c| e Sab e cc
Sabcc

L={e ,abcc,aabbcccc,abbccc,aaabcccc,aaabbbccccc,…}
HW?
L={am bn cm+n | m,n1}

L={abcc,aabbcccc,abbccc,aaabcccc,aaabbbccccc,…}

You might also like