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

  1. .SH
  2. Section 6C: The C Language Yacc Environment
  3. .PP
  4. The default mode of operation in Yacc is to
  5. write actions and the lexical
  6. analyzer in C.
  7. This has a number of advantages; primarily,
  8. it is easier to write character handling
  9. routines, such as the lexical analyzer, in a language
  10. which supports character-by-character I/O, and has
  11. shifting and masking operators.
  12. .PP
  13. When the user inputs a specification
  14. to Yacc, the output is a file of C programs, called
  15. ``y.tab.c''.
  16. These are then compiled, and loaded with a library;
  17. the library has default versions of a number of useful
  18. routines.
  19. This section discusses these routines, and
  20. how the user can write his own routines if desired.
  21. The name of the Yacc library is system dependent; see Appendix B.
  22. .PP
  23. The subroutine produced by Yacc is called ``yyparse'';
  24. it is an integer valued function.
  25. When it is called, it in turn repeatedly calls ``yylex'', the lexical analyzer
  26. supplied by the user (see Section 3),
  27. to obtain input tokens.
  28. Eventually, either an error is detected, in which case
  29. (if no error recovery is possible)
  30. yyparse returns the value 1,
  31. or the lexical analyzer returns the endmarker token
  32. (type number 0), and the parser accepts.
  33. In this case, yyparse returns the value 0.
  34. .PP
  35. Three of the routines on the Yacc library are concerned with
  36. the ``external'' environment of yyparse.
  37. There is a default ``main'' program, a default ``initialization'' routine,
  38. and a default ``accept'' routine, respectively.
  39. They are so simple that they will be given here in their entirety:
  40. .DS
  41. main( argc, argv )
  42. int argc;
  43. char *argv[ ]
  44. {
  45.     yyinit( argc, argv );
  46.     if( yyparse( ) )
  47.         return;
  48.     yyaccpt( );
  49. }
  50.  
  51. yyinit( ) { }
  52.  
  53. yyaccpt( ) { }
  54. .DE
  55. By supplying his own versions of yyinit and/or yyaccpt,
  56. the user can get control either before the parser is called
  57. (to set options, open input files, etc.)
  58. or after the accept action has been done
  59. (to close files, call the next pass of the compiler, etc.).
  60. Note that yyinit is called with the two ``command line'' arguments
  61. which have been passed into the main program.
  62. If neither of these routines is redefined,
  63. the default situation simply looks like a call
  64. to the parser, followed by the termination of the program.
  65. Of course, in many cases the user will wish to supply his own
  66. main program; for example, this is necessary if the
  67. parser is to be called more than once.
  68. .PP
  69. The other major routine on the library
  70. is called ``yyerror''; its main purpose is to write out a message
  71. when a syntax error is detected.
  72. It has a number of hooks and handles which attempt to
  73. make this error message general and easy to
  74. understand.
  75. This routine is somewhat more complex, but still approachable:
  76. .DS
  77. extern int yyline;  /* input line number */
  78.  
  79. yyerror(s)
  80. char *s;
  81. {
  82.     extern int yychar;
  83.     extern char *yysterm[ ];
  84.  
  85.     printf("\en%s", s );
  86.     if( yyline )
  87.         printf(", line %d,", yyline );
  88.     printf(" on input: ");
  89.     if( yychar >= 0400 )
  90.         printf("%s\en", yysterm[yychar\-0400] );
  91.     else switch ( yychar ) {
  92.     case \'\et\': printf( "\e\et\en" ); return;
  93.     case \'\en\': printf( "\e\en\en" ); return;
  94.     case \'\e0\': printf( "$end\en" ); return;
  95.     default: printf( "%c\en" , yychar ); return;
  96.     }
  97. }
  98. .DE
  99. The argument to yyerror is a string containing an
  100. error message; most usually, it is ``syntax error''.
  101. yyerror also uses the external variables
  102. yyline, yychar, and yysterm.
  103. yyline is a line number which,
  104. if set by the user to a nonzero number, will
  105. be printed out as part of the error message.
  106. yychar is a variable which contains the type number of the current
  107. token.
  108. yysterm has the names, supplied by the user, for all the tokens
  109. which have names.
  110. Thus, the routine spends most of its time
  111. trying to print out a reasonable name for the input token.
  112. The biggest problem with the routine as given is that,
  113. on Unix, the error message does not go out on the
  114. error file (file 2).
  115. This is hard to arrange in such a way that it works with both
  116. the portable I/O library and the system I/O library;
  117. if a way can be worked out, the routine will be changed
  118. to do this.
  119. .ul
  120. Beware:
  121. This routine will not work if any token names
  122. have been given redefined type numbers.
  123. In this case, the user must supply his own yyerror routine.
  124. Hopefully, this ``feature'' will disappear
  125. soon.
  126. .PP
  127. Finally, there is another feature which the C user of Yacc might wish to use.
  128. The integer variable yydebug is normally set to 0.
  129. If it is set to 1, the parser will output a
  130. verbose description of its actions, including
  131. a discussion of which input symbols have been read, and
  132. what the parser actions are.
  133. Depending on the operating environment,
  134. it may be possible to set this variable by using a debugging system.
  135.