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

  1. .SH
  2. Section 1: Basic Specifications
  3. .PP
  4. As we noted above, names refer to either tokens or nonterminal symbols.
  5. Yacc requires those names which will be
  6. used as token names to be declared as such.
  7. In addition, for reasons which will be discussed in Section 3, it is usually desirable
  8. to include the lexical analyzer as part of the specification file;
  9. it may be useful to include other programs as well.
  10. Thus, every specification file consists of three sections:
  11. the
  12. .ul
  13. declarations,
  14. .ul
  15. (grammar) rules,
  16. and
  17. .ul
  18. programs.
  19. The sections are separated by double percent ``%%'' marks.
  20. (The per-cent ``%'' is generally used in Yacc specifications as an escape character.)
  21. .PP
  22. In other words, a full specification file looks like
  23. .DS
  24. declarations
  25. %%
  26. rules
  27. %%
  28. programs
  29. .DE
  30. .PP
  31. The declaration section may be empty.
  32. Moreover, if the programs section is omitted, the second %% mark may be omitted also;
  33. thus, the smallest legal Yacc specification is
  34. .DS
  35. %%
  36. rules
  37. .DE
  38. .PP
  39. Blanks, tabs, and newlines are ignored except
  40. that they may not appear in names or multi-character reserved symbols.
  41. Comments may appear wherever a name or operator is legal; they are enclosed
  42. in /* . . . */, as in C and PL/I.
  43. .PP
  44. The rules section is made up of one or more grammar rules.
  45. A grammar rule has the form:
  46. .DS
  47. A  :  BODY  ;
  48. .DE
  49. A represents a nonterminal name, and BODY represents a sequence of zero or more names and literals.
  50. Notice that the colon and the semicolon are Yacc punctuation.
  51. .PP
  52. Names may be of arbitrary length, and may be made up of letters, dot ``.'', underscore ``\_'', and
  53. non-initial digits.
  54. Notice that Yacc considers that upper and lower case letters are distinct.
  55. The names used in the body of a grammar rule may represent tokens or nonterminal symbols.
  56. .PP
  57. A literal consists of a character enclosed in single quotes ``\'''.
  58. As in C, the backslash ``\e'' is an escape character within literals, and all the C escapes
  59. are recognized.
  60. Thus
  61. .DS
  62. \'\en\'    represents newline
  63. \'\er\'    represents return
  64. \'\e\'\'    represents single quote ``\'''
  65. \'\e\e\'    represents backslash ``\e''
  66. \'\et\'    represents tab
  67. \'\eb\'    represents backspace
  68. \'\exxx\' represents ``xxx'' in octal
  69. .DE
  70. For a number of technical reasons, the nul character (\'\e0\' or 000) should never
  71. be used in grammar rules.
  72. .PP
  73. If there are several grammar rules with the same left hand side, the vertical bar ``|''
  74. can be used to avoid rewriting the left hand side.
  75. In addition,
  76. the semicolon at the end of a rule can be dropped before a vertical bar.
  77. Thus the grammar rules
  78. .DS
  79. A : B C D   ;
  80. A : E F   ;
  81. A : G   ;
  82. .DE
  83. can be given to Yacc as
  84. .DS
  85. A :    B C D |
  86.     E F |
  87.     G ;
  88. .DE
  89. It is not necessary that all grammar rules with the same left side appear together in the grammar rules section,
  90. although it makes the input much more readable, and easy to change.
  91. .PP
  92. If a nonterminal symbol matches the empty string, this can be indicated in the obvious way:
  93. .DS
  94. empty :   ;
  95. .DE
  96. .PP
  97. As we mentioned above, names which represent
  98. tokens must be declared as such.
  99. The simplest way of doing this is to write
  100. .DS
  101. %token   name1 name2 . . .
  102. .DE
  103. in the declarations section.
  104. (See Sections 3 and 4 for much more discussion).
  105. Every name not defined in the declarations section is assumed to represent a nonterminal symbol.
  106. If, by the end of the rules section, some nonterminal symbol has not appeared on the left
  107. of any rule, then an error message is produced and Yacc halts.
  108. .PP
  109. The left hand side of the
  110. .I
  111. first
  112. .R
  113. grammar rule in the grammar rules section has special importance; it is taken to be the
  114. controlling nonterminal symbol for the entire input process;
  115. in technical language it is called the
  116. .I
  117. start symbol.
  118. .R
  119. In effect, the parser is designed to recognize the start symbol; thus,
  120. this symbol generally represents the largest,
  121. most general structure described by the grammar rules.
  122. .PP
  123. The end of the input is signaled by a special token, called the
  124. .ul
  125. endmarker.
  126. If the tokens up to, but not including, the endmarker form a structure
  127. which matches the start symbol, the parser subroutine returns to its caller
  128. when the endmarker is seen; we say that it
  129. .ul
  130. accepts
  131. the input.
  132. If the endmarker is seen in any other context, it is an error.
  133. .PP
  134. It is the job of the user supplied lexical analyzer
  135. to return the endmarker when appropriate; see section 3, below.
  136. Frequently, the endmarker token represents some reasonably obvious 
  137. I/O status, such as ``end-of-file'' or ``end-of-record''.
  138.