c-tokenize.lex 6.5 KB

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