Simplifiaction of grammar

1,598 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,598
On SlideShare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
84
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Simplifiaction of grammar

  1. 1. Compiler Lexical parser analyzerinput output machine program code
  2. 2. A parser knows the grammarof the programming language
  3. 3. The parser finds the derivationof a particular input derivation Parser input E => E + E E -> E + E => E + E * E10 + 2 * 5 |E*E => 10 + E*E | INT => 10 + 2 * E => 10 + 2 * 5
  4. 4. derivation tree derivation EE => E + E E + E => E + E * E => 10 + E*E 10 E * E => 10 + 2 * E => 10 + 2 * 5 2 5
  5. 5. ofContext-Free Grammars
  6. 6. A Substitution Rule Equivalent grammarA→a A→aA → aaA A → aaAA → abBc Substitute B A → ababbAcB → abbA A → abbcB→b
  7. 7. In general: A → xBz B → y1 | y2 |  | yn Substitute B equivalent A → xy1z | xy2 z |  | xyn z grammar
  8. 8. Useless Productions S → aSb S →λ S→A A → aA Useless ProductionSome derivations never terminate...S ⇒ A ⇒ aA ⇒ aaA ⇒  ⇒ aa  aA ⇒ 
  9. 9. Another grammar: S→A A → aA A→λ B → bA Useless Production Not reachable from S
  10. 10. In general: If S ⇒  ⇒ xAy ⇒  ⇒ w w∈ L(G ) Then variable A is usefulOtherwise, variable A is uselessA is useful if it occurs in some sententialform and a string of terminals can bederived from it.
  11. 11. A production A → x is usefulif all its variables are useful
  12. 12. Removing Useless ProductionsExample Grammar: S → aS | A | C A→a B → aa C → aCb
  13. 13. Recognizing useless symbols A variable may be useless because there is no way of getting a terminal string from it.( variable can not derive a string of terminals) Another reason for a variable B to be useless may be that there is no x and y such that S ⇒* x B y ( Variable does not occur in a sentential form)
  14. 14. First: find all variables that can produce strings with only terminalsS → aS | A | C Round 1: { A, B}A→aB → aaC → aCb Round 2: { A, B, S }
  15. 15. Keep only the variables { A, B, S } that produce terminal symbolsS → aS | A | CA→a S → aS | AB → aa A→aC → aCb B → aa
  16. 16.  New grammar G’ = (V’,T’,P’,S) V’ = {S,A,B} G T’={ a} S→ aS | A | C P’: S → aS | A A→a A→a B → aa B → aa C → aCb
  17. 17. algorithmG= (V,T,P,S): given context free grammarG’ = (V’,T’,P’ S) such that V’ contains only variables A for whichA ⇒* w ∈ T* is possibleStep 1: set V’ to emptyStep 2:repeat the following step until no more variables are added to V’.For every A ∈ V for which P has a production of the formA x1 x2…xn with all xi in V’ ∪ TAdd A to V’
  18. 18.  Step 3: Take P’ as all the productions in P whose symbols are all in (V’ ∪ T) If a terminal is not present in any production of P’ remove it from T to get T’.
  19. 19. Example S→AB A→b V’ = Φ B→a V’ = {A,B,E} B→D V’={A,B,E,S} E→a P’: S → AB; B →a; E →a A→ b
  20. 20. Recognize variables that cannot bereached from the start symbol Dependency graph: It has vertices labeled with variables Add an edge between C and D if and only if there is production of the form C→x Dy if a variable is not reachable from starting state Removing it and the affected productions and terminals will not change the grammar
  21. 21. Second: Find all variables reachable from S Dependency GraphS → aS | AA→a S A BB → aa not reachable
  22. 22. Keep only the variables reachable from S Final GrammarS → aS | A S → aS | AA→a A→aB → aa
  23. 23. ExampleV’ = ΦV’ = {A,B,E} AV’={A,B,E,S} E SP’: BS → AB; B →a; E →aA→ b Only A and B are reachable,so E can be removed
  24. 24.  So the new equivalent grammar•S→AB V’={A,B,S}, T’={a,b}•A → b P’:•B → a S → AB; B →a;•B → D A→b•E → a This grammar is now having only useful symbols
  25. 25. Process of removing uselesssymbols 1 Find an equivalent grammar removing symbols which can not produce string of terminals. i.e symbols (A) s.t. A⇒* w 2 From the grammar so obtained get a new grammar having only those symbols (A)each of which is present in some sentential form. S ⇒* x A y
  26. 26. Nullable Variablessuch productions areundesirableλ − production : A→λNullable Variable: A ⇒⇒ λ
  27. 27. Removing Nullable VariablesExample Grammar: S → aMb M → aMb M →λ Nullable variable
  28. 28. Final Grammar S → aMbS → aMb Substitute S → abM → aMb M →λ M → aMbM →λ M → abBoth the grammars generates the same language{anbn : n >= 1}
  29. 29. Theorem: Let G be any CFG with λ not in L(G).Thenthere exists an equivalent grammar G’ having no λproductions. First find the set Vn of all nullable variables of G as: 1 for all productions A→λ put A in Vn 2 repeat following until no further variables are added to Vn For all productions B → A1A2…AnWhere all Ai are in Vn , put B into Vn as B is also nullable
  30. 30.  Once the set Vn has been found we are ready to construct production set P’ as Look all productions of P which are of the form A→ x1 x2… xm m >= 1 Where each xi is in V ∪T For each such production of P we put into P’ those productions as well as all those generated by replacing nullable variable with λ in all possible combinations. If all xi are nullable then A → λ will not be added in P’
  31. 31. ExampleS→ABA → aAA | λB → bBB | λHere A, B are nullable(found in the first step)S is also nullable (in the second step)So Vn ={ S,A,B}.so new set of rules areS → AB | A| B Note: the grammar obtained is notA → aAA | aA| a equivalent to given one as:B → bBB |bB | b λ Is in given language but not in the language generated by the new grammar reason being condition of the theorem not satisfied
  32. 32. ExampleS → ABaCA → BCB → b| λC→D|λD→ dNullable variables are A,B,C
  33. 33.  S → ABaC is replaced by S → ABaC| BaC|AaC| Aba|aC|Aa|Ba|aA → BC is replaced by A → BC | B | CB → b| λ is replaced by B →bC → D | λ is replaced by C→DD→ d is copied as such D→ d
  34. 34.  so the new equivalent grammar without any nullable symbol is S → ABaC| BaC|AaC|Aba| aC|Aa|Ba|a A → BC | B | C B →b C→D D→ d
  35. 35. Unit-Productions Unit Production: A→ BSuch productions are undesirable
  36. 36. Removing Unit ProductionsObservation: A→ A Is removed immediately
  37. 37. Example Grammar: S → aA A→a A→ B B→A B → bb
  38. 38. S → aA S → aA | aBA→a Substitute A→aA→ B A→ B B → A| BB→A B → bbB → bb
  39. 39. S → aA | aB S → aA | aBA→a Remove A→aB → A| B B→B B→AB → bb B → bb
  40. 40. S → aA | aB S → aA | aB | aAA→a Substitute B→A A→aB→A B → bbB → bb
  41. 41. Remove repeated productions Final grammarS → aA | aB | aA S → aA | aBA→a A→aB → bb B → bb
  42. 42.  First find for each A all variables B such that A ⇒* B this can be done by drawing a dependency graph with an edge (C,D) whenever the grammar has a unit production C →D So A ⇒* B whenever there is a walk from A to B in the graph.
  43. 43. The new grammar G’ is obtained as:• include all non unit productions of P• If A ⇒* B we add to P’ A → y1 | y2| … y nWhere B → y1| y2| …yn is the set of all rules in P’ with B on the left
  44. 44. Example S → Aa | B B → A| bb A → a | bc | B S A BS ⇒* A ; S ⇒* B, B ⇒* A and A ⇒*BIn first step we add •S → Aa B → bb •A → a | bc
  45. 45.  In the second step we add S → a | bc | bb A → bb B → a | bc S → a | bc | bb|Aa So the new grammar A → a | bb |bc B → a | bb | bc
  46. 46.  Step 1: Remove Nullable Variables Step 2: Remove Unit-Productions Step 3: Remove Useless Variables
  47. 47. forContext-free Grammars
  48. 48. Chomsky Normal FormAll productions have form: A → BC and A→a variable variable terminal
  49. 49. Examples: S → AS S → AS S →a S → AAS A → SA A → SA A→b A → aa Chomsky Not Chomsky Normal Form Normal Form
  50. 50. Conversion to ChomskyNormal Form Example: S → ABa A → aab B → Ac Not Chomsky Normal Form
  51. 51. 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
  52. 52. Introduce intermediate variable: V1 S → AV1 S → ABTa V1 → BTa A → TaTaTb A → TaTaTb B → ATc B → ATcTa → a Ta → aTb → b Tb → bTc → c Tc → c
  53. 53. Introduce intermediate variable: V2 S → AV1 S → AV1 V1 → BTaV1 → BTa A → TaV2 A → TaTaTb V2 → TaTb B → ATc B → ATc Ta → a Ta → a Tb → b Tb → b Tc → c Tc → c
  54. 54. Final grammar in Chomsky Normal Form: S → AV1Initial grammar V1 → BTaS → ABa A → TaV2A → aab V2 → TaTbB → Ac B → ATc Ta → a Tb → b Tc → c
  55. 55. In general: From any context-free grammar not in Chomsky Normal Form provided empty string is not a member of the language we can obtain: An equivalent grammar in Chomsky Normal Form
  56. 56. The ProcedureFirst remove: Nullable variables Unit productions
  57. 57. For every symbol a: Add production Ta → a In productions: replace a with Ta New variable: Ta
  58. 58. Replace any production A → C1C2 Cn with A → C1V1 V1 → C2V2  Vn− 2 → Cn −1CnNew intermediate variables: V1, V2 , ,Vn−2
  59. 59. Theorem: For any context-free grammar there is an equivalent grammar in Chomsky Normal Form provide Empty string is not a Member If empty string is in the language L First Find an equivalent chomsky’s normal form for L-{λ} ,Then add a new starting var S’ and add one new prod S’ S |λ
  60. 60. Observations• Chomsky normal forms are good for parsing and proving theorems• It is very easy to find the Chomsky normal form of any context-free grammar
  61. 61. Greibach Normal FormAll productions have form: A → a V1V2 Vk k ≥0 symbol variables
  62. 62. Examples: S → cAB S → abSb A → aA | bB | b S → aa B→b Greibach Not Greibach Normal Form Normal Form
  63. 63. Conversion to Greibach Normal Form: S → aTb STb S → abSb S → aTa S → aa Ta → a Tb → b Greibach Normal Form
  64. 64. Theorem: For any context-free grammar there is an equivalent grammar in Greibach Normal Form if empty string is not a member
  65. 65. Observations• Greibach normal forms are very good for parsing• It is hard to find the Greibach normal form of any context-free grammar
  66. 66. An Application ofChomsky Normal Forms
  67. 67. The CYK Membership AlgorithmInput: • Grammar G in Chomsky Normal Form • String wOutput: find if w∈ L(G )
  68. 68. W = a1 a2 a3…anWij =ai…ajVij={A∈V : A ⇒*ij} wClearly w ∈ L(G) if and only if S ∈V1nVij= ∪{A: A BC with B ∈ Vik and C ∈ Vk+1,j}
  69. 69. The AlgorithmInput example: • Grammar G : S → AB A → BB A→a B → AB B→b • String w : aabbb
  70. 70. aabbb W11 a a b b b aa ab bb bb aab abb bbb W14 aabb abbb
  71. 71. S → ABA → BB a a b b bA→a A A B B BB → AB aa ab bb bbB→b aab abb bbb aabb abbb aabbb
  72. 72. S → ABA → BB a a b b bA→a A A B B BB → AB aa ab bb bbB→b S,B A A aab abb bbb aabb abbb aabbb
  73. 73. S → AB a a b b bA → BB A A B B BA→a aa ab bb bbB → AB S,B A A aab abb bbbB→b S,B A S,B aabb abbb A S,B V15 aabbb S,B
  74. 74.  Since S is a member of V15 The given string is a member of L(G)
  75. 75. S → AB | BCA → BA |a V12={S,A}B → CC | b V23={B}C → AB |a V34={S,C}String baaba V45={S,A}V11= {B}=V44V22= V33=V55={A,C} W12= b a B A,C BA, BC A S
  76. 76.  W34 = a b  A,C B  AB CB  V34 = S,C W13= baa b aa B B ba a A ,S A,C
  77. 77. V13 empty setV24= {B}V35={B}V14=empty setV25= {S,A,C}V15={S,A,C} so string is a member of L(G)W15 ba aba

×