ไวยกรณ์ไม่พึงบริ บท
CONTEXT-FREE
GRAMMARS
CONTEXT-FREE GRAMMARS
วัตถุประสงค์
A context-free grammar (CFG) is a
simple recursive method of specifying
grammar rules by which strings in
a language can be generated.
To a large extent, they are capable
of specifying
the syntax of high-level programming languages
and other formal languages.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
Using recursive definition to describe
any languages (regular or nonregular).
Example, palindrome language.
Alphabet = { a, b}
Rule1:
, a, b PALINDROME
Rule2:
For any S PALINDROME,
then aSa, bSb are in PALINDROME.
Note: S can be considered as a variable,
representing an arbitrary element of PALINDROME.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
Alphabet = { a, b}
Rule1: , a, b PALINDROME
Rule2: For any S PALINDROME,
then aSa, bSb are in PALINDROME.
S can be considered as a variable,
representing an arbitrary element of PALINDROME.
Rule can be considered as a relation,
representing a transformation, denoted by “”.
Rule1: S | a | b
Rule2: S aSa | bSb
Note: The vertical bar “|” means “or”.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
ตัวแปร
The production of the grammar
S
Start variable.
others
represent some auxiliary
class of strings.
(class of recursive definition).
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
ตัวอยา่ ง
Alphabet = {a, b}
L is defined by
the following rules:
S aSa | bSb | V
V aTb | bTa
T aT | bT |
Some words generated by these rules,
S aSa aVa abTaa abaa.
S aSa abSba abVba abaTbba abaabba.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
นิยาม
A context-free grammar (CFG) is a
4-tiple G = (V, , S, P) where
V and are disjoint finite sets,
S is an element of V and
P is a finite set of formulas
of the form A ,
where A V and (V )*.
S: start symbol
V: set of variables or nonterminals
: alphabet or set of terminals.
P: set of grammar rules or productions.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
ตัวอยา่ ง
G = (V, , S, P)
Alphabet = {a, b}
L is defined by the following rules:
S aSa | bSb | V
V aTb | bTa
T aT | bT |
aVa G abTaa
S *G aaaba
denotes abTaa can be obtained
from aVa by one of the productions
in G ( V bTa ).
denotes aaaba is derived from S
in zero or more steps in G.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
คาศัพธ์
Let G = (V, , S, P) be a CFG.
The language generated by G is
L(G) = { x * | S *G x }.
A language L is a
Context-free language (CFL)
if there is a CFG G so that L = L(G).
sentential form of G
if there is a derivation S *G x in G.
A string x (V )* is a
A string x * is a
if there is a
sentence of G
derivation S *G x in G.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
ปัญหา
To demonstrate that a CFG generates
a language, we must show two things:
1. every string in the language can be
derived from the grammar
2. no other string can be.
โจทย์
น่ าคิด
กาหนดให้ ภาษาบรรยายโดย
*
จงเขียน ไวยกรณ์ไม่พึงบริ บท
ของภาษานี้
โจทย์
น่ าคิด
กาหนดให้ ภาษา 0n1n เมื่อ n 0
{ , 01, 0011, 000111, … }
จงเขียน ไวยกรณ์ไม่พึงบริ บท
ของภาษานี้
โจทย์
น่ าคิด
กาหนดให้ ภาษาบรรยายโดย
(0+001)*(10+111)*1
จงเขียน ไวยกรณ์ไม่พึงบริ บท
ของภาษานี้
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
ตัวอยา่ ง
L = { x {0,1}* | n0(x) = n1(x) }.
(ni(x) = number of i’s of x.)
Clearly that is in this language.
Rule1: S
One way to produce other strings in L, add one 0 and one 1.
Rule2: S 0S1
Rule3: S 1S0
Consider strings which do not start and end with the same symbol.
Example, 01110100 or 10010110
These strings can be obtained by the concatenation of two strings in L.
Rule4: S SS.
Example, 01110100 can be obtained from 01 and 110100.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
ตัวอยา่ ง
L = { x {0,1}* | n0(x) = n1(x) }.
(ni(x) = number of i’s of x.)
Conclusion
Let G = (V, , S, P) with
S 0S1 | 1S0 | SS | .
We have that L(G) L.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
พิสูจน์
L = { x {0,1}* | n0(x) = n1(x) }.
(ni(x) = number of i’s of x.)
Now we will show that L L(G)
where G = (V, , S, P) with
S 0S1 | 1S0 | SS | .
By mathematical induction on |x|.
Let d(x) = n0(x) – n1(x).
Basic step: if |x| = 0 and x L then d(x) = 0, x = .
Of course, x L(G) by S .
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
พิสูจน์
L = { x {0,1}* | n0(x) = n1(x) }.
(ni(x) = number of i’s of x.)
Inductive step: for |x|≤ k, d(x) = 0, x L.
Let x L and |x| = k+1.
Case1: x begins with 0, ends with 1, then x = 0y1 and y L(G).
Then S *G y. Since S 0S1, we also have x L(G).
Case2: x begins with 1, ends with 0, similar to the case 1.
Case3: x begins with 1, ends with 1, then x = 1y1 and d(x) = 0.
d(1) = -1 and d(1y) = 1, then u such that u is a prefix of 1y,
d(u) = 0. x = uw, u and w L and L(G). Since S SS, x L(G).
Case4: x begins with 0, ends with 0, similar to the case 3. Q.E.D.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
ตัวอยา่ ง
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)
Let L0 be the language of strings
with more 0’s than 1’s.
Let L1 be the language of strings
with more 1’s than 0’s.
Then L = L0 L1.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
พิสูจน์
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)
Consider L0.
Clearly that 0 L0, and for any x in L0, 0x and x0 are in L0.
This suggests the production S 0 | 0S | S0.
1 should be introduced in L0.
Consider two strings x and y in L0,
xy is at least two more 0’s than 1’s.
Therefore adding a single 1 will still yield an element of L0.
Then G0 = (V, , S, P)
S 0 | 0S | S0 | 1SS | S1S | SS1.
It is clear that L(G0) L0.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
พิสูจน์
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)
Now prove that L0 L(G0).
S 0 | 0S | S0 | 1SS | S1S | SS1.
By mathematical induction on |x| in L0.
Basic step: |x| = 1 and x L0, x = 0.
Since S 0, then x L(G0).
Inductive step: |x|≤ k, d(x) > 0 then x L(G0).
For x L0 and |x| = k+1 and d(x) > 0.
Case x = 0y0. Rewrite x = wi1zi, for 1 i n; n = 1’s.
if d(wn)>0 then x is obtained by starting SS1S.
( d(wn)>0 and d(zn)>0, wn and zn L0).
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
พิสูจน์
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)
Inductive step: |x|≤ k, d(x) > 0 then x L(G0).
For x L0 and |x| = k+1 and d(x) > 0.
Case x = 0y0. Rewrite x = wi1zi, for 1 i n; n = 1’s.
if d(wn)>0 then x is obtained by starting SS1S.
( d(wn)>0 and d(zn)>0, wn and zn L0).
if d(wn)0 then m that d(wm)0 (d(w1)>0)
and also d(wm-1) = 1.
Then d(wm-1)> 0 and d(zm-1)>0.
x is obtained by starting SS1S.
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
พิสูจน์
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)
Context-free grammar of this language
is the following:
SA|B
A 0 | 0A | 1AA | A1A | AA1
B 1 | 1B | 0BB | B0B | BB0
การนิยามภาษา
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGES
ตัวอยา่ ง
ALGEBRAIC EXPRESSION
Context-free grammar of this language
is the following:
S S+S | S-S | SS | SS | (S) | a
where a is a terminal symbol.
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ทฤษฎีบท
For any context-free languages L1 and L2,
the languages L1 L2, L1L2 and L1* are
also context-free languages.
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
แนวคิด
Let = { 0, 1 }.
Consider two context-free grammars,
G1 = (V1, , S1, P1) and G2 = (V2, , S2, P2).
P1 = { S1 XA, X 0, A 0 }
P2 = ( S2 XB, X 1, B 1 }.
Find G = G1 G2.
L(G1) = { 00 } and L(G2) = { 11 }.
S S1 | S 2
S1 XA S2 XB
01 L(G1) L(G2)
X0
X1
But S * 01.
A0
B1
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
แนวคิด
Let = { 0, 1 }.
Consider two context-free grammars,
G1 = (V1, , S1, P1) and G2 = (V2, , S2, P2).
P1 = { S1 XA, X 0, A 0 }
P2 = ( S2 XB, X 1, B 1 }.
Find G = G1 G2.
L(G1) = { 00 } and L(G2) = { 11 }.
S S1 | S 2
S1 XA S2 YB
X0
Y1
A0
B1
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ทฤษฎีบท
For any context-free languages L1 and L2,
the languages L1 L2, L1L2 and L1* are
also context-free languages.
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ทฤษฎีบท
Case: L1L2 V = V1 V2 { S }
P = P1 P2 { S S1 | S2 }
Case: L1L2
V = V1 V2 { S }
P = P1 P2 { S S1S2 }
Case: L1*
V = V1 { S }
P = P1 { S S1S | }
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
Is every regular language
a context-free language ?
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ทฤษฎีบท
By definition of regular language, , { }and { a }
are regular languages, and regular is closed under
union, concatenation and Kleene’s star.
To prove this theorem, we have to show that any
regular can be produced by some context-free grammars.
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ตัวอยา่ ง
Regular language (011+1)*(01)*
From the regular expression, we have
S XY
X represents (011+1)*
Y represents (01)*
X 011X | 1X |
Y 01Y |
Example: 01110101 can be derived from CFG.
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ตัวอยา่ ง
This language can be considered as 0i1i1m1k0k
for m > 0.
S XYZ
X represents 0i1i
Y represents 1m
Z represents 1k0k
X 0X1 |
Y 1Y | 1
Z 1Z0 |
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ความเข้าใจ
Understanding the sentence begins
with understanding its grammatical
structure ( i.e., knowing how it is
derived from the grammar rules ).
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ต้นไม้
DERIVATION TREE
How the sentence
S
X
011
0111011011010101
Y
X
1
Given a CFG
011
S XY
X 011X | 1X |
Y 01Y |
X
can be generated?
01
X
011
Y
01
Y
01
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ต้นไม้
DERIVATION TREE
ความกากวม
AMBIGUOUS
A context-free grammar G is ambiguous
if there is at least one string in L(G) having
two or more distinct derivation trees.
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ต้นไม้
ความกากวม
AMBIGUOUS
Given a context-free grammar G with
= { a, + } and
SS+S|a
The sentence a+a+a is in L(G) which has two derivations.
SS+Sa+Sa+S+Sa+a+Sa+a+a
SS+SS+S+Sa+S+Sa+a+Sa+a+a
This language is ambiguous.
โจทย์
น่ าคิด
Let G be a context-free and x nG y be
a derivation in G where x can be written
x = y1A1y2A2…ykAkyk+1
with yi *.
Show that
there are strings pi ( V)* that satisfy
1. Ai ti pi.
2. y = y1p1y2p2…ykpkyk+1
3. Sum of all ti (1ik) = n.
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ตัวอยา่ ง
Given a context-free grammar G with
S if E S | if E S else S | S
This language is ambiguous.
if exp1 if exp2 statement1 else statement2
exp1
exp2
S
S
if E S else S
if E S
if E S
statement2
statement1
exp1
exp2
if E S else S
statement2
statement1
ภาษาไม่พึงบริ บท
CONTEXT-FREE GRAMMARS
CONTEXT-FREE LANGUAGES
ตัวอยา่ ง
Given a context-free grammar G with
S S + S| S S | S | a
This language is ambiguous.
Theorem
The context-free grammar G1 with productions
SS+T|T
TTF|F
F (S) | a
is unambiguous.
NORMAL
FORMS
NORMAL FORMS
วัตถุประสงค์
Ambiguity is one undesirable property of a
context-free grammar that we might wish to
Eliminate. In this section we discuss some
more straightforward ways of “improving”
a grammar without changing the resulting
language.
NORMAL FORMS
วัตถุประสงค์
We try to eliminate “-productions”, of the form
A , and unit productions,” in which one
variable is simply replaced by another.
REMARK : production G
If there are no -productions, || ||.
If there are no unit productions,
if || = || then this step consists of
replacing a variable by a single terminal.
NORMAL FORMS
วัตถุประสงค์
We try to eliminate “-productions”, of the form
A , and unit productions,” in which one
variable is simply replaced by another.
Let L be the length of the current string.
Let T be the number of terminals of the current string.
For any string x Language, of length k, then L+T = 2k.
That is
the number of steps S G x is at most 2k-1 steps.
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
ตัวอยา่ ง
Given a context-free grammar G with productions
S
ABCBCDA 1. Replacing ABCBCDA by A1BC1BC2DA2
2. Create 32 productions
A
CD
S A1BC1BC2DA2 and the others
B
Cb
with or without A1, C1, C2, D and A2.
C
a|
3. Adding
A
CD | C | D
B
Cb | b
D
bD |
C
D
a
bD | b
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
นิยาม
A nullable variable in a context-free
grammar G = ( V, , S, P ) is defined as
follows:
1. Any variable A for which P contains
the production A is nullable.
2. If A B1B2…Bn and B1, B2, …, Bn are
nullables, then A is also nullable.
3. No other variables in V are nullable.
A is nullable if and only if A *G
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
อัลกอริ ทึม
N0 = { A V | P contains the production A };
j = 0;
Do
j = j + 1;
Nj = Nj-1 { A | P contains A B that B Nj1* }
While Nj Nj-1;
From
the
previous
example,
N0 variables.
= { C, D } and N1 = { A, C, D }.
N is
the
set of all
nullable
j
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
อัลกอริ ทึม
Given a CFG G=(V,,S,P), construct a CFG G1=(V,,S,P1)
without -productions as follows:
Initialize P1 to be P.
Find all nullable variables in V, using FindNull.
For every A in P,
add to P1 every production that can be obtained
form this one by deleting from or more
the occurrences in of nullable variables.
Delete all -production from P1.
Delete any duplicates.
Delete productions of the from A A.
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB
ACD | CD
Cb
a|
bD |
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB
ACD | CD
Cb
a|
bD |
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB
ACD | CD | AC | C
Cb
a|
bD | b
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB
ACD | CD | AC | C
Cb
a|
bD | b
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB | AB
ACD | CD | AC | C | AD | D | A |
Cb | b
a
bD | b
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB | AB
ACD | CD | AC | C | AD | D | A |
Cb | b
a
bD | b
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB | AB | BC | CB | B
ACD | CD | AC | C | AD | D | A
Cb | b
a
bD | b
NORMAL FORMS
-Production
CONTEXT-FREE LANGUAGES
ทฤษฎีบท
Let G = ( V, , S, P ) be a context-free
grammar, and let G1 = ( V, , S, P ) be
a context-free grammar obtained from
G by Algorithm RemoveNull.
Then G1 has no -productions, and
L(G1) = L(G) – { }.
NORMAL FORMS
Unit-Production
CONTEXT-FREE LANGUAGES
นิยาม
We try to eliminate “unit productions”, of the
form A B, and A G* B.
Suppose that the context-free
grammar G has no -productions.
Unit productions
NORMAL FORMS
Unit-Production
CONTEXT-FREE LANGUAGES
นิยาม
An A-derivable variable in a context-free
grammar G = ( V, , S, P ) is defined as
follows:
1. Any variable B for which P contains
the production A B, then B is Aderivable.
2. If C is A-derivable, if C B is in P, and
if B A, then B is A-derivable.
3. No other variables in V are A-derivable.
B is A-derivable if and only if A *G B.
NORMAL FORMS
Algorithm RemoveUnit
REMOVING THE UNIT PRODUCTIONS FROM A CFG.
Given a CFG G=(V,,S,P) without -productions,
construct a CFG G1=(V,,S,P1) having no unit productions
as follows:
Initialize P1 to be P.
For each A in V, find the set of all A-derivable variables.
For every pair (A, B) such that B is A-derivable,
and every non-unit production B ,
add the production A to P1.
Delete all unit productions from P1.
NORMAL FORMS
THEOREM
Let G = ( V, , S, P ) be a context-free
grammar with no -productions,
and let G1 = ( V, , S, P ) be a context-free
grammar obtained from
G by Algorithm RemoveUnit.
Then G1 contains no unit productions, and
L(G1) = L(G).
NORMAL FORMS
EXAMPLE
Given a context-free grammar G without
-productions, and with productions
S
T
F
S+T|T
TF|F
(S) | a
S-derivable = { T, F }
T-derivable = { F }
Productions in P1
S
S + T | T | T F | (S) | a
T
T F | F | (S) | a
F
(S) | a
NORMAL FORMS
CHOMSKY NORMAL FORM
Eliminating specific types of productions,
such as -productions and unit productions,
it may also be useful to impose restrictions
on the form of the remaining productions.
Several types of “Normal forms” have
been introduced; we shall present one of
them, the Chomsky normal form.
NORMAL FORMS
CNF
CHOMSKY NORMAL FORM
Definition
A context-free grammar G = ( V, , S, P )
is in Chomsky normal form if every
production is of one of these two types:
A BC
Aa
where A, B and C are variables, and a is
a terminal symbol.
NORMAL FORMS
CNF
CHOMSKY NORMAL FORM
Construct a CFG in CNF
Given context-free grammar G = ( V, , S, P )
Construct G1 = ( V, , S, P1 ) with no
-productions and with no unit productions.
Construct G2 = ( V2, , S, P2 ) with productions
A B1B2…Bk , for k 2
Aa
Replace A B1B2…Bk by
A B1X1, X1 B2X2, …, Xk-1 Bk-1Bk
NORMAL FORMS
EXAMPLE
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB | AB | BC | CB | B
ACD | CD | AC | C | AD | D | A
Cb | b
a
bD | b
S-derivable = { B }
A-derivable = { C, D }
B-derivable = C-derivable = D-derivable =
NORMAL FORMS
EXAMPLE
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB | AB | BC | CB | Cb | b
ACD | CD | AC | AD | a | bD | b
Cb | b
a
bD | b
S-derivable = { B }
A-derivable = { C, D }
B-derivable = C-derivable = D-derivable =
NORMAL FORMS
EXAMPLE
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB | AB | BC | CB | Cb | b
ACD | CD | AC | AD | a | bD | b
Cb | b
a
bD | b
Xb
Xbc
Xcb
Xcd
b
BC
CB
CD
NORMAL FORMS
EXAMPLE
Given a context-free grammar G with
productions
S
A
B
C
D
ABC | ACB | AB | BC | CB | CXb | b
ACD | CD | AC | AD | a | XbD | b
CXb | b
a
XbD | b
Xb
Xbc
Xcb
Xcd
b
BC
CB
CD
NORMAL FORMS
EXAMPLE
Given a context-free grammar G with
productions
S
A
B
C
D
AXbc | AXcb | AB | BC | CB | CXb | b
AXcd | CD | AC | AD | a | XbD | b
CXb | b
a
XbD | b
Xb
Xbc
Xcb
Xcd
b
BC
CB
CD
NORMAL FORMS
THEOREM
For any G = ( V, , S, P ) be a context-free
grammar,
there is a G1 = ( V1, , S, P1 ) be
a context-free grammar in CNF
so that
L(G1) = L(G) – { }.
© Copyright 2026 Paperzz