c99.l 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. D [0-9]
  2. L [a-zA-Z_]
  3. H [a-fA-F0-9]
  4. E ([Ee][+-]?{D}+)
  5. P ([Pp][+-]?{D}+)
  6. FS (f|F|l|L)
  7. IS ((u|U)|(u|U)?(l|L|ll|LL)|(l|L|ll|LL)(u|U))
  8. %{
  9. #include <stdio.h>
  10. #include "y.tab.h"
  11. void count(void);
  12. %}
  13. %%
  14. "/*" { comment(); }
  15. "//"[^\n]* { /* consume //-comment */ }
  16. "auto" { count(); return(AUTO); }
  17. "_Bool" { count(); return(BOOL); }
  18. "break" { count(); return(BREAK); }
  19. "case" { count(); return(CASE); }
  20. "char" { count(); return(CHAR); }
  21. "_Complex" { count(); return(COMPLEX); }
  22. "const" { count(); return(CONST); }
  23. "continue" { count(); return(CONTINUE); }
  24. "default" { count(); return(DEFAULT); }
  25. "do" { count(); return(DO); }
  26. "double" { count(); return(DOUBLE); }
  27. "else" { count(); return(ELSE); }
  28. "enum" { count(); return(ENUM); }
  29. "extern" { count(); return(EXTERN); }
  30. "float" { count(); return(FLOAT); }
  31. "for" { count(); return(FOR); }
  32. "goto" { count(); return(GOTO); }
  33. "if" { count(); return(IF); }
  34. "_Imaginary" { count(); return(IMAGINARY); }
  35. "inline" { count(); return(INLINE); }
  36. "int" { count(); return(INT); }
  37. "long" { count(); return(LONG); }
  38. "register" { count(); return(REGISTER); }
  39. "restrict" { count(); return(RESTRICT); }
  40. "return" { count(); return(RETURN); }
  41. "short" { count(); return(SHORT); }
  42. "signed" { count(); return(SIGNED); }
  43. "sizeof" { count(); return(SIZEOF); }
  44. "static" { count(); return(STATIC); }
  45. "struct" { count(); return(STRUCT); }
  46. "switch" { count(); return(SWITCH); }
  47. "typedef" { count(); return(TYPEDEF); }
  48. "union" { count(); return(UNION); }
  49. "unsigned" { count(); return(UNSIGNED); }
  50. "void" { count(); return(VOID); }
  51. "volatile" { count(); return(VOLATILE); }
  52. "while" { count(); return(WHILE); }
  53. {L}({L}|{D})* { count(); return(check_type()); }
  54. 0[xX]{H}+{IS}? { count(); return(CONSTANT); }
  55. 0[0-7]*{IS}? { count(); return(CONSTANT); }
  56. [1-9]{D}*{IS}? { count(); return(CONSTANT); }
  57. L?'(\\.|[^\\'\n])+' { count(); return(CONSTANT); }
  58. {D}+{E}{FS}? { count(); return(CONSTANT); }
  59. {D}*"."{D}+{E}?{FS}? { count(); return(CONSTANT); }
  60. {D}+"."{D}*{E}?{FS}? { count(); return(CONSTANT); }
  61. 0[xX]{H}+{P}{FS}? { count(); return(CONSTANT); }
  62. 0[xX]{H}*"."{H}+{P}?{FS}? { count(); return(CONSTANT); }
  63. 0[xX]{H}+"."{H}*{P}?{FS}? { count(); return(CONSTANT); }
  64. L?\"(\\.|[^\\"\n])*\" { count(); return(STRING_LITERAL); }
  65. "..." { count(); return(ELLIPSIS); }
  66. ">>=" { count(); return(RIGHT_ASSIGN); }
  67. "<<=" { count(); return(LEFT_ASSIGN); }
  68. "+=" { count(); return(ADD_ASSIGN); }
  69. "-=" { count(); return(SUB_ASSIGN); }
  70. "*=" { count(); return(MUL_ASSIGN); }
  71. "/=" { count(); return(DIV_ASSIGN); }
  72. "%=" { count(); return(MOD_ASSIGN); }
  73. "&=" { count(); return(AND_ASSIGN); }
  74. "^=" { count(); return(XOR_ASSIGN); }
  75. "|=" { count(); return(OR_ASSIGN); }
  76. ">>" { count(); return(RIGHT_OP); }
  77. "<<" { count(); return(LEFT_OP); }
  78. "++" { count(); return(INC_OP); }
  79. "--" { count(); return(DEC_OP); }
  80. "->" { count(); return(PTR_OP); }
  81. "&&" { count(); return(AND_OP); }
  82. "||" { count(); return(OR_OP); }
  83. "<=" { count(); return(LE_OP); }
  84. ">=" { count(); return(GE_OP); }
  85. "==" { count(); return(EQ_OP); }
  86. "!=" { count(); return(NE_OP); }
  87. ";" { count(); return(';'); }
  88. ("{"|"<%") { count(); return('{'); }
  89. ("}"|"%>") { count(); return('}'); }
  90. "," { count(); return(','); }
  91. ":" { count(); return(':'); }
  92. "=" { count(); return('='); }
  93. "(" { count(); return('('); }
  94. ")" { count(); return(')'); }
  95. ("["|"<:") { count(); return('['); }
  96. ("]"|":>") { count(); return(']'); }
  97. "." { count(); return('.'); }
  98. "&" { count(); return('&'); }
  99. "!" { count(); return('!'); }
  100. "~" { count(); return('~'); }
  101. "-" { count(); return('-'); }
  102. "+" { count(); return('+'); }
  103. "*" { count(); return('*'); }
  104. "/" { count(); return('/'); }
  105. "%" { count(); return('%'); }
  106. "<" { count(); return('<'); }
  107. ">" { count(); return('>'); }
  108. "^" { count(); return('^'); }
  109. "|" { count(); return('|'); }
  110. "?" { count(); return('?'); }
  111. [ \t\v\n\f] { count(); }
  112. . { /* Add code to complain about unmatched characters */ }
  113. %%
  114. int yywrap(void)
  115. {
  116. return 1;
  117. }
  118. void comment(void)
  119. {
  120. char c, prev = 0;
  121. while ((c = input()) != 0) /* (EOF maps to 0) */
  122. {
  123. if (c == '/' && prev == '*')
  124. return;
  125. prev = c;
  126. }
  127. error("unterminated comment");
  128. }
  129. int column = 0;
  130. void count(void)
  131. {
  132. int i;
  133. for (i = 0; yytext[i] != '\0'; i++)
  134. if (yytext[i] == '\n')
  135. column = 0;
  136. else if (yytext[i] == '\t')
  137. column += 8 - (column % 8);
  138. else
  139. column++;
  140. ECHO;
  141. }
  142. int check_type(void)
  143. {
  144. /*
  145. * pseudo code --- this is what it should check
  146. *
  147. * if (yytext == type_name)
  148. * return TYPE_NAME;
  149. *
  150. * return IDENTIFIER;
  151. */
  152. /*
  153. * it actually will only return IDENTIFIER
  154. */
  155. return IDENTIFIER;
  156. }