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

  1. .SH
  2. Section 0: Introduction
  3. .PP
  4. Yacc provides a general tool for imposing structure on the input to a computer program.
  5. The Yacc user prepares a
  6. specification of the input process; this includes rules
  7. which describe the input structure, code which is to be invoked when these
  8. structures are recognized, and a low-level routine to do the
  9. basic input.
  10. Yacc then produces a subroutine to do the input procedure;
  11. this subroutine, called a
  12. .I
  13. parser,
  14. .R
  15. calls the user-supplied low-level input routine
  16. (called the
  17. .I
  18. lexical analyzer)
  19. .R
  20. to pick up the basic items
  21. (called
  22. .I
  23. tokens)
  24. .R
  25. from the input stream.
  26. These tokens are organized according to the input structure rules,
  27. called
  28. .I
  29. grammar rules;
  30. .R
  31. when one of these rules has been recognized,
  32. then the user code supplied for this rule, called an
  33. .I
  34. action,
  35. .R
  36. is invoked; actions have the ability to return values and
  37. make use of the values of other actions.
  38. .PP
  39. The heart of the input specification is a collection of grammar rules.
  40. Each rule describes an allowable structure and gives it a name.
  41. For example, one grammar rule might be
  42. .DS
  43. date  :  month\_name  day  \',\'  year   ;
  44. .DE
  45. Here, date, month\_name, day, and year represent structures of interest in the input process;
  46. presumably, month\_name, day, and year are defined elsewhere.
  47. The comma ``,'' is quoted by single quotes; this implies that the
  48. comma is to appear literally in the input.
  49. The colon and semicolon merely serve as punctuation in the rule, and have
  50. no significance in controlling the input.
  51. Thus, with proper definitions, the input
  52. .DS
  53. July  4, 1776
  54. .DE
  55. might be matched by the above rule.
  56. .PP
  57. As we mentioned above, an important part of the input process is carried out by the
  58. lexical analyzer.
  59. This user routine reads the true input stream, recognizing those
  60. structures which are more conveniently or more
  61. efficiently recognized directly, and communicates these recognized tokens
  62. to the parser.
  63. For historical reasons, the name of a structure recognized by the lexical analyzer is called a
  64. .I
  65. terminal symbol
  66. .R
  67. name, while the name of a structure recognized by the parser is called a
  68. .I
  69. nonterminal symbol
  70. .R
  71. name.
  72. To avoid the obvious confusion of terminology, we shall usually refer to
  73. terminal symbol names as
  74. .I
  75. token names.
  76. .R
  77. .PP
  78. There is considerable leeway in deciding whether to recognize structures by the lexical
  79. analyzer or by a grammar rule.
  80. Thus, in the above example it would be possible to have other rules of the form
  81. .DS
  82. month\_name  :  \'J\' \'a\' \'n\'   ;
  83. month\_name  :  \'F\' \'e\' \'b\'   ;
  84.  
  85.          . . .
  86.  
  87. month\_name  :  \'D\' \'e\' \'c\'   ;
  88. .DE
  89. Here, the lexical analyzer would only need to recognize individual letters, and
  90. month\_name would be a nonterminal symbol.
  91. Rules of this sort tend to be a bit wasteful of time and space, and may
  92. even restrict the power of the input process
  93. (although they are easy to write).
  94. For a more efficient input process, the lexical analyzer itself might
  95. recognize the month names,
  96. and return an indication that a
  97. month\_name was seen; in this case, month\_name would be a token.
  98. .PP
  99. Literal characters, such as ``,'', must also be passed through the lexical
  100. analyzer, and are considered tokens.
  101. .PP
  102. As an example of the flexibility of the grammar rule approach, we might add to the above
  103. specifications the rule
  104. .DS
  105. date  :  month \'/\' day \'/\' year   ;
  106. .DE
  107. and thus optionally allow the form
  108. .DS
  109. 7/4/1776
  110. .DE
  111. as a synonym for
  112. .DS
  113. July 4, 1776
  114. .DE
  115. In most cases, this new rule could be ``slipped in'' to a working system with minimal effort,
  116. and a very small chance of disrupting existing input.
  117. .PP
  118. Frequently, the input being read does not conform to the
  119. specifications due to errors in the
  120. input.
  121. The parsers produced by Yacc have
  122. the very desirable property that they will detect these
  123. input errors at the earliest
  124. place at which this can be done with a
  125. left-to-right scan;
  126. thus, not only is the chance of reading and computing with bad
  127. input data substantially reduced, but the bad data can usually be quickly found.
  128. Error handling facilities,
  129. entered as part of the input specifications, frequently
  130. permit the reentry of bad data,
  131. or the continuation of the input process after skipping over the bad data.
  132. .PP
  133. In some cases, Yacc fails to produce a parser when given a set of
  134. specifications.
  135. For example, the specifications may be self contradictory, or they may
  136. require a more powerful recognition mechanism than that available to Yacc.
  137. The former cases probably represent true design errors;
  138. the latter cases
  139. can often be corrected
  140. by making
  141. the lexical analyzer
  142. more powerful, or by rewriting some of the grammar rules.
  143. The class of specifications which Yacc can handle compares very favorably
  144. with other systems of this type; moreover, the
  145. constructions which are difficult for Yacc to handle are
  146. also frequently difficult for human beings to handle.
  147. Some users have reported that the discipline of formulating valid
  148. Yacc specifications for their input revealed errors of
  149. conception or design early in the program development.
  150. .PP
  151. The next several sections describe the
  152. basic process of preparing a Yacc specification;
  153. Section 1 describes the preparation of grammar rules,
  154. Section 2 the preparation of the user supplied actions associated with these rules,
  155. and Section 3 the preparation of lexical analyzers.
  156. In Section 4, we discuss the
  157. diagnostics produced when Yacc
  158. is unable to produce a parser
  159. from the given specifications.
  160. This section also describes a simple, frequently useful mechanism for
  161. handling operator precedences.
  162. Section 5 discusses error detection and recovery.
  163. Sections 6C and 6R discuss the operating environment and special features
  164. of the subroutines which Yacc produces in C and Ratfor, respectively.
  165. Section 7 gives some hints which
  166. may lead to better designed, more efficient,
  167. and clearer specifications.
  168. Finally, Section 8 has a brief summary.
  169. Appendix A has a brief example, and Appendix B tells how to run Yacc on
  170. the UNIX operating system.
  171. Appendix C has a brief description of mechanisms and syntax
  172. which are no longer actively supported, but which
  173. are provided for historical continuity with older versions of Yacc.
  174.