งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

ไวยกรณ์ไม่พึงบริบท CONTEXT-FREE GRAMMARS

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ไวยกรณ์ไม่พึงบริบท CONTEXT-FREE GRAMMARS"— ใบสำเนางานนำเสนอ:

1 ไวยกรณ์ไม่พึงบริบท CONTEXT-FREE GRAMMARS

2 the syntax of high-level programming languages
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.

3 การนิยามภาษา Alphabet  = { a, b}
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.

4 การนิยามภาษา Alphabet  = { a, b} Rule1: S   | a | b
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”. , a and b are terminals. S is a nonterminal or variable.

5 การนิยามภาษา ตัวแปร The production of the grammar S Start variable.
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).

6 การนิยามภาษา ตัวอย่าง NONPALINDROME Alphabet  = {a, b}
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. NONPALINDROME

7 การนิยามภาษา นิยาม A context-free grammar (CFG) is a
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.

8 การนิยามภาษา ตัวอย่าง G = (V, , S, P) Alphabet  = {a, b}
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 denotes abTaa can be obtained from aVa by one of the productions in G ( V  bTa ). S *G aaaba denotes aaaba is derived from S in zero or more steps in G.

9 การนิยามภาษา คำศัพธ์ L(G) = { x  * | S *G x }.
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). 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

10 ? การนิยามภาษา ปัญหา To demonstrate that a CFG generates
CONTEXT-FREE GRAMMARS การนิยามภาษา DESCRIBE THE LANGUAGES ปัญหา ? To demonstrate that a CFG generates a language, we must show two things: every string in the language can be derived from the grammar no other string can be.

11 ? โจทย์ น่าคิด กำหนดให้ ภาษาบรรยายโดย จงเขียน ไวยกรณ์ไม่พึงบริบท
โจทย์ น่าคิด กำหนดให้ ภาษาบรรยายโดย * จงเขียน ไวยกรณ์ไม่พึงบริบท ของภาษานี้ ?

12 ? โจทย์ น่าคิด กำหนดให้ ภาษา 0n1n เมื่อ n  0
โจทย์ น่าคิด กำหนดให้ ภาษา 0n1n เมื่อ n  0 { , 01, 0011, , … } จงเขียน ไวยกรณ์ไม่พึงบริบท ของภาษานี้ ?

13 ? โจทย์ น่าคิด กำหนดให้ ภาษาบรรยายโดย จงเขียน ไวยกรณ์ไม่พึงบริบท
โจทย์ น่าคิด กำหนดให้ ภาษาบรรยายโดย (0+001)*(10+111)*1 จงเขียน ไวยกรณ์ไม่พึงบริบท ของภาษานี้ ?

14 (ni(x) = number of i’s of x.)
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, or These strings can be obtained by the concatenation of two strings in L. Rule4: S  SS. Example, can be obtained from 01 and

15 (ni(x) = number of i’s of x.)
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.

16 (ni(x) = number of i’s of x.)
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  .

17 (ni(x) = number of i’s of x.)
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.

18 (ni(x) = number of i’s of x.)
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.

19 (ni(x) = number of i’s of x.)
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.

20 (ni(x) = number of i’s of x.)
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 SS1S. ( d(wn)>0 and d(zn)>0, wn and zn L0).

21 (ni(x) = number of i’s of x.)
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 SS1S. ( 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 SS1S.

22 (ni(x) = number of i’s of x.)
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: S  A | B A  0 | 0A | 1AA | A1A | AA1 B  1 | 1B | 0BB | B0B | BB0

23 การนิยามภาษา ตัวอย่าง ALGEBRAIC EXPRESSION
CONTEXT-FREE GRAMMARS การนิยามภาษา DESCRIBE THE LANGUAGES ตัวอย่าง ALGEBRAIC EXPRESSION Context-free grammar of this language is the following: S  S+S | S-S | SS | SS | (S) | a where a is a terminal symbol.

24 ภาษาไม่พึงบริบท ทฤษฎีบท For any context-free languages L1 and L2,
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.

25 ภาษาไม่พึงบริบท แนวคิด L(G1) = { 00 } and L(G2) = { 11 }. S  S1 | S2
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 | S2 S1  XA S2  XB X  0 X  1 A  0 B  1 01  L(G1)  L(G2) But S * 01.

26 ภาษาไม่พึงบริบท แนวคิด L(G1) = { 00 } and L(G2) = { 11 }. S  S1 | S2
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 | S2 S1  XA S2  YB X  0 Y  1 A  0 B  1

27 ภาษาไม่พึงบริบท ทฤษฎีบท For any context-free languages L1 and L2,
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.

28 ภาษาไม่พึงบริบท ทฤษฎีบท Case: L1L2 V = V1  V2  { S }
CONTEXT-FREE GRAMMARS ภาษาไม่พึงบริบท CONTEXT-FREE LANGUAGES ทฤษฎีบท Case: L1L2 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 |  }

29 ? ภาษาไม่พึงบริบท Is every regular language a context-free language ?
CONTEXT-FREE GRAMMARS ภาษาไม่พึงบริบท CONTEXT-FREE LANGUAGES ? Is every regular language a context-free language ?

30 ภาษาไม่พึงบริบท ทฤษฎีบท
CONTEXT-FREE GRAMMARS ภาษาไม่พึงบริบท CONTEXT-FREE LANGUAGES ทฤษฎีบท Every regular language is a context-free language. 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.

31 ภาษาไม่พึงบริบท ตัวอย่าง Regular language (011+1)*(01)*
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: can be derived from CFG.

32 ภาษาไม่พึงบริบท ตัวอย่าง
CONTEXT-FREE GRAMMARS ภาษาไม่พึงบริบท CONTEXT-FREE LANGUAGES ตัวอย่าง Let L be the language { 0i1j0k | j > i + k }. Find the context-free grammar for L. 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 | 

33 ภาษาไม่พึงบริบท ความเข้าใจ DERIVATION TREE
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 ). DERIVATION TREE

34 ภาษาไม่พึงบริบท ต้นไม้ DERIVATION TREE How the sentence
CONTEXT-FREE GRAMMARS ภาษาไม่พึงบริบท CONTEXT-FREE LANGUAGES ต้นไม้ DERIVATION TREE How the sentence can be generated? S X Y 011 X 01 Y 1 X 01 Y Given a CFG S  XY X  011X | 1X |  Y  01Y |  X 011 01 011

35 ภาษาไม่พึงบริบท ต้นไม้ ความกำกวม AMBIGUOUS DERIVATION TREE
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.

36 ภาษาไม่พึงบริบท ต้นไม้ ความกำกวม AMBIGUOUS
CONTEXT-FREE GRAMMARS ภาษาไม่พึงบริบท CONTEXT-FREE LANGUAGES ต้นไม้ ความกำกวม AMBIGUOUS Given a context-free grammar G with  = { a, + } and S  S + S | a The sentence a+a+a is in L(G) which has two derivations. SS+Sa+Sa+S+Sa+a+Sa+a+a SS+SS+S+Sa+S+Sa+a+Sa+a+a This language is ambiguous.

37 ? โจทย์ น่าคิด Let G be a context-free and x nG y be
โจทย์ น่าคิด 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 (1ik) = n. ?

38 ภาษาไม่พึงบริบท ตัวอย่าง if exp1 if exp2 statement1 else statement2
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 S S if E S if E S else S PARENTHESES CAN BE USED TO REMOVE THE AMBIGUITY exp1 exp1 if E S if E S else S statement2 exp2 statement2 exp2 statement1 statement1

39 ภาษาไม่พึงบริบท ตัวอย่าง Given a context-free grammar G with
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 S  S + T | T T  T  F | F F  (S) | a is unambiguous.

40 NORMAL FORMS

41 วัตถุประสงค์ Ambiguity is one undesirable property of a
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.

42 วัตถุประสงค์ We try to eliminate “-productions”, of the form
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.

43 วัตถุประสงค์ We try to eliminate “-productions”, of the form
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.

44 ตัวอย่าง -Production ELIMINATING THE -PRODUCTION
NORMAL FORMS -Production CONTEXT-FREE LANGUAGES ตัวอย่าง ELIMINATING THE -PRODUCTION Given a context-free grammar G with productions S  ABCBCDA A  CD B  Cb C  a |  D  bD |  1. Replacing ABCBCDA by A1BC1BC2DA2 2. Create 32 productions S  A1BC1BC2DA2 and the others with or without A1, C1, C2, D and A2. 3. Adding A  CD | C | D B  Cb | b C  a D  bD | b

45 นิยาม -Production A is nullable if and only if A *G  NULLABLE
NORMAL FORMS -Production CONTEXT-FREE LANGUAGES นิยาม NULLABLE 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 

46 From the previous example, N0 = { C, D } and N1 = { A, C, D }.
NORMAL FORMS -Production CONTEXT-FREE LANGUAGES อัลกอริทึม Algorithm: FindNull 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 }.

47 อัลกอริทึม -Production Algorithm: RemoveNull CONTEXT-FREE LANGUAGES
NORMAL FORMS NORMAL FORMS -Production CONTEXT-FREE LANGUAGES อัลกอริทึม Algorithm: RemoveNull 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.

48 -Production ตัวอย่าง Given a context-free grammar G with productions
NORMAL FORMS NORMAL FORMS -Production CONTEXT-FREE LANGUAGES ตัวอย่าง Given a context-free grammar G with productions S  ABC | ACB A  ACD | CD B  Cb C  a |  D  bD | 

49 -Production ตัวอย่าง Given a context-free grammar G with productions
NORMAL FORMS NORMAL FORMS -Production CONTEXT-FREE LANGUAGES ตัวอย่าง Given a context-free grammar G with productions S  ABC | ACB A  ACD | CD B  Cb C  a |  D  bD | 

50 -Production ตัวอย่าง Given a context-free grammar G with productions
NORMAL FORMS NORMAL FORMS -Production CONTEXT-FREE LANGUAGES ตัวอย่าง Given a context-free grammar G with productions S  ABC | ACB A  ACD | CD | AC | C B  Cb C  a |  D  bD | b

51 -Production ตัวอย่าง Given a context-free grammar G with productions
NORMAL FORMS NORMAL FORMS -Production CONTEXT-FREE LANGUAGES ตัวอย่าง Given a context-free grammar G with productions S  ABC | ACB A  ACD | CD | AC | C B  Cb C  a |  D  bD | b

52 -Production ตัวอย่าง Given a context-free grammar G with productions
NORMAL FORMS NORMAL FORMS -Production CONTEXT-FREE LANGUAGES ตัวอย่าง Given a context-free grammar G with productions S  ABC | ACB | AB A  ACD | CD | AC | C | AD | D | A |  B  Cb | b C  a D  bD | b

53 -Production ตัวอย่าง Given a context-free grammar G with productions
NORMAL FORMS NORMAL FORMS -Production CONTEXT-FREE LANGUAGES ตัวอย่าง Given a context-free grammar G with productions S  ABC | ACB | AB A  ACD | CD | AC | C | AD | D | A |  B  Cb | b C  a D  bD | b

54 -Production ตัวอย่าง Given a context-free grammar G with productions
NORMAL FORMS NORMAL FORMS -Production CONTEXT-FREE LANGUAGES ตัวอย่าง Given a context-free grammar G with productions S  ABC | ACB | AB | BC | CB | B A  ACD | CD | AC | C | AD | D | A B  Cb | b C  a D  bD | b

55 ทฤษฎีบท -Production Context-free grammar
NORMAL FORMS -Production CONTEXT-FREE LANGUAGES ทฤษฎีบท Context-free grammar 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) – {  }.

56 Suppose that the context-free grammar G has no -productions.
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

57 นิยาม Unit-Production B is A-derivable if and only if A *G B.
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 A- derivable. 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.

58 Algorithm RemoveUnit REMOVING THE UNIT PRODUCTIONS FROM A CFG.
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.

59 THEOREM Let G = ( V, , S, P ) be a context-free
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).

60 EXAMPLE Given a context-free grammar G without
NORMAL FORMS EXAMPLE Given a context-free grammar G without -productions, and with productions S  S + T | T T  T  F | F 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

61 CHOMSKY NORMAL FORM Eliminating specific types of productions,
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.

62 CNF 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 A  a where A, B and C are variables, and a is a terminal symbol.

63 CNF CHOMSKY NORMAL FORM
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 A  a Replace A  B1B2…Bk by A  B1X1, X1  B2X2, …, Xk-1  Bk-1Bk

64 EXAMPLE Given a context-free grammar G with productions
NORMAL FORMS EXAMPLE Given a context-free grammar G with productions S  ABC | ACB | AB | BC | CB | B A  ACD | CD | AC | C | AD | D | A B  Cb | b C  a D  bD | b S-derivable = { B } A-derivable = { C, D } B-derivable = C-derivable = D-derivable = 

65 EXAMPLE Given a context-free grammar G with productions
NORMAL FORMS EXAMPLE Given a context-free grammar G with productions S  ABC | ACB | AB | BC | CB | Cb | b A  ACD | CD | AC | AD | a | bD | b B  Cb | b C  a D  bD | b S-derivable = { B } A-derivable = { C, D } B-derivable = C-derivable = D-derivable = 

66 EXAMPLE Given a context-free grammar G with productions
NORMAL FORMS EXAMPLE Given a context-free grammar G with productions S  ABC | ACB | AB | BC | CB | Cb | b A  ACD | CD | AC | AD | a | bD | b B  Cb | b C  a D  bD | b Xb  b Xbc  BC Xcb  CB Xcd  CD

67 EXAMPLE Given a context-free grammar G with productions
NORMAL FORMS EXAMPLE Given a context-free grammar G with productions S  ABC | ACB | AB | BC | CB | CXb | b A  ACD | CD | AC | AD | a | XbD | b B  CXb | b C  a D  XbD | b Xb  b Xbc  BC Xcb  CB Xcd  CD

68 EXAMPLE Given a context-free grammar G with productions
NORMAL FORMS EXAMPLE Given a context-free grammar G with productions S  AXbc | AXcb | AB | BC | CB | CXb | b A  AXcd | CD | AC | AD | a | XbD | b B  CXb | b C  a D  XbD | b Xb  b Xbc  BC Xcb  CB Xcd  CD

69 THEOREM For any G = ( V, , S, P ) be a context-free grammar,
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) – {  }.


ดาวน์โหลด ppt ไวยกรณ์ไม่พึงบริบท CONTEXT-FREE GRAMMARS

งานนำเสนอที่คล้ายกัน


Ads by Google