/usr.bin/lex/yylex.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 218 lines · 168 code · 22 blank · 28 comment · 11 complexity · 7929ef83deebbc10dbe7b11e72f23000 MD5 · raw file

  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. /* $Header: /home/daffy/u0/vern/flex/RCS/yylex.c,v 2.13 95/03/04 16:10:41 vern Exp $ */
  28. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include <ctype.h>
  31. #include "flexdef.h"
  32. #include "parse.h"
  33. /* yylex - scan for a regular expression token */
  34. int yylex()
  35. {
  36. int toktype;
  37. static int beglin = false;
  38. extern char *yytext;
  39. if ( eofseen )
  40. toktype = EOF;
  41. else
  42. toktype = flexscan();
  43. if ( toktype == EOF || toktype == 0 )
  44. {
  45. eofseen = 1;
  46. if ( sectnum == 1 )
  47. {
  48. synerr( _( "premature EOF" ) );
  49. sectnum = 2;
  50. toktype = SECTEND;
  51. }
  52. else
  53. toktype = 0;
  54. }
  55. if ( trace )
  56. {
  57. if ( beglin )
  58. {
  59. fprintf( stderr, "%d\t", num_rules + 1 );
  60. beglin = 0;
  61. }
  62. switch ( toktype )
  63. {
  64. case '<':
  65. case '>':
  66. case '^':
  67. case '$':
  68. case '"':
  69. case '[':
  70. case ']':
  71. case '{':
  72. case '}':
  73. case '|':
  74. case '(':
  75. case ')':
  76. case '-':
  77. case '/':
  78. case '\\':
  79. case '?':
  80. case '.':
  81. case '*':
  82. case '+':
  83. case ',':
  84. (void) putc( toktype, stderr );
  85. break;
  86. case '\n':
  87. (void) putc( '\n', stderr );
  88. if ( sectnum == 2 )
  89. beglin = 1;
  90. break;
  91. case SCDECL:
  92. fputs( "%s", stderr );
  93. break;
  94. case XSCDECL:
  95. fputs( "%x", stderr );
  96. break;
  97. case SECTEND:
  98. fputs( "%%\n", stderr );
  99. /* We set beglin to be true so we'll start
  100. * writing out numbers as we echo rules.
  101. * flexscan() has already assigned sectnum.
  102. */
  103. if ( sectnum == 2 )
  104. beglin = 1;
  105. break;
  106. case NAME:
  107. fprintf( stderr, "'%s'", nmstr );
  108. break;
  109. case CHAR:
  110. switch ( yylval )
  111. {
  112. case '<':
  113. case '>':
  114. case '^':
  115. case '$':
  116. case '"':
  117. case '[':
  118. case ']':
  119. case '{':
  120. case '}':
  121. case '|':
  122. case '(':
  123. case ')':
  124. case '-':
  125. case '/':
  126. case '\\':
  127. case '?':
  128. case '.':
  129. case '*':
  130. case '+':
  131. case ',':
  132. fprintf( stderr, "\\%c",
  133. yylval );
  134. break;
  135. default:
  136. if ( ! isascii( yylval ) ||
  137. ! isprint( yylval ) )
  138. fprintf( stderr,
  139. "\\%.3o",
  140. (unsigned int) yylval );
  141. else
  142. (void) putc( yylval,
  143. stderr );
  144. break;
  145. }
  146. break;
  147. case NUMBER:
  148. fprintf( stderr, "%d", yylval );
  149. break;
  150. case PREVCCL:
  151. fprintf( stderr, "[%d]", yylval );
  152. break;
  153. case EOF_OP:
  154. fprintf( stderr, "<<EOF>>" );
  155. break;
  156. case OPTION_OP:
  157. fprintf( stderr, "%s ", yytext );
  158. break;
  159. case OPT_OUTFILE:
  160. case OPT_PREFIX:
  161. case CCE_ALNUM:
  162. case CCE_ALPHA:
  163. case CCE_BLANK:
  164. case CCE_CNTRL:
  165. case CCE_DIGIT:
  166. case CCE_GRAPH:
  167. case CCE_LOWER:
  168. case CCE_PRINT:
  169. case CCE_PUNCT:
  170. case CCE_SPACE:
  171. case CCE_UPPER:
  172. case CCE_XDIGIT:
  173. fprintf( stderr, "%s", yytext );
  174. break;
  175. case 0:
  176. fprintf( stderr, _( "End Marker\n" ) );
  177. break;
  178. default:
  179. fprintf( stderr,
  180. _( "*Something Weird* - tok: %d val: %d\n" ),
  181. toktype, yylval );
  182. break;
  183. }
  184. }
  185. return toktype;
  186. }