c-tokenize.lex 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. %option noyywrap
  2. %option nounput
  3. %pointer
  4. EOL \n
  5. SPACE [ \t\v\f]
  6. WS [ \t\v\n\f]
  7. DIGIT [0-9]
  8. LETTER [a-zA-Z_]
  9. OCTDIGIT [0-7]
  10. HEXDIGIT [a-fA-F0-9]
  11. EXPONENT [Ee][+-]?{DIGIT}+
  12. FLOQUAL (f|F|l|L)
  13. INTQUAL (l|L|ll|LL|lL|Ll|u|U)
  14. %{
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. /* Prevent compilation of static input() function in generated scanner
  19. code. This function is never actually used, and GCC 4.3 will emit
  20. an error for that. */
  21. #define YY_NO_INPUT
  22. int yylex(void);
  23. int yyget_lineno (void);
  24. FILE *yyget_in (void);
  25. FILE *yyget_out (void);
  26. int yyget_leng (void);
  27. char *yyget_text (void);
  28. void yyset_lineno (int line_number);
  29. void yyset_in (FILE * in_str);
  30. void yyset_out (FILE * out_str);
  31. int yyget_debug (void);
  32. void yyset_debug (int bdebug);
  33. int yylex_destroy (void);
  34. int filter_snarfage = 0;
  35. int print = 1;
  36. enum t_state {
  37. SKIP,
  38. MULTILINE,
  39. MULTILINE_COOKIE,
  40. COOKIE
  41. };
  42. enum t_state state = SKIP;
  43. int cookie_was_last = 0;
  44. #define OUT_RAW(type,text) if (print) printf ("(%s . \"%s\")\n", #type, text)
  45. #define OUT_T(type) OUT_RAW (type, yytext)
  46. #define OUT_S if (print) printf ("%s\n", yytext)
  47. #define OUT(type) if (print) printf ("%s\n", #type)
  48. #define IS_COOKIE cookie_was_last = 1
  49. #define IS_NOT_COOKIE cookie_was_last = 0
  50. %}
  51. %%
  52. \/\*(\n|[^*]|\*[^/])*\*\/ { OUT_T (comment); }
  53. ({SPACE}*(\\\n)*{SPACE}*)+ ;
  54. ({SPACE}*\n*{SPACE}*)+ { OUT(eol); }
  55. #.*\n { OUT(hash); IS_NOT_COOKIE; }
  56. {LETTER}({LETTER}|{DIGIT})* { OUT_T (id); IS_NOT_COOKIE; }
  57. 0[xX]{HEXDIGIT}+{INTQUAL}? { OUT_RAW (int_hex, yytext + 2); IS_NOT_COOKIE; }
  58. 0{OCTDIGIT}+{INTQUAL}? { OUT_RAW (int_oct, yytext + 1); IS_NOT_COOKIE; }
  59. {DIGIT}+{INTQUAL}? { OUT_T (int_dec); IS_NOT_COOKIE; }
  60. L?\'(\\.|[^\\\'])+\' { OUT_T (char); IS_NOT_COOKIE; }
  61. {DIGIT}+{EXPONENT}{FLOQUAL}? { OUT_T (flo_dec); IS_NOT_COOKIE; }
  62. {DIGIT}*"."{DIGIT}+({EXPONENT})?{FLOQUAL}? { OUT_T (flo_dec); IS_NOT_COOKIE; }
  63. {DIGIT}+"."{DIGIT}*({EXPONENT})?{FLOQUAL}? { OUT_T (flo_dec); IS_NOT_COOKIE; }
  64. L?\"(\\.|[^\\\"])*\" { OUT_S; IS_NOT_COOKIE; }
  65. "..." { OUT (ellipsis); IS_NOT_COOKIE; }
  66. ">>=" { OUT (shift_right_assign); IS_NOT_COOKIE; }
  67. "<<=" { OUT (shift_left_assign); IS_NOT_COOKIE; }
  68. "+=" { OUT (add_assign); IS_NOT_COOKIE; }
  69. "-=" { OUT (sub_assign); IS_NOT_COOKIE; }
  70. "*=" { OUT (mul-assign); IS_NOT_COOKIE; }
  71. "/=" { OUT (div_assign); IS_NOT_COOKIE; }
  72. "%=" { OUT (mod_assign); IS_NOT_COOKIE; }
  73. "&=" { OUT (logand_assign); IS_NOT_COOKIE; }
  74. "^=" { OUT (logxor_assign); IS_NOT_COOKIE; }
  75. "|=" { OUT (logior_assign); IS_NOT_COOKIE; }
  76. ">>" { OUT (right_shift); IS_NOT_COOKIE; }
  77. "<<" { OUT (left_shift); IS_NOT_COOKIE; }
  78. "++" { OUT (inc); IS_NOT_COOKIE; }
  79. "--" { OUT (dec); IS_NOT_COOKIE; }
  80. "->" { OUT (ptr); IS_NOT_COOKIE; }
  81. "&&" { OUT (and); IS_NOT_COOKIE; }
  82. "||" { OUT (or); IS_NOT_COOKIE; }
  83. "<=" { OUT (le); IS_NOT_COOKIE; }
  84. ">=" { OUT (ge); IS_NOT_COOKIE; }
  85. "==" { OUT (eq); IS_NOT_COOKIE; }
  86. "!=" { OUT (ne); IS_NOT_COOKIE; }
  87. ";" { OUT (semicolon); IS_NOT_COOKIE; }
  88. ("{"|"<%") {
  89. OUT (brace_open);
  90. if (filter_snarfage && cookie_was_last && state == COOKIE)
  91. state = MULTILINE;
  92. IS_NOT_COOKIE; }
  93. ("}"|"%>") {
  94. OUT (brace_close);
  95. if (filter_snarfage && cookie_was_last && state == MULTILINE_COOKIE) {
  96. state = SKIP;
  97. print = 0;
  98. }
  99. IS_NOT_COOKIE; }
  100. "," { OUT (comma); IS_NOT_COOKIE; }
  101. ":" { OUT (colon); IS_NOT_COOKIE; }
  102. "=" { OUT (assign); IS_NOT_COOKIE; }
  103. "(" { OUT (paren_open); IS_NOT_COOKIE; }
  104. ")" { OUT (paren_close); IS_NOT_COOKIE; }
  105. ("["|"<:") { OUT (bracket_open); IS_NOT_COOKIE; }
  106. ("]"|":>") { OUT (bracket_close); IS_NOT_COOKIE; }
  107. "." { OUT (dot); IS_NOT_COOKIE; }
  108. "&" { OUT (amp); IS_NOT_COOKIE; }
  109. "!" { OUT (bang); IS_NOT_COOKIE; }
  110. "~" { OUT (tilde); IS_NOT_COOKIE; }
  111. "-" { OUT (minus); IS_NOT_COOKIE; }
  112. "+" { OUT (plus); IS_NOT_COOKIE; }
  113. "*" { OUT (star); IS_NOT_COOKIE; }
  114. "/" { OUT (slash); IS_NOT_COOKIE; }
  115. "%" { OUT (percent); IS_NOT_COOKIE; }
  116. "<" { OUT (lt); IS_NOT_COOKIE; }
  117. ">" { OUT (gt); IS_NOT_COOKIE; }
  118. \^{WS}*\^ {
  119. if (filter_snarfage)
  120. switch (state) {
  121. case SKIP:
  122. state = COOKIE;
  123. print = 1;
  124. OUT (snarf_cookie);
  125. break;
  126. case MULTILINE:
  127. case MULTILINE_COOKIE:
  128. state = MULTILINE_COOKIE;
  129. OUT (snarf_cookie);
  130. break;
  131. case COOKIE:
  132. state = SKIP;
  133. OUT (snarf_cookie);
  134. print = 0;
  135. break;
  136. default:
  137. /* whoops */
  138. abort ();
  139. break;
  140. }
  141. else
  142. OUT (snarf_cookie);
  143. IS_COOKIE; }
  144. "^" { OUT (caret); IS_NOT_COOKIE; }
  145. "|" { OUT (pipe); IS_NOT_COOKIE; }
  146. "?" { OUT (question); IS_NOT_COOKIE; }
  147. . { fprintf (stderr, "*%s", yytext); fflush (stderr); IS_NOT_COOKIE; }
  148. %%
  149. int
  150. main (int argc, char *argv[])
  151. {
  152. if (argc > 1 && !strcmp (argv[1], "--filter-snarfage")) {
  153. filter_snarfage = 1;
  154. print = 0;
  155. }
  156. yylex ();
  157. return EXIT_SUCCESS;
  158. }
  159. /*
  160. Local Variables:
  161. c-file-style: "gnu"
  162. End:
  163. */