Chapter 3 Context Free Language
Chapter 3 Context Free Language
4
Context-Free Grammar: G (V , T , S , P )
A s
Variable String of
variables and
terminals
5
Example of Context-Free Grammar
S aSb |
productions
P {S aSb, S }
G V , T , S , P
V {S }
T {a, b} start variable
variables
terminals
6
Language of a Grammar:
For a grammarG S
with start variable
*
L(G ) {w : S w, w T *}
String of terminals or
7
Example:
n n
L(G ) {a b : n 0}
9
Example:
n n
L {a b : n 0}
is a context-free language
since context-free grammarG :
S aSb |
generates L(G ) L
10
Another Example
Context-free grammar G :
S aSa | bSb |
Example derivations:
S aSa abSba abba
S aSa abSba abaSaba abaaba
R
L(G ) {ww : w {a, b}*}
Palindromes of even length
11
Another Example
Context-free grammar G :
S aSb | SS |
Example derivations:
S SS aSbS abS ab
S SS aSbS abS abaSb abab
1. S AB 2. A aaA 4. B Bb
3. A 5. B
13
1. S AB 2. A aaA 4. B Bb
3. A 5. B
1 2 3 4 5
S AB aaAB aaB aaBb aab
1 4 5 2 3
S AB ABb Ab aaAb aab
At each step, we substitute the
rightmost variable
15
1. S AB 2. A aaA 4. B Bb
3. A 5. B
17
S AB A aaA | B Bb |
S AB
S
A B
yield AB
18
S AB A aaA | B Bb |
S AB aaAB
S
A B
yield aaAB
a a A
19
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
yield aaABb
20
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb
S
A B
a a A B b
yield
aaBb aaBb
21
S AB A aaA | B Bb |
S AB aaAB aaABb aaBb aab
Derivation Tree S
(parse tree)
A B
a a A B b
yield
aab aab
22
Sometimes, derivation order doesn’t matter
Leftmost derivation:
S AB aaAB aaB aaBb aab
Rightmost derivation:
S AB ABb Ab aaAb aab
S
Give same A B
derivation tree
a a A B b
23
Ambiguity
24
Grammar for mathematical expressions
E E E | E E | (E) | a
Example strings:
(a a) a (a a (a a))
25
E E E | E E | (E) | a
E E E aE aEE
E
a a E a a*a
E E
A leftmost derivation
for a a a
a E E
a a
26
E E E | E E | (E) | a
a a
27
E E E | E E | (E) | a
E E E E
a E E E E a
a a a a
28
take a 2
a a a 2 2 2
E E
E E E E
2 E E E E 2
2 2 2 2
29
Good Tree Bad Tree
2 2 2 6 2 2 2 8
6 Compute expression result 8
E using the tree E
2 4 4 2
E E E E
2 2 2 2
2 E E E E 2
2 2 2 2
30
Two different derivation trees
may cause problems in applications which
use the derivation trees:
• Evaluating expressions
• In general, in compilers
for programming languages
31
Ambiguous Grammar:
A context-free grammar G is ambiguous
if there is a string w L(G ) which has:
E E E E
a E E E E a
a a a a
33
E E E | E E | (E) | a
his grammar is ambiguous also because
string a a a has two leftmost derivations
E E E aE aEE
a a E a a*a
Variables Terminals
37
A successful example:
Equivalent
Ambiguous
Non-Ambiguous
Grammar
Grammar
E E E
E E T |T
E E E
T T F | F
E (E )
E a F (E ) | a
F F a
Unique
derivation tree
for a a a a a
39
An un-successful example:
n n m n m m
L {a b c } {a b c }
n, m 0
L is inherently ambiguous:
every grammar that generates this
language is ambiguous
40
Example (ambiguous) grammar forL :
n n m n m m
L {a b c } {a b c }
S S1 | S 2 S1 S1c | A S 2 aS2 | B
A aAb | B bBc |
41
The string a nb nc n L
has always two different derivation trees
(for any grammar)
For example
S S
S1 S2
S1 c a S2
42
Simplifications
of
Context-Free Grammars
43
• To Simplify a Context-Free Grammars,
Removing All
Step 1: Remove Nullable Variables
Step 2: Remove Unit-Productions: any
production of the context-free grammar
of the form AB, where A,B are
elements of V is called a unit
production.
Step 3: Remove Useless Variables:
either it cannot be reached from the
start symbol or it can’t derive a terminal
string. 44
A Substitution Rule
Equivalent
grammar
S aB
S aB | ab
A aaA
Substitute A aaA
A abBc B b A abBc | abbc
B aA
B aA
B b
45
A Substitution Rule
S aB | ab
A aaA
A abBc | abbc
B aA
Substitute
B aA
S aB | ab | aaA
A aaA Equivalent
A abBc | abbc | abaAc grammar
46
In general:
A xBz
B y1
Substitute
B y1
equivalent
A xBz | xy1z grammar
47
Nullable Variables
production : A
Nullable Variable: A
48
Removing Nullable Variables
Example Grammar:
S aMb
M aMb
M
Nullable variable
49
Final Grammar
S aMb
S aMb
Substitute S ab
M aMb M
M aMb
M
M ab
50
Unit-Productions
Unit Production: A B
(a single variable in both sides)
Is removed immediately
51
Example Grammar:
S aA
A a
A B
B A
B bb
52
S aA
S aA | aB
A a
Substitute A a
A B A B B A| B
B A
B bb
B bb
53
S aA | aB S aA | aB
A a Remove A a
B A| B B B B A
B bb B bb
54
S aA | aB
S aA | aB | aA
A a Substitute
B A A a
B A
B bb
B bb
55
Remove repeated productions
Final grammar
S aA | aB | aA S aA | aB
A a A a
B bb B bb
56
Useless Productions
S aSb
S
S A
A aA Useless Production
S A aA aaA aa aA
57
Another grammar:
S A
A aA
A
B bA Useless Production
Not reachable from S
58
In general: contains only
terminals
if S xAy w
w L(G )
59
A production A x is useless
if any of its variables is useless
S aSb
S Productions
Variables S A useless
useless A aA useless
useless B C useless
useless C D useless
60
Removing Useless Productions
Example Grammar:
S aS | A | C
A a
B aa
C aCb
61
First: find all variables that can produce
strings with only terminals
S aS | A | C Round 1: { A, B}
A a S A
B aa
C aCb Round 2: { A, B, S }
62
Keep only the variables
that produce terminal symbols: { A, B, S }
(the rest variables are useless)
S aS | A | C
A a S aS | A
B aa A a
C aCb B aa
Remove useless productions
63
Second: Find all variables
reachable from S
S aS | A
A a S A B
B aa not
reachable
64
Keep only the variables
reachable from S
(the rest variables are useless)
Final Grammar
S aS | A
S aS | A
A a
A a
B aa
65
Normal Forms
for
Context-free Grammars
66
Chomsky Normal Form(CNF)
Each productions has form:
A BC or A a
S AS S AS
S a S AAS
A SA A SA
A b A aa
Chomsky Not Chomsky
Normal Form Normal Form
68
Convertion to Chomsky Normal Form
Example: S ABa
A aab
B Ac
Not Chomsky
Normal Form
69
Introduce variables for terminals: Ta , Tb , Tc
S ABTa
S ABa A TaTaTb
A aab B ATc
B Ac Ta a
Tb b
Tc c
70
Introduce intermediate variable: V1
S AV1
S ABTa
V1 BTa
A TaTaTb
A TaTaTb
B ATc
B ATc
Ta a
Ta a
Tb b
Tb b
Tc c
Tc c
71
Introduce intermediate variable: V2
S AV1
S AV1
V1 BTa
V1 BTa
A TaV2
A TaTaTb
V2 TaTb
B ATc
B ATc
Ta a
Ta a
Tb b
Tb b
Tc c
Tc c 72
Final grammar in Chomsky Normal Form:
S AV1
V1 BTa
A TaV2
Initial grammar
V2 TaTb
S ABa B ATc
A aab Ta a
B Ac Tb b
Tc c 73
In general:
we can obtain:
An equivalent grammar
in Chomsky Normal Form
74
The Procedure
First remove: Nullable variables, Unit productions
Add production Ta a
New variable: Ta
75
Replace any production A C1C2 Cn
with A C1V1
V1 C2V2
Vn 2 Cn 1Cn
New intermediate variables:V1, V2 , ,Vn 2
Theorem: For any context-free grammar
(which doesn’t produce )
there is an equivalent grammar
in Chomsky Normal Form 76
Greibach Normal Form (GNF)
In Chomsky’s Normal Form (CNF), restrictions are
put on the length of right sides of a production,
whereas in Greibach Normal Form (GNF), restriction
are put on the positions in which terminals and
variables can appear.
symbol variables
77
Examples:
S cAB
S abSb
A aA | bB | b
S aa
B b
78
Conversion to Greinbach Normal Form:
S aTb STb
S abSb S aTa
S aa Ta a
Tb b
Greinbach
Normal Form
79
Theorem:For any context-free grammar
(which doesn’t produce )
there is an equivalent grammar
in Greinbach Normal Form
Observations
• Greinbach normal forms are very good
for parsing
• It is hard to find the Greinbach normal
form of any context-free grammar
80
Closure Properties of Context-free
Language
• A set is closed (under an operation) if and only if the
operation on two elements of the set produces
another element of the set.
• If an element outside the set is produced, then the operation
is not closed.
• Closure is a property which describes the application of the
property on any two elements of the set; the result is also
included in the set.
81
Context-free Language Closed Under
Union
Concatenation
Star Closure
Intersection
Not Closed Under Complementation
Every Regular Language is a Context-free
Language
82
Pumping Lemma for CFL
The pumping lemma for CFL is used to prove that
certain
sets are not context free.
Every CFL fulfills some general properties.
But if a set or language fulfills all the properties
of the pumping lemma for CFL, it cannot be said that
the language is context free.
But the reverse is true, i.e., if a language breaks the
properties it can be said that the language is not context
free.
83
Pumping Lemma for CFL: Let L be a
CFL. Then, we can find a natural number n
such that
1. Every z ∈ L with z ≥ n can be written as
w = uvwxy, for some strings u,v,w,x,y.
2. | vx | ≥ 1
3. | vwx | ≤ n
4. uvkwxky ∈ L for all k ≥ 0
84