Top Banner
1 Grammars
129

Grammar

Oct 30, 2014

Download

Education

lavishka_anuj

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 1. Grammars 1

2. GrammarsGrammars express languagesExample: the English languagesentencenoun _ phrase predicatenoun _ phrase article noun predicateverb 2 3. articleaarticlethenounboynoundogverbrunsverbwalks 3 4. A derivation of the boy walks:sentencenoun _ phrase predicatenoun _ phrase verbarticlenoun verb the noun verb the boy verb the boy walks 4 5. A derivation of a dog runs:sentencenoun _ phrasepredicatenoun _ phraseverbarticlenounverb a noun verb a dog verb a dog runs5 6. Language of the grammar: L = { a boy runs, a boy walks, the boy runs, the boy walks, a dog runs, a dog walks, the dog runs,6 7. Notation nounboy noundog Variable Terminal Productionor ruleNon-terminal7 8. Another Example Grammar: S aSbS Derivation of sentenceab :S aSb abS aSb S 8 9. S aSbGrammar:SDerivation of sentence aabb :S aSb aaSbb aabbS aSb S9 10. SaSb aaSbb aaaSbbbaaabbbOther derivations:SaSb aaSbb aaaSbbb aaaaSbbbb aaaabbbb 10 11. Language of the grammar: L = { a boy runs, a boy walks, the boy runs, the boy walks, a dog runs, a dog walks, the dog runs, the dog walks } 11 12. Language of the grammar S aSb S n n L {a b : n 0}12 13. More NotationGV , T , P, SGrammarV : A finiteSet of variablesT : A finiteSet of terminal symbols S : Start variableP: Set of Production rules13V and T are assumed to be disjoint 14. ExampleGSaSb Grammar : SG V , T , P, SV {S } T {a, b} P {S aSb, S14 } 15. More NotationSentential Form:A sentence that containsvariables and terminalsExample:SaSb aaSbbaaaSbbbaaabbb Sentential Forms sentence 15 16. * S aaabbbWe write:Instead of:SaSbaaSbb aaaSbbb aaabbb16 17. *w1 wnIn general we write: w1 w2 w3wnIf: 17 18. *w wBy default:18 19. ExampleGrammar DerivationsS aSb*SS*S ab*S aabb*S aaabbb 19 20. ExampleGrammar DerivationsS aSb S aaSbbS aaSbbaaaaaSbbbbb20 21. Another Grammar ExampleG SAb Grammar: A aAb ADerivations:SAbaAbb abbS Ab aAbb aaAbbb aabbb21 22. ExampleGrammar DerivationsS aSb S aaSbbS aaSbbaaaaaSbbbbb22 23. More DerivationsS Ab aAbb aaAbbbaaaAbbbbaaaaAbbbbbaaaabbbbbS aaaabbbbbS aaaaaabbbbbbb n nS a b b 23 24. Language of a Grammar For a grammar G with start variable S :L(G ) {w : S w} String of terminals 24 25. ExampleGS Ab For grammar : A aAbA n nL(G ) {a b b : n 0} n n Since:Sa b b 25 26. A Convenient Notation A aAb AaAb | A articleaarticlea | the articlethe26 27. Linear Grammars27 28. Linear Grammars Grammars with at most one variable at the right side of a production SaSb S Ab Examples: SA aAb A28 29. A Non-Linear GrammarGrammar G : S SS SWhy S SS SaSbWhat will happen if SbSaS S insteadL(G) {w : na ( w) nb ( w)}Number of a in string w29 30. Another Linear Grammar G SA Grammar : AaB | BAb n nL(G ) {a b : n 0}30 31. Right-Linear Grammars All productions have form: AxBAxstring of Example: terminalsSabSSa 31 32. Left-Linear GrammarsABx All productions have form: orAxstring ofSAabterminals Example:AAab | BBa 32 33. Regular Grammars 33 34. Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples: G1G2 S abS SAab S a AAab | B Ba 34 35. Observation Regular grammars generate regular languages G2 G1 Examples: S Aab SabS AAab | B Sa Ba L(G1) (ab) * aL(G2 ) aab(ab) * 35 36. Regular GrammarsGenerateRegular Languages36 37. TheoremLanguages RegularGenerated by LanguagesRegular Grammars37 38. Theorem - Part 1Languages RegularGenerated by LanguagesRegular GrammarsAny regular grammar generatesa regular language38 39. Theorem - Part 2Languages RegularGenerated by LanguagesRegular Grammars Any regular language is generated by a regular grammar 39 40. Proof Part 1 Languages Regular Generated by Languages Regular GrammarsThe language L (G ) generated byany regular grammar G is regular 40 41. The case of Right-LinearGrammarsLet G be a right-linear grammarWe will prove: L(G ) is regular L( M ) L(G )Proof idea:We will construct NFA M41 with L(M)=L(G) 42. GrammarG is right-linearExample: S aA | B A aa B B b B|a 42 43. Construct NFA M such thatevery state is a grammar variable: A special SVF final stateBS aA | BA aa BB b B|a 43 44. Add edges for each production: aA SVFBS aA 44 45. a ASVF BS aA | B45 46. A a aSa VF BS aA | BA aa B46 47. A a a S a VF BS aA | B bA aa BB bB47 48. A a aSa VF a BS aA | B bA aa BB bB | a48 49. A a aSaVFa BbS aA aaaBaaabB aaaba49 50. NFA MGrammarAGaSaA | Ba Aaa BS aBbB | aVFaB L( M ) L(G )b aaab * a b * a 50 51. In General A right-linear grammarG has variables: V0 ,V1,V2 , and productions:Vi a1a2 amV jorVia1a2 am51 52. We construct the NFAM such that:each variableVicorresponds to anode: V1V3V0VFV2special V4final state52 53. Vi a1a2 amV jFor each production:we add transitions and intermediatenodesVia1 a2 am Vj53 54. For each production: Vi a1a2 amwe add transitions and intermediatenodesVia1 a2 amVF 54 55. Resulting NFAM looks like this:a9a2 a4 a1V1 V3a3a5V0 a3 a4 VF a8a9V2 a5V4It holds that:L(G )L( M )55 56. The case of Left-LinearGrammars Let G be a left-linear grammar We will prove: L (G ) is regular Proof idea:We will construct a right-lineargrammar G withRL(G ) L(G ) 56 57. Since G is left-linear grammarthe productions look like: ABa1a2 ak Aa1a2 ak 57 58. Left G A Ba1a2 aklinear Construct right-linear grammar G A BvRight G Aak a2a1Blinear R Av B 58 59. Construct right-linear grammar GLeft GAa1a2 aklinearAvRight G Aak a2a1linearR Av59 60. R L(G ) L(G ) It is easy to see that:G Since is right-linear, we have: R L(G )L(G )L(G )Regular RegularRegularLanguageLanguage Language60 61. Proof - Part 2 Languages Regular Generated by Languages Regular GrammarsAny regular language L is generatedby some regular grammar G 61 62. Any regular language L is generatedby some regular grammar GProof idea:Let M be the NFA with L L(M ).Construct from M a regular grammar Gsuch that L ( M ) L (G ) 62 63. Since L is regularthere is an NFA M such that L L(M )bExample:M aaq0q1q2bL ab * ab(b * ab) * q3L L(M )63 64. Convert M to a right-linear grammarbM a aq0 q1q2 bq0 aq1 q364 65. b M aa q0 q1q2q0 aq1bq1 bq1 q3q1 aq2 65 66. b M aa q0 q1q2q0 aq1q1 bq1bq1 aq2 q3q2 bq3 66 67. L(G ) L( M ) L Gbq0 aq1 M aa q0 q1q2q1 bq1q1 aq2bq2 bq3 q3q3 q1q3 67 68. In GeneralaFor any transition:qp Add production: qapvariable terminal variable68 69. For any final state:qfAdd production:qf 69 70. Since G is right-linear grammarG is also a regular grammarwith L(G ) L( M ) L70 71. For any regular language one canconstruct left linear as well as rightlinear grammar. 71 72. A a a a VFSaF Ba BB Bbhere F isstart symbolS aA | BB Aaa bBSA aa BA SaB bB | aS this is new rule 72as S is now a final state 73. A aB D CbB bB DB CB aB aC B B b Now D is theCb D start variableD B B Aa AD73 74. Language generated by bothaaab* a +b*a 74 75. 75 76. 76 77. 77 78. Context-Free Languages 78 79. Context-Free Languagesn nR{a b } {ww }Regular Languages79 80. Context-Free LanguagesContext-FreePushdownGrammarsAutomatastack automaton 80 81. Context-Free Grammars81 82. ExampleA context-free grammar G: SaSbSA derivation:S aSb aaSbb aabb82 83. S aSbSL(G ) n n{a b : n 0}(((( ))))83 84. A context-free grammar G: S aSaS bSbSAnother derivation:SaSa abSbaabaSabaabaaba84 85. S aSaS bSbSL(G ) R{ww : w {a, b}*} 85 86. ExampleA context-free grammar G: S aSbS SSSA derivation:S SS aSbSabSab86 87. A context-free grammar G:SaSb SSS SA derivation:SSS aSbS abSabaSbabab87 88. S aSbS SSSL(G ){w : na ( w) nb ( w),and na (v) nb (v)in any prefix v}() ((( ))) (( )) 88 89. Definition: Context-FreeGrammarsGrammar G (V , T , P, S )Variables Terminal StartsymbolsvariableProductions of the form: A x x is string of variables and terminals89 90. Definition: Context-FreeLanguages A language L is context-free if and only ifthere is a C.F.grammarGwith LL(G )90 91. Derivation Order1. S AB2. A aaA4. B Bb3. A 5. BLeftmost derivation:12 345S AB aaABaaBaaBbaabRightmost derivation:14 523S AB ABb Ab aaAb aab 91 92. S aABA bBbB A|Leftmost derivation:SaAB abBbB abAbB abbBbbB abbbbB abbbbRightmost derivation: SaAB aA abBb abAbabbBbb abbbb 92 93. Derivation Trees 93 94. Def: G =(V,T,P,S)An ordered tree is a derivation tree for G iff1 The root is labeled S2 every leaf has a label from T { }3 Every interior vertex has a label from V4 If a vertex has label A(variable) and its childrenare labeled from (L to R)a1,a2,an then P must contain a productionA a1 a2 an5 A leaf labeledhas no sibling.94 95. Yield:The string of terminals obtained byreading the leaves of the tree from leftto right omitting any s encounteredis called yield of the tree. 95 96. Partial derivation treeA tree that has properties 3,4,5 but 1need notAnd 2 is replaced byVT { } 96 97. S AB A aaA |BBb | S AB S AB97 98. S AB A aaA | BBb | S ABaaAB S A Baa A 98 99. S ABA aaA | BBb |S ABaaABaaABbSA Ba a A B b99 100. S ABA aaA |BBb |S AB aaAB aaABbaaBbS A BaaAB b100 101. S AB A aaA |B Bb |S AB aaABaaABb aaBb aabDerivation TreeS A B a a A B b101 102. S AB A aaA |B Bb |S AB aaABaaABb aaBbaabDerivation TreeS A Byield a a A B baa b aab102 103. Partial Derivation TreesSAB A aaA |B Bb |S ABPartial derivation tree SAB 103 104. S ABaaABPartial derivation treeSAB aaA 104 105. sententialS ABaaAB formPartial derivation treeSA Byield aaAaaAB105 106. Sometimes, derivation order doesnt matterLeftmost:SABaaABaaB aaBb aabRightmost:SABABb AbaaAb aab SSame derivation treeA B aaAB b106 107. Ambiguity107 108. E E E | E E | (E) | aa a a EE E E a E a E Ea a E a a*aE Eleftmost derivationaE Eaa108 109. E E E | E E | (E) | aa a aEE E E E E a E E Ea a E a a aE Eleftmost derivationE E aa 109 a 110. E E E | E E | (E) | a a a aTwo derivation treesEEEE E Ea EE E E aa aa 110 a 111. The grammarEE E | E E | (E) | ais ambiguous: string a a a has two derivation treesEEE EE Ea E EE E aa aa a 111 112. The grammarE E E | E E | (E) | ais ambiguous:string a a a has two leftmost derivationsEE E a Ea E E a a Ea a*aEE EE E E a E E a a E a a a 112 113. Definition:A context-free grammarG is ambiguousif some string w L(G ) has:two or more derivation trees 113 114. In other words:A context-free grammar G is ambiguousif some string w L(G ) has: two or more leftmost derivations (or rightmost)114 115. Why do we care about ambiguity? a a a take a 2E EE E E Ea E E E E aa a a 115 a 116. 2 2 2E EE E E E2 E E E E 22 2 2 116 2 117. 2 2 2 62 2 2 8 6 8EE 2 4 42EE EE2 2 2 22 EE E E222 2 117 2 118. Correct result: 2 2 2 66 E 24E E 2 2 2 EE 22118 119. Ambiguity is bad for programming languages We want to remove ambiguity 119 120. We fix the ambiguous grammar: EE E | E E | (E) | aNew non-ambiguous grammar: E E T E T T T F T F F (E) 120 F a 121. E E T T T F Ta T a T Fa F F a a F a a a E a a aEE T E TE TT T FT T FT F F F aF (E)Fa a a 121 122. Unique derivation treeE a a aE T TTF FFa aa122 123. The grammar G:E E TE TT T FT FF (E)F ais non-ambiguous: Every string w L (G ) has a unique derivation tree123 124. Inherent Ambiguity Some context free languages have only ambiguous grammarsExample: n n mn m m L {a b c } {a b c }SS1 | S2 S1 S1c | AS2 aS2 | B AaAb |B bBc |124 125. n n nThe string a b chas two derivation treesSSS1 S2 S1 caS2125 126. Compiler Lexicalparser analyzerinputoutput machine program code 126 127. A parser knows the grammarof the programming language127 128. The parser finds the derivationof a particular inputderivation Parser inputE => E + E E -> E + E=> E + E * E10 + 2 * 5|E*E=> 10 + E*E| INT=> 10 + 2 * E=> 10 + 2 * 5 128 129. derivation tree derivation EE => E + EE + E=> E + E * E=> 10 + E*E 10 E * E=> 10 + 2 * E=> 10 + 2 * 52 5 129