home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume1 / ansi.c / scan.l < prev   
Encoding:
Text File  |  1986-11-30  |  4.2 KB  |  175 lines

  1. D            [0-9]
  2. L            [a-zA-Z_]
  3. H            [a-fA-F0-9]
  4. E            [Ee][+-]?{D}+
  5. LS            (l|L)
  6. US            (u|U)
  7.  
  8. %{
  9. #include <stdio.h>
  10. #include "y.tab.h"
  11.  
  12. void count();
  13. %}
  14.  
  15. %%
  16. "/*"            { comment(); }
  17.  
  18. "auto"            { count(); return(AUTO); }
  19. "break"            { count(); return(BREAK); }
  20. "case"            { count(); return(CASE); }
  21. "char"            { count(); return(CHAR); }
  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. "int"            { count(); return(INT); }
  35. "long"            { count(); return(LONG); }
  36. "register"        { count(); return(REGISTER); }
  37. "return"        { count(); return(RETURN); }
  38. "short"            { count(); return(SHORT); }
  39. "signed"        { count(); return(SIGNED); }
  40. "sizeof"        { count(); return(SIZEOF); }
  41. "static"        { count(); return(STATIC); }
  42. "struct"        { count(); return(STRUCT); }
  43. "switch"        { count(); return(SWITCH); }
  44. "typedef"        { count(); return(TYPEDEF); }
  45. "union"            { count(); return(UNION); }
  46. "unsigned"        { count(); return(UNSIGNED); }
  47. "void"            { count(); return(VOID); }
  48. "volatile"        { count(); return(VOLATILE); }
  49. "while"            { count(); return(WHILE); }
  50.  
  51. {L}({L}|{D})*        { count(); return(check_type()); }
  52.  
  53. 0[xX]{H}+{LS}?{US}?    { count(); return(CONSTANT); }
  54. 0[xX]{H}+{US}?{LS}?    { count(); return(CONSTANT); }
  55. 0{D}+{LS}?{US}?        { count(); return(CONSTANT); }
  56. 0{D}+{US}?{LS}?        { count(); return(CONSTANT); }
  57. {D}+{LS}?{US}?        { count(); return(CONSTANT); }
  58. {D}+{US}?{LS}?        { count(); return(CONSTANT); }
  59. '(\\.|[^\\'])+'        { count(); return(CONSTANT); }
  60.  
  61. {D}+{E}{LS}?        { count(); return(CONSTANT); }
  62. {D}*"."{D}+({E})?{LS}?    { count(); return(CONSTANT); }
  63. {D}+"."{D}*({E})?{LS}?    { count(); return(CONSTANT); }
  64.  
  65. \"(\\.|[^\\"])*\"    { count(); return(STRING_LITERAL); }
  66.  
  67. ">>="            { count(); return(RIGHT_ASSIGN); }
  68. "<<="            { count(); return(LEFT_ASSIGN); }
  69. "+="            { count(); return(ADD_ASSIGN); }
  70. "-="            { count(); return(SUB_ASSIGN); }
  71. "*="            { count(); return(MUL_ASSIGN); }
  72. "/="            { count(); return(DIV_ASSIGN); }
  73. "%="            { count(); return(MOD_ASSIGN); }
  74. "&="            { count(); return(AND_ASSIGN); }
  75. "^="            { count(); return(XOR_ASSIGN); }
  76. "|="            { count(); return(OR_ASSIGN); }
  77. ">>"            { count(); return(RIGHT_OP); }
  78. "<<"            { count(); return(LEFT_OP); }
  79. "++"            { count(); return(INC_OP); }
  80. "--"            { count(); return(DEC_OP); }
  81. "->"            { count(); return(PTR_OP); }
  82. "&&"            { count(); return(AND_OP); }
  83. "||"            { count(); return(OR_OP); }
  84. "<="            { count(); return(LE_OP); }
  85. ">="            { count(); return(GE_OP); }
  86. "=="            { count(); return(EQ_OP); }
  87. "!="            { count(); return(NE_OP); }
  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. "?"            { count(); return('?'); }
  112.  
  113. [ \t\v\n\f]        { count(); }
  114. .            { /* ignore bad characters */ }
  115.  
  116. %%
  117.  
  118. yywrap()
  119. {
  120.     return(1);
  121. }
  122.  
  123. comment()
  124. {
  125.     char c, c1;
  126.  
  127. loop:
  128.     while ((c = input()) != '*' && c != 0)
  129.         putchar(c);
  130.  
  131.     if ((c1 = input()) != '/' && c != 0)
  132.     {
  133.         unput(c1);
  134.         goto loop;
  135.     }
  136.  
  137.     if (c != 0)
  138.         putchar(c1);
  139. }
  140.  
  141. int column = 0;
  142.  
  143. void count()
  144. {
  145.     int i;
  146.  
  147.     for (i = 0; yytext[i] != '\0'; i++)
  148.         if (yytext[i] == '\n')
  149.             column = 0;
  150.         else if (yytext[i] == '\t')
  151.             column += 8 - (column % 8);
  152.         else
  153.             column++;
  154.  
  155.     ECHO;
  156. }
  157.  
  158. int check_type()
  159. {
  160. /*
  161. * pseudo code --- this is what it should check
  162. *
  163. *    if (yytext == type_name)
  164. *        return(TYPE_NAME);
  165. *
  166. *    return(IDENTIFIER);
  167. */
  168.  
  169. /*
  170. *    it actually will only return IDENTIFIER
  171. */
  172.  
  173.     return(IDENTIFIER);
  174. }
  175.