c-tokenize.lex 6.3 KB

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