/usr.bin/lex/yylex.c

https://bitbucket.org/kmv/aeriebsd-src · C · 220 lines · 166 code · 22 blank · 32 comment · 11 complexity · 4abe49455521fc67ab0676bd12b38e33 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, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. *
  23. * Neither the name of the University nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE.
  31. */
  32. #include <ctype.h>
  33. #include "flexdef.h"
  34. #include "parse.h"
  35. /* yylex - scan for a regular expression token */
  36. int yylex()
  37. {
  38. int toktype;
  39. static int beglin = false;
  40. extern char *yytext;
  41. if ( eofseen )
  42. toktype = EOF;
  43. else
  44. toktype = flexscan();
  45. if ( toktype == EOF || toktype == 0 )
  46. {
  47. eofseen = 1;
  48. if ( sectnum == 1 )
  49. {
  50. synerr( _( "premature EOF" ) );
  51. sectnum = 2;
  52. toktype = SECTEND;
  53. }
  54. else
  55. toktype = 0;
  56. }
  57. if ( trace )
  58. {
  59. if ( beglin )
  60. {
  61. fprintf( stderr, "%d\t", num_rules + 1 );
  62. beglin = 0;
  63. }
  64. switch ( toktype )
  65. {
  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. case '+':
  85. case ',':
  86. (void) putc( toktype, stderr );
  87. break;
  88. case '\n':
  89. (void) putc( '\n', stderr );
  90. if ( sectnum == 2 )
  91. beglin = 1;
  92. break;
  93. case SCDECL:
  94. fputs( "%s", stderr );
  95. break;
  96. case XSCDECL:
  97. fputs( "%x", stderr );
  98. break;
  99. case SECTEND:
  100. fputs( "%%\n", stderr );
  101. /* We set beglin to be true so we'll start
  102. * writing out numbers as we echo rules.
  103. * flexscan() has already assigned sectnum.
  104. */
  105. if ( sectnum == 2 )
  106. beglin = 1;
  107. break;
  108. case NAME:
  109. fprintf( stderr, "'%s'", nmstr );
  110. break;
  111. case CHAR:
  112. switch ( yylval )
  113. {
  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. case '+':
  133. case ',':
  134. fprintf( stderr, "\\%c",
  135. yylval );
  136. break;
  137. default:
  138. if ( ! isascii( yylval ) ||
  139. ! isprint( yylval ) )
  140. fprintf( stderr,
  141. "\\%.3o",
  142. (unsigned int) yylval );
  143. else
  144. (void) putc( yylval,
  145. stderr );
  146. break;
  147. }
  148. break;
  149. case NUMBER:
  150. fprintf( stderr, "%d", yylval );
  151. break;
  152. case PREVCCL:
  153. fprintf( stderr, "[%d]", yylval );
  154. break;
  155. case EOF_OP:
  156. fprintf( stderr, "<<EOF>>" );
  157. break;
  158. case OPTION_OP:
  159. fprintf( stderr, "%s ", yytext );
  160. break;
  161. case OPT_OUTFILE:
  162. case OPT_PREFIX:
  163. case CCE_ALNUM:
  164. case CCE_ALPHA:
  165. case CCE_BLANK:
  166. case CCE_CNTRL:
  167. case CCE_DIGIT:
  168. case CCE_GRAPH:
  169. case CCE_LOWER:
  170. case CCE_PRINT:
  171. case CCE_PUNCT:
  172. case CCE_SPACE:
  173. case CCE_UPPER:
  174. case CCE_XDIGIT:
  175. fprintf( stderr, "%s", yytext );
  176. break;
  177. case 0:
  178. fprintf( stderr, _( "End Marker\n" ) );
  179. break;
  180. default:
  181. fprintf( stderr,
  182. _( "*Something Weird* - tok: %d val: %d\n" ),
  183. toktype, yylval );
  184. break;
  185. }
  186. }
  187. return toktype;
  188. }