PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Dependencies/boo/lib/antlr-2.7.5/doc/inheritance.html

https://github.com/w4x/boolangstudio
HTML | 171 lines | 163 code | 8 blank | 0 comment | 0 complexity | 08b8800a82eddf14fc93fef03124632a MD5 | raw file
Possible License(s): GPL-2.0
  1. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <title>ANTLR Specification: Grammar Inheritance</title>
  6. </head>
  7. <body bgcolor="#FFFFFF" text="#000000">
  8. <h1><a name="_bb1">Grammar Inheritance</a></h1>
  9. <p>
  10. Object-oriented programming languages such as C++ and Java allow you to define a new object as it differs from an existing object, which provides a number of benefits. &quot;Programming by difference&quot; saves development/testing time and future changes to the <i>base</i> or <i>superclass</i> are automatically propogated to the <i>derived</i> or <i>subclass</i>.
  11. </p>
  12. <h2><a name="_bb2">Introduction and motivation</a></h2>
  13. <p>
  14. Allowing the ANTLR programmer to define a new grammar as it differs from an existing grammar provides significant benefits. Development time goes down because the programmer only has to specify the rules that are different or that need to be added. Further, when the base grammar changes, all derived grammars will automatically reflect the change. Grammar inheritance is also an interesting way to change the behavior of an existing grammar. A rule or set of rules can be respecified with the same structure, but with different actions.
  15. </p>
  16. <p>
  17. The most obvious use of grammar inheritance involves describing multiple dialects of the same language. Previous solutions would require multiple grammar versions or a single grammar that recognized all dialects at once (using semantics to constrain the input to a single dialect). With grammar inheritance, one could write a base grammar for the common components and then have a derived grammar for each dialect. Code sharing would occur at the grammar and output parser class level.
  18. </p>
  19. <p>
  20. Consider a simple subset of English:
  21. </p>
  22. <pre><tt>class PrimarySchoolEnglish;
  23. sentence
  24. : subject predicate
  25. ;
  26. subject
  27. : NOUN
  28. ;
  29. predicate
  30. : VERB
  31. ;</tt></pre>
  32. <p>
  33. This grammar recognizes sentences like: <tt>Dilbert speaks</tt>.
  34. </p>
  35. <p>
  36. To extend this grammar to include sentences manageable by most American college students, we might add direct objects to the definition of a sentence. Rather than copying and modifying the <tt>PrimarySchoolEnglish</tt> grammar, we can simply extend it:
  37. </p>
  38. <pre><tt>
  39. class AmericanCollegeEnglish extends
  40. PrimarySchoolEnglish;
  41. sentence
  42. : subject predicate object
  43. ;
  44. object
  45. : PREPOSITION ARTICLE NOUN
  46. ;</tt></pre>
  47. <p>
  48. This grammar describes sentences such as <tt>Dilbert speaks to a dog</tt>. While this looks trivial to implement (just add the appropriate <tt>extends</tt> clause in Java to the output parser class), it involves grammar analysis to preserve grammatical correctness. For example, to generate correct code, ANTLR needs to pull in the base grammar and modify it according to the overridden rules. To see this, consider the following grammar for a simple language:
  49. </p>
  50. <pre><tt>class Simple;
  51. stat: expr ASSIGN expr
  52. | SEMICOLON
  53. ;
  54. expr: ID
  55. ;</tt></pre>
  56. <p>
  57. Clearly, the <tt>ID</tt> token is the lookahead set that predicts the recognition of the first alternative of <tt>stat</tt>. Now, examine a derived dialect of <tt>Simple</tt>:
  58. </p>
  59. <pre><tt>class Derived extends Simple;
  60. expr: ID
  61. | INT
  62. ;</tt> </pre>
  63. <p>
  64. In this case, { <tt>ID</tt>, <tt>INT</tt> } predicts the first alternative of <tt>stat</tt>. Unfortunately, a derived grammar affects the recognition of rules inherited from the base grammar! ANTLR must not only override <tt>expr</tt> in <tt>Derived</tt>, but it must override <tt>stat</tt>.
  65. </p>
  66. <p>
  67. Determinining which rules in the base grammar are affected is not easy, so our implementation&nbsp; simply makes a copy of the base grammar and generates a whole new parser with the appropriate modifications. From the programmer's perspective, code/grammar sharing would have occurred, however, from an implementation perspective a copy of the base grammar would be made.
  68. </p>
  69. <h2><a name="_bb3">Functionality</a></h2>
  70. <p>
  71. Grammar <font size="2" face="Courier New">Derived</font> inherits from Grammar <font size="2" face="Courier New">Base</font> all of the rules, options, and actions of <font size="2" face="Courier New">Base</font> including formal/actual rule parameters and rule actions. <font size="2" face="Courier New">Derived</font> may override any option or rule and specify new options, rules, and member action. The subgrammar does not inherit actions outside of classes or file options. Consider rule <font size="2" face="Courier New">Base</font> defined as:
  72. </p>
  73. <pre>class Base extends Parser;
  74. options {
  75. k = 2;
  76. }
  77. {
  78. int count = 0;
  79. }
  80. a : A B {<em>an-action</em>}
  81. | A C
  82. ;
  83. c : C
  84. ;</pre>
  85. <p>
  86. A new grammar may be derived as follows:
  87. </p>
  88. <pre>class Derived extends Base;
  89. options {
  90. k = 3; // need more lookahead; override
  91. buildAST=true;// add an option
  92. }
  93. {
  94. int size = 0; // override; no 'count' def here
  95. }
  96. a : A B {<em>an-action</em>}
  97. | A C {<em>an-extra-action</em>}
  98. | Z // add an alt to rule a
  99. ;
  100. b : a
  101. | A B D // requires LL(3)
  102. ;</pre>
  103. <p>
  104. ANTLR will actually interpret the subgrammar as if you had typed:
  105. </p>
  106. <pre>class Derived extends Parser;
  107. options {
  108. k=3;
  109. buildAST=true;
  110. }
  111. {
  112. int size = 0; // override Base action
  113. }
  114. a : A B {an-action}
  115. | A C {an-extra-action}
  116. | Z // add an alt to rule a
  117. ;
  118. b : a
  119. | A B D // requires LL(3)
  120. ;
  121. // inherited from grammar Base
  122. c : C
  123. ;</pre>
  124. <p>
  125. Rules may be overridden to change their signatures such as their parameters or return types:
  126. </p>
  127. <pre>class Base extends Parser;
  128. a[int x] returns [int y]
  129. : A
  130. ;
  131. class Derived extends Base;
  132. a[float z]
  133. : A
  134. ;</pre>
  135. <p>
  136. ANTLR will generate a warning, however:
  137. </p>
  138. <pre><small>warning: rule Derived.a has different signature than Base.a</small></pre>
  139. <p>
  140. Because of this ability, the subgrammars do not actually inherit, in the Java-sense, from the supergrammar.&nbsp; Different signatures on the generated methods would prevent the parser from compiling.
  141. </p>
  142. <h2><a name="_bb4">Where Are Those Supergrammars?</a></h2>
  143. <p>
  144. The set of potential &quot;supergrammars&quot; available to some grammar P includes any other grammar in the same file as P and any listed on the ANTLR command line with
  145. </p>
  146. <pre>-glib f1.g;f2.g</pre>
  147. <p>
  148. where the files must include path names if they are located in another directory.
  149. </p>
  150. <p>
  151. How is supergrammar P found? The grammars defined in the supergrammar list are read in and an inheritance hierarchy is constructed; any repeated grammar definition in this is ignored.&nbsp; The grammars in the normally specified grammar file are also included in the hierarchy.&nbsp; Incomplete hierarchies results in an error message from ANTLR. &nbsp; Grammars in the same file as P are given precendence to those obtained from other files.
  152. </p>
  153. <p>
  154. The type of grammar (Lexer,Parser,TreeParser) is determined by the type of the highest grammar in the inheritance chain.
  155. </p>
  156. <h2><a name="_bb7">Error Messages</a></h2>
  157. <p>
  158. ANTLR generates a file called <tt>expandedT.g</tt>, given a grammar input file (not the -glib files) called <tt>T.g</tt>.&nbsp; All error messages are relative to this as you really want to see the whole grammar when dealing with ambiguities etc...&nbsp; In the future, we may have a better solution.
  159. </p>
  160. <p>
  161. <font face="Arial" size="2">Version: $Id: //depot/code/org.antlr/release/antlr-2.7.5/doc/inheritance.html#1 $</font>
  162. </body>
  163. </html>