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

  1. .SH
  2. Section 2: Actions
  3. .PP
  4. To each grammar rule, the user may associate an action to be performed each time
  5. the rule is recognized in the input process.
  6. This action may return a value, and may obtain the values returned by previous
  7. actions in the grammar rule.
  8. In addition, the lexical analyzer can return values
  9. for tokens, if desired.
  10. .PP
  11. When invoking Yacc, the user specifies a programming language; currently, Ratfor and C are supported.
  12. An action is an arbitrary statement in this language, and as such can do
  13. input and output, call subprograms, and alter
  14. external vectors and variables (recall that a ``statement'' in both C and Ratfor can be compound
  15. and do many distinct tasks).
  16. An action is specified by an equal sign ``=''
  17. at the end of a grammar rule, followed by
  18. one or more statements, enclosed in curly braces ``{'' and ``}''.
  19. For example,
  20. .DS
  21. A: \'(\' B \')\' = { hello( 1, "abc" );  }
  22. .DE
  23. and
  24. .DS
  25. XXX: YYY ZZZ =
  26.     {
  27.         printf("a message\en");
  28.         flag = 25;
  29.     }
  30. .DE
  31. are grammar rules with actions in C.
  32. A grammar rule with an action need not end with a semicolon; in fact, it is an error to have a semicolon
  33. before the equal sign.
  34. .PP
  35. To facilitate easy communication between the actions and the parser, the action statements are altered
  36. slightly.
  37. The symbol ``dollar sign'' ``$'' is used as a signal to Yacc in this context.
  38. .PP
  39. To return a value, the action normally sets the
  40. pseudo-variable ``$$'' to some integer value.
  41. For example, an action which does nothing but return the value 1 is
  42. .DS
  43. = { $$ = 1; }
  44. .DE
  45. .PP
  46. To obtain the values returned by previous actions and the lexical analyzer, the
  47. action may use the (integer) pseudo-variables $1, $2, . . .,
  48. which refer to the values returned by the
  49. components of the right side of a rule, reading from left to right.
  50. Thus, if the rule is
  51. .DS
  52. A: B C D ;
  53. .DE
  54. for example, then $2 has the value returned by C, and $3 the value returned by D.
  55. .PP
  56. As a more concrete example, we might have the rule
  57. .DS
  58. expression: \'(\' expression \')\' ;
  59. .DE
  60. We wish the value returned by this rule to be the value of the expression in parentheses.
  61. Then we write
  62. .DS
  63. expression: \'(\' expression \')\'    = { $$ = $2 ; }
  64. .DE
  65. .PP
  66. As a default, the value of a rule is the value of the first element in it ($1).
  67. This is true even if there is no explicit action given for the rule.
  68. Thus, grammar rules of the form
  69. .DS
  70. A: B ;
  71. .DE
  72. frequently need not have an explict action.
  73. .PP
  74. Notice that, although the values of actions are integers, these integers may in fact
  75. contain pointers (in C) or indices into an array (in Ratfor); in this way,
  76. actions can return and reference more complex data structures.
  77. .PP
  78. Sometimes, we wish to get control before a rule is fully parsed, as well as at the
  79. end of the rule.
  80. There is no explicit mechanism in Yacc to allow this; the same effect can be obtained, however,
  81. by introducing a new symbol which matches the empty string, and inserting an action for this symbol.
  82. For example, we might have a rule describing an ``if'' statement:
  83. .DS
  84. statement: IF \'(\' expr \')\' THEN statement
  85. .DE
  86. Suppose that we wish to get control after seeing the right parenthesis
  87. in order to output some code.
  88. We might accomplish this by the rules:
  89. .DS
  90. statement:  IF \'(\' expr \')\' actn THEN statement 
  91.     = { call action1 }
  92.  
  93. actn:   /* matches the empty string */
  94.     = { call action2 }
  95. .DE
  96. .PP
  97. Thus, the new nonterminal symbol actn matches no input, but serves only to call action2 after the
  98. right parenthesis is seen.
  99. .PP
  100. Frequently, it is more natural in such cases to break the rule into
  101. parts where the action is needed.
  102. Thus, the above example might also have been written
  103. .DS
  104. statement:  ifpart THEN statement
  105.     = { call action1 }
  106.  
  107. ifpart:      IF \'(\' expr \')\'
  108.     = { call action2 }
  109. .DE
  110. .PP
  111. In many applications, output is not done directly by the actions;
  112. rather, a data structure, such as a parse tree, is constructed in memory,
  113. and transformations are applied to it before output is generated.
  114. Parse trees are particularly easy to
  115. construct, given routines which build and maintain the tree
  116. structure desired.
  117. For example, suppose we have a C function
  118. ``node'', written so that the call
  119. .DS
  120. node( L, n1, n2 )
  121. .DE
  122. creates a node with label L, and descendants n1 and n2, and returns a pointer
  123. to the newly created node.
  124. Then we can cause a parse tree to be built by supplying actions such as:
  125. .DS
  126. expr: expr \'+\' expr 
  127.     = { $$ = node( \'+\', $1, $3 ); }
  128. .DE
  129. in our specification.
  130. .PP
  131. The user may define other variables to be used by the actions.
  132. Declarations and definitions can appear in two places in the
  133. Yacc specification: in the declarations section, and at the head of the rules sections, before the
  134. first grammar rule.
  135. In each case, the declarations and definitions are enclosed in the marks ``%{'' and ``%}''.
  136. Declarations and definitions placed in the declarations section have global scope, 
  137. and are thus known to the action statements and the lexical analyzer.
  138. Declarations and definitions placed at the head of the rules section have scope local to
  139. the action statements.
  140. Thus, in the above example, we might have included
  141. .DS
  142. %{ int variable 0; %}
  143. .DE
  144. in the declarations section, or, perhaps,
  145. .DS
  146. %{ static int variable; %}
  147. .DE
  148. at the head of the rules section.
  149. If we were writing Ratfor actions, we might want to include some
  150. COMMON statements at the beginning of the rules section, to allow for
  151. easy communication between the actions and other routines.
  152. For both C and Ratfor, Yacc has used only external names beginning in ``yy'';
  153. the user should avoid such names.
  154.