Beruflich Dokumente
Kultur Dokumente
Unit
Pushdown
Context Free Grammar
(CFG)
Automata
Prof. Dixita Kagathara
dixita.kagathara@darshan.ac.in
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
11 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Context Free Grammar
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
2Darshan
2 Institute
Institute of
of Engineering
Engineering &
& Technology
Technology
Context Free Grammar
A context free grammar (CFG) is a 4-tuple where,
is finite set of non terminals,
is disjoint finite set of terminals,
is an element of and it’s a start symbol,
is a finite set formulas of the form where and .
Application of CFG:
1. CFG are extensively used to specify the syntax of
programming language.
2. CFG is used to develop a parser.
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
33 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Context Free Language
Let be a CFG. The language generated by is
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
44 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
CFG Examples
• Write CFG for either a or b
Sa | b
• Write CFG for a+
S aS | a
• Write CFG for a*
S aS | ^
• Write CFG for (ab)*
SabS | ^
• Write CFG for any string of a and b
S aS | bS | a | b
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
55 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
CFG Examples
• Write CFG for ab*
SaX
X˄| bX
• Write CFG for regular expression a*b*
SXY
XaX|˄
YbY|˄
• Write CFG for regular expression (a+b)*
SaS | bS | ^
• Write CFG for regular expression a(a+b)*
SaX
XaX | bX | ^
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
66 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
CFG Examples
• Write CFG for a* | b*
SA | B
A˄| aA
B^ |bB
• Write CFG for regular expression (011+1)*(01)*
SAB
A011A | 1A | ^
B01B | ^
• Write CFG for balanced parenthesis
S [] | {} | [s] | {s} | ^
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
77 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
CFG Examples
• Write CFG which contains at least three times 1.
SA1A1A1A
A0A | 1A | ^
• Write CFG that must start and end with same symbol.
S0A0 | 1A1
A0A | 1A | ^
• The language of even & odd length palindrome string over {a,b}
SaSa|bSb|a|b|˄
• No. of a and no. of b are same
SaSb|bSa|˄
• The language of {a, b} ends in a
SaS | bS |a
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
88 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
CFG Examples
• Write CFG for regular expression (a+b)*a(a+b)*a(a+b)*
SXaXaX
XaX|bX|˄
• Write CFG for number of 0’s and 1’s are same (n0(x)=n1(x))
S0S1 | 1S0 | ^
• Write CFG for L={aibjck | i=j or j=k}
For i=j for j=k
SAB SCD
AaAb | ab CaC | a
BcB | c DbDc | bc
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
99 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
CFG Examples
• Write CFG for L={ aibjck | j>i+k}
SABC
AaAb |˄
BbB | b
CbCc |˄
• Write CFG for L={ 0i1j0k | j>i+k}
SABC
A0A1 |˄
B1B | 1
C1C0 |˄
• Write CFG for the language of Algebraic expressions
SS+S | S*S | S-S | S/S | (S) | a
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
10
10 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
CFG Examples
• Write CFG for syntax of programming language
<statement> … | <if-statement> | <for-statement> | …
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
11
11 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Recursive Definition
1. Recursive Definition of {a,b}*
˄∈L
For any S∈L, Sa∈L
For any S∈L, Sb∈L
No other strings are in L.
2. Recursive Definition of Palindrome (pal)
˄, a, b ∈ pal
For any S ∈ pal , aSa ∈ pal and bSb ∈ pal
No other string are in pal
3. Recursive Definition of the language {anbn / n≥0}
˄∈L
For every X ∈ L, aXb ∈L
No other strings are in L
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
12
12 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Regular Grammar
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar 13
Darshan
Darshan Institute
13 Institute of
of Engineering
Engineering &
& Technology
Technology
Regular Grammar
A grammar is regular if every production takes one of the two
forms,
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
14
14 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
FA to Regular Grammar
0 1
1
0
a B a
b
b
A C
b b
a a
a
E D
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
16
16 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Derivation trees & Ambiguity
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar 17
Darshan
Darshan Institute
17 Institute of
of Engineering
Engineering &
& Technology
Technology
Derivation
Derivation is used to find whether the string belongs to a given
grammar or not.
Types of derivations are:
1. Leftmost derivation
2. Rightmost derivation
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
18
18 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Leftmost Derivation
• A derivation of a string W in a grammar G is a left most derivation
if at every step the left most non terminal is replaced.
• Let us consider a Grammar
SS+S | S-S | S*S | S/S | (S) | a
• String: a*a-a
S
S
S-S S - S
S*S-S
a*S-S S * S a
a*a-S
a*a-a a a
Leftmost Derivation Tree
Leftmost Derivation
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
19
19 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Rightmost Derivation
• A derivation of a string W in a grammar G is a right most
derivation if at every step the right most non terminal is replaced.
• Let us consider a Grammar
SS+S | S-S | S*S | S/S | (S) | a
• String: a*a-a
S
S
S*S S * S
S*S-S
S*S-a a S - S
S*a-a
a*a-a a a
Rightmost Derivation Tree
Rightmost Derivation
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
20
20 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Derivation
SA1B
A0A | �
B0B | 1B | � Perform leftmost & Rightmost derivation.
(String: 00101)
Leftmost Derivation Rightmost Derivation
S S
A1B A1B
A10B
0A1B
A101B
00A1B A101
001B 0A101
0010B 00A101
00101B 00101
00101
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
21
21 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Exercise
1. SA1B
A0A | �
B0B | 1B | � (String: 1001) Perform leftmost derivation.
2. EE+E | E*E | id | (E) | -E
String : id + id * id. Perform rightmost derivation
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
22
22 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Ambiguous Grammar
A context free grammar G is ambiguous if there is at least one
string in L(G) having two or more distinct derivation tree.
Ex: SS+S | S-S | S*S | S/S | (S) | a
String: a+a*a
S S
S*S S+S
S+S*S a+S
a+S*S a+S*S
a+a*S a+a*S
a+a*a a+a*a
Here, we have two left most derivation for string a+a*a hence, above
grammar is ambiguous.
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
23
23 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Exercise
Check whether following grammars are ambiguous or not:
1. S aS | Sa | � (string: aaaa)
2. S aSbS | bSaS | � (string: abab)
3. SSS+ | SS* | a (string: aa+a*)
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
24
24 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Unambiguous Grammar
S S+S | S*S | (S) | a
S S+T | T Equivalent
T T*F | F Unambiguous
String: a+a*a
F (S) | a grammar
S
S+T
T+T
F+T
a+T
Here, we do not have two left most derivation for
a+T*F
string a+a*a hence, grammar is Unambiguous.
a+F*F
a+a*F
a+a*a
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
25
25 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Simplified forms & Normal forms
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar 26
Darshan
Darshan Institute
26 Institute of
of Engineering
Engineering &
& Technology
Technology
Nullable Variable
A Nullable variable in a CFG, is defined as follows:
1. Any variable A for which P contains is nullable.
2. If P contains the production are nullable variable, then A is
nullable.
3. No other variables in V are nullable.
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
27
27 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Elimination of ^ productions
Given a CFG , construct a CFG with no ^- production as follows.
1. Initialize P1 to be P.
2. Find all nullable variable in V.
3. For every production in P, add to P1 every production that can be
obtained from this one by deleting from one or more of the
occurrences of nullable variables in .
4. Delete all the empty productions from P1. also delete any
duplicates, as well as production of the form
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
28
28 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Eliminate ˄ production
Sa X |Yb
X ˄ | S Nullable variable={X}
YbY|b
Replacing X by ^ in all
SaX | Yb | a^
productions containing X
X^ | S on RHS and rewriting
YbY|b the production again
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
29
29 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Example: Eliminate ^ production
SAC
AaAb|˄
CaC|a
Grammar after elimination of ^ production:
SAC | C
AaAb| ab
CaC|a
SXaX|bX|Y
XXaX|XbX|˄
Yab
Grammar after elimination of ^ production:
S XaX | bX | Y | aX | Xa | a | b
X XaX |XbX | aX | Xa | a | Xb | bX | b
Yab
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
30
30 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
A-derivable
A variable is called A-derivable ,
1. If is a production, B is A-derivable.
2. If C is A-derivable, is a production, and , then B is A-derivable.
3. No other variables are A-derivable.
SA SA
SB AB
S-derivable={A,B} S-derivable={A,B}
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
31
31 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Unit Production & Elimination of Unit productions
A production of the form AB is termed as unit production.
Where A & B are nonterminals.
Algorithm
Given a CFG with no ^ productions, construct a CFG having no
unit production as follows.
1. Initialize P1 to be P.
2. For each A ∈ V ,finding the set of A derivable variable.
3. For every pair (A, B) such that B is A- derivable and every non unit
production Bα, add the production Aα to P1 if it is not
already present in P1.
4. Delete all unit productions from P1.
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
32
32 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Elimination of unit production
SABA|BA|AA|AB|A|B
Unit Productions
A aA|a
B bB|b are SA and SB
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
33
33 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Chomsky Normal Form
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar 34
Darshan
Darshan Institute
34 Institute of
of Engineering
Engineering &
& Technology
Technology
Chomsky Normal Form (CNF)
A context free grammar is in Chomsky normal form (CNF) if every
production is one of these two forms:
A BC
Aa
Where A, B, and C are nonterminals and a is terminal.
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
35
35 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Converting CFG to CNF
Steps to convert CFG to CNF
1. Eliminate ˄-Productions
2. Eliminate Unit Productions
3. Restricting the right side of productions to single terminal or
string of two or more nonterminals
4. Final step of CNF (shorten the string of NT to length 2)
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
36
36 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Example (CFG to CNF)
SAAC
AaAb|˄
CaC|a
Step 1: Elimination of ^ production
Eliminate A^
SAAC| AC | C
AaAb| ab
CaC|a
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
37
37 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
CFG to CNF
Step-2: Eliminate Unit Production
Unit Production is SC
SAAC|AC| aC|a
C
AaAb|ab
CaC|a
aC|a
SAAC|AC| PC
A PAQ|PQ
aAb|ab
C PC|a
aC|a
Pa
Qb
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
38
38 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
CFG to CNF
Step-4 : Shorten the string of NT to length 2
SAX1
X1AC SAAC|AC|PC|a
SAC|PC|a APAQ|PQ
CPC|a
APY1
Pa
Y1AQ Qb
APQ
CPC|a
Pa
Qb
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
40
40 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Example: CFG to CNF
SbA|aB
AbAA|aS|a
BaBB|bS|b
Step 1 & 2 are not required as there is no ^ and Unit productions
Step-3:Replace all mixed string with solid NT:
SQA|PB
AQAA|PS|a
BPBB|QS|b
Pa
Qb
Step-4 : Shorten the string of NT to length 2
SQA|PB
AQT1|PS|a T1AA
BPV1|QS|b V1BB
PaQb
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
41
41 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Example: CFG to CNF
SAA
AB|BB
BabB|b|bb
Step 1 is not required as there is no ^ productions
Step-2: Eliminate Unit Production:
SAA Step-4 : Shorten the string of NT to length 2
SAA
A abB|b|bb|BB A PT1|b|QQ|BB T1QB
BabB|b|bb B PV1|b|QQ V1QB
Step-3:Replace all mixed string with solid NT: Pa
SAA Qb
A PQB|b|QQ|BB
B PQB|b|QQ
Pa
Qb
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
42
42 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
End of Unit - 3
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar 43
Darshan
Darshan Institute
43 Institute of
of Engineering
Engineering &
& Technology
Technology