c-tokenize.lex 6.5 KB

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