1 / 69

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

ไวยกรณ์ไม่พึงบริบท 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

dschultz
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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. 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. 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. 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. 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. 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. 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. 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 if there is a derivation S *G x in G.

  10. 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 { , 01, 0011, 000111, … } จงเขียน ไวยกรณ์ไม่พึงบริบท ของภาษานี้ ?

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

  14. 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.

  15. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.

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

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

  41. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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

More Related