ไวยกรณ์ไม่พึงบริบท context-free grammars
DESCRIPTION
ไวยกรณ์ไม่พึงบริบท 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 - PowerPoint PPT PresentationTRANSCRIPT
ว�ตถุ�ประสงค์�ว�ตถุ�ประสงค์�A context-free grammar (CFG) is aA context-free grammar (CFG) is asimple recursive method of specifyingsimple recursive method of specifyinggrammar rules by which strings ingrammar rules by which strings ina language can be generated.a language can be generated.
To a large extent, they are capableTo a large extent, they are capableof specifyingof specifying
the syntax of high-level programming languages
and other formal languages.and other formal languages.
CONTEXT-FREE GRAMMARS
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษาUsing recursive definition to describeUsing recursive definition to describeany languages (regular or nonregular).any languages (regular or nonregular).
Example, palindrome language.
Alphabet = { a, b}Rule1: , a, b PALINDROMERule2: 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
Alphabet = { a, b}Rule1: , a, b PALINDROMERule2: 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 | bRule2: S aSa | bSb
Note: The vertical bar “|” means “or”.
, a and b are terminals.
S is a nonterminal or variable.
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
ต�วแปต�วแปรรThe production of the grammar
S Start variable.
others represent some auxiliaryclass of strings.(class of recursive definition).
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
Alphabet Alphabet = {a, b} = {a, b}L is defined byL is defined bythe following rules:the following rules:
S S aSa | bSb | V aSa | bSb | VV V aTb | bTa aTb | bTaT T aT | bT | aT | bT |
Some words generated by these rules,S aSa aVa abTaa abaa.S aSa abSba abVba abaTbba abaabba.
NONPALINDROME
ต�วอยต�วอยางาง
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
A context-free grammar (CFG) is a4-tiple G = (V, , S, P) where
V and are disjoint finite sets,S is an element of V andP is a finite set of formulasof the form A ,
where A V and (V )*.S: start symbolV: set of variables or nonterminals: alphabet or set of terminals.P: set of grammar rules or productions.
นิ�ยาม่นิ�ยาม่DESCRIBE THE LANGUAGES
การนิ�ยาม่ภาษาการนิ�ยาม่ภาษาCONTEXT-FREE GRAMMARS
G = (V, , S, P)Alphabet = {a, b}L is defined by the following rules:
S aSa | bSb | VV aTb | bTaT aT | bT |
aVa G abTaa denotes abTaa can be obtainedfrom aVa by one of the productionsin G ( V bTa ).
S *G aaaba denotes aaaba is derived from Sin zero or more steps in G.
ต�วอยต�วอยางาง
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
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).
A string x (V )* is a sentential form of G
if there is a derivation S *G x in G.
A string x * is a sentence of G
if there is a derivation S *G x in G.
ค์�าค์�าศั�พึธ์�ศั�พึธ์�
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
?To demonstrate that a CFG generatesTo demonstrate that a CFG generatesa language, we must show two things:a language, we must show two things:
1.1. every string in the language can beevery string in the language can bederived from the grammarderived from the grammar
2.2. no other string can be.no other string can be.
ป!ญหป!ญหาา
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
โจทย์� น่�าคิ�ดน่�าคิ�ด
?ก�าหนิดให& ภาษาบรรยายโดยก�าหนิดให& ภาษาบรรยายโดย
**
จงเขี,ยนิ จงเขี,ยนิ ไวยกรณ์�ไม่พึ�งไวยกรณ์�ไม่พึ�งบร�บทบร�บท
ขีองภาษานิ,-ขีองภาษานิ,-
โจทย์� น่�าคิ�ดน่�าคิ�ด
?ก�าหนิดให& ภาษาก�าหนิดให& ภาษา 0n1n เม่./อ เม่./อ n n 00
{ { , 01, 0011, 000111, … }, 01, 0011, 000111, … }
จงเขี,ยนิ จงเขี,ยนิ ไวยกรณ์�ไม่พึ�งไวยกรณ์�ไม่พึ�งบร�บทบร�บท
ขีองภาษานิ,-ขีองภาษานิ,-
โจทย์� น่�าคิ�ดน่�าคิ�ด
?ก�าหนิดให& ภาษาบรรยายโดยก�าหนิดให& ภาษาบรรยายโดย
(0+001)*(10+111)*1(0+001)*(10+111)*1
จงเขี,ยนิ จงเขี,ยนิ ไวยกรณ์�ไม่พึ�งไวยกรณ์�ไม่พึ�งบร�บทบร�บท
ขีองภาษานิ,-ขีองภาษานิ,-
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 0S1Rule3: S 1S0
Consider strings which do not start and end with the same symbol.Example, 01110100 or 10010110These strings can be obtained by the concatenation of two strings in L.
Rule4: S SS.Example, 01110100 can be obtained from 01 and 110100.
ต�วอยต�วอยางาง
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
ConclusionConclusionLet G = (V, Let G = (V, , S, P) with, S, P) with
S S 0S1 | 1S0 | SS | 0S1 | 1S0 | SS | ..
We have that L(G) We have that L(G) L. L.
L = { x {0,1}* | n0(x) = n1(x) }.
(ni(x) = number of i’s of x.)
ต�วอยต�วอยางาง
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
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 .
L = { x {0,1}* | n0(x) = n1(x) }.
(ni(x) = number of i’s of x.)พึ�ส0จนิ�พึ�ส0จนิ�
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
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.
L = { x {0,1}* | n0(x) = n1(x) }.
(ni(x) = number of i’s of x.)พึ�ส0จนิ�พึ�ส0จนิ�
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)
ต�วอยต�วอยางาง
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
Let L0 be the language of stringswith more 0’s than 1’s.
Let L1 be the language of strings
with more 1’s than 0’s.
Then L = L0 L1.
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.
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)พึ�ส0จนิ�พึ�ส0จนิ�
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
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).
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)พึ�ส0จนิ�พึ�ส0จนิ�
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
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.
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)พึ�ส0จนิ�พึ�ส0จนิ�
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
Context-free grammar of this languageis the following:
S A | BA 0 | 0A | 1AA | A1A | AA1B 1 | 1B | 0BB | B0B | BB0
L = { x {0,1}* | n0(x) n1(x) }.
(ni(x) = number of i’s of x.)พึ�ส0จนิ�พึ�ส0จนิ�
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
ALGEBRAIC EXPRESSIONALGEBRAIC EXPRESSION
Context-free grammar of this languageis the following:
S S+S | S-S | SS | SS | (S) | a
where a is a terminal symbol.
ต�วอยต�วอยางาง
DESCRIBE THE LANGUAGESการนิ�ยาม่ภาษาการนิ�ยาม่ภาษา
CONTEXT-FREE GRAMMARS
For any context-free languages L1 and L2,the languages L1 L2, L1L2 and L1* arealso context-free languages.
ทฤษฎี,ทฤษฎี,บทบท
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
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 | S2
S1 XA S2 XBX 0 X 1A 0 B 1
01 L(G1) L(G2)But S * 01.
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
แนิวค์�แนิวค์�ดด
L(G1) = { 00 } and L(G2) = { 11 }.S S1 | S2
S1 XA S2 YBX 0 Y 1A 0 B 1
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.
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
แนิวค์�แนิวค์�ดด
For any context-free languages L1 and L2,the languages L1 L2, L1L2 and L1* arealso context-free languages.
ทฤษฎี,ทฤษฎี,บทบท
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
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 LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
??Is every regular language Is every regular language a context-free language ?a context-free language ?
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
By definition of regular language, , { }and { a } are regular languages, and regular is closed underunion, concatenation and Kleene’s star.
To prove this theorem, we have to show that anyregular can be produced by some context-free grammars.
ทฤษฎี,ทฤษฎี,บทบท
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
Every regular language is a context-
free language.
From the regular expression, we haveS XY X represents (011+1)*
Y represents (01)*
X 011X | 1X |
Y 01Y |
Example: 01110101 can be derived from CFG.
Regular language (011+1)*(01)*Regular language (011+1)*(01)*ต�วอยต�วอยางาง
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
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 | 1Z 1Z0 |
ต�วอยต�วอยางาง
Let L be the language { 0 i1 j0k | j > i + k }.
Find the context-free grammar for L.
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
Understanding the sentence begins with understanding its grammaticalstructure ( i.e., knowing how it isderived from the grammar rules ).
DERIVATION TREE
ค์วาม่เขี&าใจ
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
How the sentence0111011011010101can be generated?
Given a CFGS XYX 011X | 1X | Y 01Y |
S
X Y
011 X 01 Y
1 X 01 Y
01 011 X
011
ต&นิไม่&CONTEXT-FREE LANGUAGES
ภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บทCONTEXT-FREE GRAMMARS
DERIVATION TREE
ค์วาม่ก�ากวม่
AMBIGUOUSA context-free grammar G is ambiguousif there is at least one string in L(G) havingtwo or more distinct derivation trees.
ต&นิไม่&CONTEXT-FREE LANGUAGES
ภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บทCONTEXT-FREE GRAMMARS
DERIVATION TREE
Given a context-free grammar G with = { a, + } and S S + S | aThe sentence a+a+a is in L(G) which has two derivations.
SS+Sa+Sa+S+Sa+a+Sa+a+aSS+SS+S+Sa+S+Sa+a+Sa+a+a
This language is ambiguous.
ค์วาม่ก�ากวม่
AMBIGUOUS
ต&นิไม่&CONTEXT-FREE LANGUAGES
ภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บทCONTEXT-FREE GRAMMARS
โจทย์� น่�าคิ�ดน่�าคิ�ด
?Let G be a context-free and x n
G y bea derivation in G where x can be written
x = y1A1y2A2…ykAkyk+1
with yi *.Show thatthere are strings pi ( V)* that satisfy
1. Ai ti pi.2. y = y1p1y2p2…ykpkyk+1
3. Sum of all ti (1ik) = n.
Given a context-free grammar G withS if E S | if E S else S | SThis language is ambiguous.
S
if E S
if E S else S
statement2
exp1
statement1exp2
S
if E S
if E S else S
statement2
exp1
statement1exp2
PARENTHESES CAN
BE USED TO REMOVE
THE AMBIGUITY
ต�วอยต�วอยางาง
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
if exp1 if exp2 statement1 else statement2
Given a context-free grammar G withS S + S| S S | S | aThis language is ambiguous.
TheoremThe context-free grammar G1 with productions
S S + T | TT T F | FF (S) | a
is unambiguous.
ต�วอยต�วอยางาง
CONTEXT-FREE LANGUAGESภาษาไม่พึ�งบร�บทภาษาไม่พึ�งบร�บท
CONTEXT-FREE GRAMMARS
Ambiguity is one undesirable property of acontext-free grammar that we might wish toEliminate. In this section we discuss somemore straightforward ways of “improving”a grammar without changing the resultinglanguage.
NORMAL FORMS
ว�ตถุ�ประสงค์�ว�ตถุ�ประสงค์�
We try to eliminate “-productions”, of the formA , and unit productions,” in which onevariable is simply replaced by another.
REMARK : production G
If there are no -productions, || ||.If there are no unit productions,
if || = || then this step consists ofreplacing a variable by a single terminal.
NORMAL FORMS
ว�ตถุ�ประสงค์�ว�ตถุ�ประสงค์�
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 isthe number of steps S G x is at most 2k-1 steps.
NORMAL FORMS
ว�ตถุ�ประสงค์�ว�ตถุ�ประสงค์�We try to eliminate “-productions”, of the formA , and unit productions,” in which onevariable is simply replaced by another.
Given a context-free grammar G with productionsS ABCBCDAA CDB CbC a | D bD |
NORMAL FORMS
ต�วอยต�วอยางาง
CONTEXT-FREE LANGUAGES -Production-Production
ELIMINATING THE -PRODUCTION
1. Replacing ABCBCDA by A1BC1BC2DA2
2. Create 32 productionsS A1BC1BC2DA2 and the otherswith or without A1, C1, C2, D and A2.
3. Adding A CD | C | DB Cb | bC aD bD | b
CONTEXT-FREE LANGUAGES
NORMAL FORMS
นิ�ยาม่นิ�ยาม่ -Production-Production
NULLABLE
A nullable variable in a context-freegrammar G = ( V, , S, P ) is defined
asfollows:
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
N0 = { A V | P contains the production A };j = 0;Do j = j + 1;
Nj = Nj-1 { A | P contains A B that B Nj-1* }While Nj Nj-1;
Nj is the set of all nullable variables.
From the previous example, N0 = { C, D } and N1 = { A, C, D }.
NORMAL FORMS
CONTEXT-FREE LANGUAGES
อ�ลกอร�อ�ลกอร�ท�ม่ท�ม่
-Production-ProductionAlgorithm: FindNull
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 obtainedform this one by deleting from or morethe occurrences in of nullable variables.
Delete all -production from P1.Delete any duplicates.Delete productions of the from A A.
NORMAL FORMSNORMAL FORMS
CONTEXT-FREE LANGUAGES
อ�ลกอร�อ�ลกอร�ท�ม่ท�ม่
-Production-ProductionAlgorithm: RemoveNull
ตั�วอย์�างGiven a context-free grammar G with productions
S ABC | ACBA ACD | CDB CbC a | D bD |
NORMAL FORMSNORMAL FORMS
CONTEXT-FREE LANGUAGES -Production-Production
ตั�วอย์�าง
NORMAL FORMSNORMAL FORMS
CONTEXT-FREE LANGUAGES -Production-Production
Given a context-free grammar G with productions
S ABC | ACBA ACD | CDB CbC a | D bD |
ตั�วอย์�างGiven a context-free grammar G with productions
S ABC | ACBA ACD | CD | AC | CB CbC a | D bD | b
NORMAL FORMSNORMAL FORMS
CONTEXT-FREE LANGUAGES -Production-Production
ตั�วอย์�างGiven a context-free grammar G with productions
S ABC | ACBA ACD | CD | AC | CB CbC a | D bD | b
NORMAL FORMSNORMAL FORMS
CONTEXT-FREE LANGUAGES -Production-Production
ตั�วอย์�างGiven a context-free grammar G withproductions
S ABC | ACB | ABA ACD | CD | AC | C | AD | D | A | B Cb | bC a D bD | b
NORMAL FORMSNORMAL FORMS
CONTEXT-FREE LANGUAGES -Production-Production
ตั�วอย์�างGiven a context-free grammar G withproductions
S ABC | ACB | ABA ACD | CD | AC | C | AD | D | A | B Cb | bC a D bD | b
NORMAL FORMSNORMAL FORMS
CONTEXT-FREE LANGUAGES -Production-Production
ตั�วอย์�างGiven a context-free grammar G withproductions
S ABC | ACB | AB | BC | CB | BA ACD | CD | AC | C | AD | D | AB Cb | bC a D bD | b
NORMAL FORMSNORMAL FORMS
CONTEXT-FREE LANGUAGES -Production-Production
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, andL(G1) = L(G) – { }.
NORMAL FORMS
CONTEXT-FREE LANGUAGES
ทฤษฎี,บทฤษฎี,บทท
-Production-ProductionContext-free grammar
Unit productionsUnit productions
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.
CONTEXT-FREE LANGUAGES
NORMAL FORMS
นิ�ยาม่นิ�ยาม่ UnitUnit-Production-Production
An A-derivable variable in a context-free
grammar G = ( V, , S, P ) is defined as
follows:
1. Any variable B for which P containsthe production A B, then B is A-
derivable.2. If C is A-derivable, if C B is in P,
andif 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.
CONTEXT-FREE LANGUAGES
NORMAL FORMS
นิ�ยาม่นิ�ยาม่ UnitUnit-Production-Production
Algorithm RemoveUnitAlgorithm RemoveUnitREMOVING THE UNIT PRODUCTIONS FROM A CFG.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
THEOREMTHEOREMLet G = ( V, , S, P ) be a context-
freegrammar with no -productions,and let G1 = ( V, , S, P ) be a
context-freegrammar obtained fromG by Algorithm RemoveUnit.
Then G1 contains no unit productions, and
L(G1) = L(G).
NORMAL FORMS
EXAMPLEEXAMPLEGiven a context-free grammar G without-productions, and with productions
S S + T | TT T F | FF (S) | a
S-derivable = { T, F }T-derivable = { F }
Productions in P1
S S + T | T | T F | (S) | aT T F | F | (S) | aF (S) | a
NORMAL FORMS
CHOMSKY NORMAL FORMCHOMSKY NORMAL FORMEliminating specific types of
productions,such as -productions and unit
productions,it may also be useful to impose
restrictionson the form of the remaining
productions.
Several types of “Normal forms” havebeen introduced; we shall present one
ofthem, the Chomsky normal form.
NORMAL FORMS
CNFCNFCHOMSKY NORMAL FORMCHOMSKY NORMAL FORM
Definition
A context-free grammar G = ( V, , S, P )
is in Chomsky normal form if everyproduction is of one of these two
types:
A BCA a
where A, B and C are variables, and a is
a terminal symbol.
NORMAL FORMS
CNFCNFCHOMSKY NORMAL FORMCHOMSKY 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 2A a
Replace A B1B2…Bk byA B1X1, X1 B2X2, …, Xk-1 Bk-1Bk
NORMAL FORMS
EXAMPLEEXAMPLEGiven a context-free grammar G withproductions
S ABC | ACB | AB | BC | CB | BA ACD | CD | AC | C | AD | D | AB Cb | bC a D bD | b
S-derivable = { B }A-derivable = { C, D }B-derivable = C-derivable = D-derivable =
NORMAL FORMS
EXAMPLEEXAMPLEGiven a context-free grammar G withproductions
S ABC | ACB | AB | BC | CB | Cb | bA ACD | CD | AC | AD | a | bD | bB Cb | bC a D bD | b
S-derivable = { B }A-derivable = { C, D }B-derivable = C-derivable = D-derivable =
NORMAL FORMS
EXAMPLEEXAMPLEGiven a context-free grammar G withproductions
S ABC | ACB | AB | BC | CB | Cb | bA ACD | CD | AC | AD | a | bD | bB Cb | bC a D bD | b
Xb bXbc BCXcb CBXcd CD
NORMAL FORMS
EXAMPLEEXAMPLEGiven a context-free grammar G withproductions
S ABC | ACB | AB | BC | CB | CXb | bA ACD | CD | AC | AD | a | XbD | bB CXb | bC a D XbD | b
Xb bXbc BCXcb CBXcd CD
NORMAL FORMS
EXAMPLEEXAMPLEGiven a context-free grammar G withproductions
S AXbc | AXcb | AB | BC | CB | CXb | bA AXcd | CD | AC | AD | a | XbD | bB CXb | bC a D XbD | b
Xb bXbc BCXcb CBXcd CD
NORMAL FORMS