home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V6 / usr / doc / yacc / ss7 < prev    next >
Encoding:
Text File  |  1975-06-26  |  9.0 KB  |  297 lines

  1. .SH
  2. Section 7. Hints for Preparing Specifications
  3. .PP
  4. This section contains miscellaneous hints on preparing efficient, easy to change,
  5. and clear specifications.
  6. The individual subsections are, more or less,
  7. independent; the reader seeing Yacc for the first
  8. time may well find that
  9. this entire section could be omitted.
  10. .SH
  11. Input Style
  12. .PP
  13. It is difficult to
  14. input rules with substantial actions
  15. and still have a readable specification file.
  16. The following style hints owe much to Brian Kernighan,
  17. and are officially endorsed by the author.
  18. .IP a.
  19. Use all capital letters for token names, all lower case letters for
  20. nonterminal names.
  21. This rule comes under the heading of ``knowing who to blame when
  22. things go wrong.``
  23. .IP b.
  24. Put grammar rules and actions on separate lines.
  25. This allows either to be changed without
  26. an automatic need to change the other.
  27. .IP c.
  28. Put all rules with the same left hand side together.
  29. Put the left hand side in only once, and let all
  30. following rules begin with a vertical bar.
  31. .IP d.
  32. Indent rule bodies by one tab stop, and action bodies by two tab stops.
  33. .PP
  34. The example in Appendix A is written following this style, as are
  35. the examples in the text of this paper (where space permits).
  36. The user must make up his own mind about these stylistic questions;
  37. the central problem, however, is to make the rules visible through
  38. the morass of action code.
  39. .SH
  40. Common Actions
  41. .PP
  42. When several grammar rules have the same action, the user might well wish to
  43. provide only one code sequence.
  44. A simple, general mechanism is, of course, to use subroutine calls.
  45. It is also possible to put a label on the first statement of an action,
  46. and let other actions be simply a goto to this
  47. label.
  48. Thus, if the user had a routine which built trees,
  49. he might wish to have only one call to it, as follows:
  50. .DS
  51. expr :
  52.     expr \'+\' expr =
  53.     {  binary:
  54.         $$ = btree( $1, $2, $3 );
  55.     } |
  56.     expr \'\-\' expr =
  57.     {
  58.         goto binary;
  59.     } |
  60.     expr \'*\' expr =
  61.     {
  62.         goto binary;
  63.     } ;
  64. .DE
  65. .SH
  66. Left Recursion
  67. .PP
  68. The algorithm used by the Yacc parser encourages so called ``left recursive''
  69. grammar rules: rules of the form
  70. .DS
  71. name : name rest\_of\_rule ;
  72. .DE
  73. These rules frequently arise when
  74. writing specifications of sequences and lists:
  75. .DS
  76. list :
  77.     item |
  78.     list \',\' item ;
  79. .DE
  80. and
  81. .DS
  82. sequence :
  83.     item |
  84.     sequence item ;
  85. .DE
  86. Notice that, in each of these cases, the first rule
  87. will be reduced for the first item only, and the second rule
  88. will be reduced for the second and all succeeding items.
  89. .PP
  90. If the user were to write these rules right recursively, such as
  91. .DS
  92. sequence :
  93.     item |
  94.     item sequence ;
  95. .DE
  96. the parser would be a bit bigger, and the items would be seen, and reduced,
  97. from right to left.
  98. More seriously, an internal stack in the parser
  99. would be in danger of overflowing if a very long sequence were read.
  100. Thus, the user should use left recursion wherever reasonable.
  101. .PP
  102. The user should also consider whether a sequence with zero
  103. elements has any meaning, and if so, consider writing
  104. the sequence specification with an empty rule:
  105. .DS
  106. sequence :
  107.     | /* empty */
  108.     sequence item ;
  109. .DE
  110. Once again, the first rule would always be reduced exactly once, before the
  111. first item was read,
  112. and then the second rule would be reduced once for each item read.
  113. Experience suggests that permitting empty sequences
  114. leads to increased generality, which frequently is not evident at the
  115. time the rule is first written.
  116. There are cases, however, when the Yacc algorithm can fail when
  117. such a change is made.
  118. In effect, conflicts might arise when Yacc is asked to decide
  119. which empty sequence it has seen, when it hasn\'t seen enough to
  120. know!
  121. Nevertheless,
  122. this principle is still worth following wherever possible.
  123. .SH
  124. Lexical Tie-ins
  125. .PP
  126. Frequently, there are lexical decisions which depend on the
  127. presence of various constructions in the specification.
  128. For example, the lexical analyzer might want to
  129. delete blanks normally, but not within quoted strings.
  130. Or names might be entered into a symbol table in declarations,
  131. but not in expressions.
  132. .PP
  133. One way of handling these situations is
  134. to create a global flag which is
  135. examined by the lexical analyzer, and set by actions.
  136. For example, consider a situation where we have a program which
  137. consists of 0 or more declarations, followed by 0 or more statements.
  138. We declare a flag called ``dflag'', which is 1 during declarations, and 0 during
  139. statements.
  140. We may do this as follows:
  141. .DS
  142. %{
  143.     int dflag ;
  144. %}
  145. %%
  146. program :
  147.     decls  stats ;
  148.  
  149. decls :
  150.     = /* empty */
  151.     {
  152.         dflag = 1;
  153.     } |
  154.     decls declaration ;
  155.  
  156. stats :
  157.     = /* empty */
  158.     {
  159.         dflag = 0;
  160.     } |
  161.     stats statement ;
  162.  
  163.     . . .  other rules . . .
  164. .DE
  165. The flag dflag is now set to zero when reading statements, and 1 when reading declarations,
  166. .ul
  167. except for the first token in the first statement.
  168. This token must be seen by the parser before it can tell that
  169. the declaration section has ended and the statements have
  170. begun.
  171. Frequently, however, this single token exception does not
  172. affect the lexical scan required.
  173. .PP
  174. Clearly, this kind of ``backdoor'' approach can be elaborated on
  175. to a noxious degree.
  176. Nevertheless, it represents a way of doing some things
  177. that are difficult, if not impossible, to
  178. do otherwise.
  179. .SH
  180. Bundling
  181. .PP
  182. Bundling is a technique for collecting together various character strings
  183. so that they can be output at some later time.
  184. It is derived from a feature of the same name in the compiler/compiler TMG [6].
  185. .PP
  186. Bundling has two components \- a nice user interface,
  187. and a clever implementation trick.
  188. They will be discussed in that order.
  189. .PP
  190. The user interface consists of two routines, ``bundle'' and ``bprint''.
  191. .DS
  192. bundle( a1, a2, . . ., an )
  193. .DE
  194. accepts a variable number of arguments which are either character strings or bundles, and
  195. returns a bundle,
  196. whose value will be the concatenation of the values of a1, . . ., an.
  197. .DS
  198. bprint( b )
  199. .DE
  200. accepts a bundle as argument and outputs its value.
  201. .PP
  202. For example, suppose that we wish to read arithmetic expressions, and output
  203. function calls to routines called ``add'', ``sub'',
  204. ``mul'', ``div'', and ``assign''.
  205. Thus, we wish to translate
  206. .DS
  207. a = b \- c*d
  208. .DE
  209. into
  210. .DS
  211. assign(a,sub(b,mul(c,d)))
  212. .DE
  213. .PP
  214. A Yacc specification file which does this is given in Appendix D; this includes
  215. an implementation of the bundle and bprint
  216. routines.
  217. A rule and action of the form
  218. .DS
  219. expr:
  220.     expr \'+\' expr =
  221.     {
  222.         $$ = bundle( "add(", $1, ",", $3, ")" );
  223.     }
  224. .DE
  225. causes the returned value of
  226. expr to be come a bundle, whose value is the
  227. character string containing the desired function call.
  228. Each NAME token has a value which is a pointer to the
  229. actual name which has been read.
  230. Finally, when the entire input line has been read
  231. and the value has been bundled,
  232. the value is written out
  233. and the bundles and names
  234. are cleared, in preparation for the next input line.
  235. .PP
  236. Bundles are implemented as arrays of pointers, terminated by a zero pointer.
  237. Each pointer either points to a bundle or to a character string.
  238. There is an array, called
  239. .ul
  240. bundle space,
  241. which contains all the bundles.
  242. .PP
  243. The implementation trick is to check the values of the pointers in bundles \-
  244. if the pointer points into bundle space, it is assumed to point to a bundle;
  245. otherwise it is assumed to point to a character string.
  246. .PP
  247. The treatment of functions with a variable number of arguments, like bundle,
  248. is likely to differ from one implementation of C to another.
  249. .PP
  250. In general, one may wish to have a simple storage allocator which
  251. allocates and frees bundles,
  252. in order to handle situations where it is not appropriate to completely
  253. clear all of bundle space at one time.
  254. .SH
  255. Reserved Words
  256. .PP
  257. Some programming languages
  258. permit the user to
  259. use words like ``if'', which are normally reserved,
  260. as label or variable names, provided that such use does not
  261. conflict with the legal use of these names in the programming language.
  262. This is extremely hard to do in the framework of Yacc,
  263. since it is difficult to pass the required information to the lexical analyzer
  264. which tells it ``this instance of if is a keyword, and that instance is a variable''.
  265. The user can make a stab at it, using the
  266. mechanism described in the last subsection,
  267. but it is difficult.
  268. .PP
  269. A number of ways of making this easier are under advisement, and one
  270. will probably be supported eventually.
  271. Until this day comes, I suggest that the keywords be
  272. .ul
  273. reserved;
  274. that is, be forbidden for use as variable names.
  275. There are powerful stylistic reasons for preferring this, anyway
  276. (he said weakly . . . ).
  277. .SH
  278. Non-integer Values
  279. .PP
  280. Frequently, the user wishes to have values which are
  281. bigger than integers;
  282. again, this is an area where Yacc does not make the job as easy as it might,
  283. and some additional support is likely.
  284. Nevertheless, at the cost of writing a storage manager,
  285. the user can return pointers or indices to blocks of storage
  286. big enough to contain the full values desired.
  287. .SH
  288. Previous Work
  289. .PP
  290. There have been many previous applications of Yacc.
  291. The user who is contemplating a big application might well
  292. find that others have developed relevant techniques,
  293. or even portions of grammars.
  294. Yacc specifications appear to be easier to change than
  295. the equivalent computer programs, so that the ``prior art'' is more
  296. relevant here as well.
  297.