Sie sind auf Seite 1von 43

Unit– 3– 4

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

 A language is a context free Language (CFL) if there is a CFG so


that .

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
Sa | b
• Write CFG for a+
S aS | a
• Write CFG for a*
S aS | ^
• Write CFG for (ab)*
SabS | ^
• 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*
SaX
X˄| bX
• Write CFG for regular expression a*b*
SXY
XaX|˄
YbY|˄
• Write CFG for regular expression (a+b)*
SaS | bS | ^
• Write CFG for regular expression a(a+b)*
SaX
XaX | 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*
SA | B
A˄| aA
B^ |bB
• Write CFG for regular expression (011+1)*(01)*
SAB
A011A | 1A | ^
B01B | ^
• 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.
SA1A1A1A
A0A | 1A | ^
• Write CFG that must start and end with same symbol.
S0A0 | 1A1
A0A | 1A | ^
• The language of even & odd length palindrome string over {a,b}
SaSa|bSb|a|b|˄
• No. of a and no. of b are same
SaSb|bSa|˄
• The language of {a, b} ends in a
SaS | 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)*
SXaXaX
XaX|bX|˄
• Write CFG for number of 0’s and 1’s are same (n0(x)=n1(x))
S0S1 | 1S0 | ^
• Write CFG for L={aibjck | i=j or j=k}
For i=j for j=k
SAB SCD
AaAb | ab CaC | a
BcB | c DbDc | 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}
SABC
AaAb |˄
BbB | b
CbCc |˄
• Write CFG for L={ 0i1j0k | j>i+k}
SABC
A0A1 |˄
B1B | 1
C1C0 |˄
• Write CFG for the language of Algebraic expressions
SS+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> | …

<if-statement> if ( <expression> ) <statement>

<for-statement> for ( <expression>; <expression>; <expression> ) <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,

where and are Nonterminal and is terminal.

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

At last, all the incoming


transitions to the accepting states
are designated by the production
Source State → input symbol
Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
15
15 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Exercise: FA to Regular Grammar
b

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
SS+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
SS+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
SA1B
A0A | �
B0B | 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. SA1B
A0A | �
B0B | 1B | � (String: 1001) Perform leftmost derivation.
2. EE+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: SS+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. SSS+ | 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
Sa X |Yb
X ˄ | S Nullable variable={X}
YbY|b

Replacing X by ^ in all
SaX | Yb | a^
productions containing X
X^ | S on RHS and rewriting
YbY|b the production again

SaX|Yb|a Removing ^ productions


XS and production of the
YbY|b form AA

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
SAC
AaAb|˄
CaC|a
Grammar after elimination of ^ production:
SAC | C
AaAb| ab
CaC|a
SXaX|bX|Y
XXaX|XbX|˄
Yab
Grammar after elimination of ^ production:
S XaX | bX | Y | aX | Xa | a | b
X XaX |XbX | aX | Xa | a | Xb | bX | b
Yab

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.

SA SA
SB AB
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 AB 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
SABA|BA|AA|AB|A|B
Unit Productions
A aA|a
B bB|b are SA and SB

SABA|BA|AA|AB |aA|a|bB|b Removing unit


AaA|a
BbB|b productions

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
Aa
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)
SAAC
AaAb|˄
CaC|a
Step 1: Elimination of ^ production

Eliminate A^
SAAC| AC | C
AaAb| ab
CaC|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 SC
SAAC|AC| aC|a
C
AaAb|ab
CaC|a

Step-3: Replace all mixed string with solid NT

aC|a
SAAC|AC| PC
A PAQ|PQ
aAb|ab
C PC|a
aC|a
Pa
Qb

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
SAX1
X1AC SAAC|AC|PC|a
SAC|PC|a APAQ|PQ
CPC|a
APY1
Pa
Y1AQ Qb
APQ
CPC|a
Pa
Qb

Chomsky Normal Form


Unit
Unit –– 3:
3: Context
Context Free
Free Grammar
Grammar Darshan
39
39 Institute
Darshan Institute of
of Engineering
Engineering &
& Technology
Technology
Example: CFG to CNF
SaAbB
AAb|b
BBa|a
Step 1 is not required as there is no ^ productions
Step 2 is not required as there is no unit productions
Step-3: Replace all mixed string with solid NT:
SPAQB Step-4 : Shorten the string of NT to length 2
AAQ|b SPT1 T1AT2 T2QB
BBP|a AQB|b
Pa BBP|a
Qb Pa
Qb

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
SbA|aB
AbAA|aS|a
BaBB|bS|b
Step 1 & 2 are not required as there is no ^ and Unit productions
Step-3:Replace all mixed string with solid NT:
SQA|PB
AQAA|PS|a
BPBB|QS|b
Pa
Qb
Step-4 : Shorten the string of NT to length 2
SQA|PB
AQT1|PS|a T1AA
BPV1|QS|b V1BB
PaQb

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
SAA
AB|BB
BabB|b|bb
Step 1 is not required as there is no ^ productions
Step-2: Eliminate Unit Production:
SAA Step-4 : Shorten the string of NT to length 2
SAA
A abB|b|bb|BB A PT1|b|QQ|BB T1QB
BabB|b|bb B PV1|b|QQ V1QB
Step-3:Replace all mixed string with solid NT: Pa
SAA Qb
A PQB|b|QQ|BB
B PQB|b|QQ
Pa
Qb

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

Das könnte Ihnen auch gefallen