yylex.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* yylex - scanner front-end for flex */
  2. /*-
  3. * Copyright (c) 1990 The Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Vern Paxson.
  8. *
  9. * The United States Government has rights in this work pursuant
  10. * to contract no. DE-AC03-76SF00098 between the United States
  11. * Department of Energy and the University of California.
  12. *
  13. * Redistribution and use in source and binary forms are permitted provided
  14. * that: (1) source distributions retain this entire copyright notice and
  15. * comment, and (2) distributions including binaries display the following
  16. * acknowledgement: ``This product includes software developed by the
  17. * University of California, Berkeley and its contributors'' in the
  18. * documentation or other materials provided with the distribution and in
  19. * all advertising materials mentioning features or use of this software.
  20. * Neither the name of the University nor the names of its contributors may
  21. * be used to endorse or promote products derived from this software without
  22. * specific prior written permission.
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  26. */
  27. #ifndef lint
  28. static char rcsid[] =
  29. "@(#) $Id$ (LBL)";
  30. #endif
  31. #include <ctype.h>
  32. #include "flexdef.h"
  33. #include "parse.h"
  34. /* ANSI C does not guarantee that isascii() is defined */
  35. #ifndef isascii
  36. #define isascii(c) ((c) <= 0177)
  37. #endif
  38. /* yylex - scan for a regular expression token
  39. *
  40. * synopsis
  41. *
  42. * token = yylex();
  43. *
  44. * token - return token found
  45. */
  46. int yylex()
  47. {
  48. int toktype;
  49. static int beglin = false;
  50. if ( eofseen )
  51. toktype = EOF;
  52. else
  53. toktype = flexscan();
  54. if ( toktype == EOF || toktype == 0 )
  55. {
  56. eofseen = 1;
  57. if ( sectnum == 1 )
  58. {
  59. synerr( "premature EOF" );
  60. sectnum = 2;
  61. toktype = SECTEND;
  62. }
  63. else if ( sectnum == 2 )
  64. {
  65. sectnum = 3;
  66. toktype = 0;
  67. }
  68. else
  69. toktype = 0;
  70. }
  71. if ( trace )
  72. {
  73. if ( beglin )
  74. {
  75. fprintf( stderr, "%d\t", num_rules + 1 );
  76. beglin = 0;
  77. }
  78. switch ( toktype )
  79. {
  80. case '<':
  81. case '>':
  82. case '^':
  83. case '$':
  84. case '"':
  85. case '[':
  86. case ']':
  87. case '{':
  88. case '}':
  89. case '|':
  90. case '(':
  91. case ')':
  92. case '-':
  93. case '/':
  94. case '\\':
  95. case '?':
  96. case '.':
  97. case '*':
  98. case '+':
  99. case ',':
  100. (void) putc( toktype, stderr );
  101. break;
  102. case '\n':
  103. (void) putc( '\n', stderr );
  104. if ( sectnum == 2 )
  105. beglin = 1;
  106. break;
  107. case SCDECL:
  108. fputs( "%s", stderr );
  109. break;
  110. case XSCDECL:
  111. fputs( "%x", stderr );
  112. break;
  113. case WHITESPACE:
  114. (void) putc( ' ', stderr );
  115. break;
  116. case SECTEND:
  117. fputs( "%%\n", stderr );
  118. /* we set beglin to be true so we'll start
  119. * writing out numbers as we echo rules. flexscan() has
  120. * already assigned sectnum
  121. */
  122. if ( sectnum == 2 )
  123. beglin = 1;
  124. break;
  125. case NAME:
  126. fprintf( stderr, "'%s'", nmstr );
  127. break;
  128. case CHAR:
  129. switch ( yylval )
  130. {
  131. case '<':
  132. case '>':
  133. case '^':
  134. case '$':
  135. case '"':
  136. case '[':
  137. case ']':
  138. case '{':
  139. case '}':
  140. case '|':
  141. case '(':
  142. case ')':
  143. case '-':
  144. case '/':
  145. case '\\':
  146. case '?':
  147. case '.':
  148. case '*':
  149. case '+':
  150. case ',':
  151. fprintf( stderr, "\\%c", yylval );
  152. break;
  153. default:
  154. if ( ! isascii( yylval ) || ! isprint( yylval ) )
  155. fprintf( stderr, "\\%.3o", yylval );
  156. else
  157. (void) putc( yylval, stderr );
  158. break;
  159. }
  160. break;
  161. case NUMBER:
  162. fprintf( stderr, "%d", yylval );
  163. break;
  164. case PREVCCL:
  165. fprintf( stderr, "[%d]", yylval );
  166. break;
  167. case EOF_OP:
  168. fprintf( stderr, "<<EOF>>" );
  169. break;
  170. case 0:
  171. fprintf( stderr, "End Marker" );
  172. break;
  173. default:
  174. fprintf( stderr, "*Something Weird* - tok: %d val: %d\n",
  175. toktype, yylval );
  176. break;
  177. }
  178. }
  179. return ( toktype );
  180. }