home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compcomp / cgrammer / scan.l < prev    next >
Encoding:
Text File  |  1991-02-19  |  6.4 KB  |  264 lines

  1. D            [0-9]
  2. O            [0-7]
  3. L            [a-zA-Z_]
  4. H            [a-fA-F0-9]
  5. E            [Ee][+-]?{D}+
  6. FS            (f|F|l|L)
  7. IS            (u|U|l|L)*
  8.  
  9. %{
  10. #include <stdio.h>
  11. #include <sys/param.h>
  12. #include "y.tab.h"
  13.  
  14. int s_u_e_last = 0;
  15. void count ();
  16. void comment ();
  17. %}
  18.  
  19. %%
  20. "/*"            { comment(); }
  21.  
  22. "auto"            { count(0); return(AUTO); }
  23. "break"            { count(0); return(BREAK); }
  24. "case"            { count(0); return(CASE); }
  25. "char"            { count(0); return(CHAR); }
  26. "const"            { count(0); return(CONST); }
  27. "continue"        { count(0); return(CONTINUE); }
  28. "default"        { count(0); return(DEFAULT); }
  29. "do"            { count(0); return(DO); }
  30. "double"        { count(0); return(DOUBLE); }
  31. "else"            { count(0); return(ELSE); }
  32. "enum"            { count(0); s_u_e_last = 1; return(ENUM); }
  33. "extern"        { count(0); return(EXTERN); }
  34. "float"            { count(0); return(FLOAT); }
  35. "for"            { count(0); return(FOR); }
  36. "goto"            { count(0); return(GOTO); }
  37. "if"            { count(0); return(IF); }
  38. "int"            { count(0); return(INT); }
  39. "long"            { count(0); return(LONG); }
  40. "register"        { count(0); return(REGISTER); }
  41. "return"        { count(0); return(RETURN); }
  42. "short"            { count(0); return(SHORT); }
  43. "signed"        { count(0); return(SIGNED); }
  44. "sizeof"        { count(0); return(SIZEOF); }
  45. "static"        { count(0); return(STATIC); }
  46. "struct"        { count(0); s_u_e_last = 1; return(STRUCT); }
  47. "switch"        { count(0); return(SWITCH); }
  48. "typedef"        { count(0); return(TYPEDEF); }
  49. "union"            { count(0); s_u_e_last = 1; return(UNION); }
  50. "unsigned"        { count(0); return(UNSIGNED); }
  51. "void"            { count(0); return(VOID); }
  52. "volatile"        { count(0); return(VOLATILE); }
  53. "while"            { count(0); return(WHILE); }
  54.  
  55. {L}({L}|{D})*        { count(1); return(check_type(yytext)); }
  56.  
  57. 0[xX]{H}+{IS}?        { count(0); return(CONSTANT); }
  58. 0{O}*{IS}?        { count(0); return(CONSTANT); }
  59. {D}+{IS}?        { count(0); return(CONSTANT); }
  60. '(\\.|[^\\'])+'        { count(0); return(CONSTANT); }
  61.  
  62. {D}+{E}{FS}?        { count(0); return(CONSTANT); }
  63. {D}*"."{D}+({E})?{FS}?    { count(0); return(CONSTANT); }
  64. {D}+"."({E})?{FS}?    { count(0); return(CONSTANT); }
  65.  
  66. \"(\\.|[^\\"])*\"    { count(0); return(STRING_LITERAL); }
  67.  
  68. "..."            { count(0); return(ELIPSIS); }
  69. ">>="            { count(0); return(RIGHT_ASSIGN); }
  70. "<<="            { count(0); return(LEFT_ASSIGN); }
  71. "+="            { count(0); return(ADD_ASSIGN); }
  72. "-="            { count(0); return(SUB_ASSIGN); }
  73. "*="            { count(0); return(MUL_ASSIGN); }
  74. "/="            { count(0); return(DIV_ASSIGN); }
  75. "%="            { count(0); return(MOD_ASSIGN); }
  76. "&="            { count(0); return(AND_ASSIGN); }
  77. "^="            { count(0); return(XOR_ASSIGN); }
  78. "|="            { count(0); return(OR_ASSIGN); }
  79. ">>"            { count(0); return(RIGHT_OP); }
  80. "<<"            { count(0); return(LEFT_OP); }
  81. "++"            { count(0); return(INC_OP); }
  82. "--"            { count(0); return(DEC_OP); }
  83. "->"            { count(0); return(PTR_OP); }
  84. "&&"            { count(0); return(AND_OP); }
  85. "||"            { count(0); return(OR_OP); }
  86. "<="            { count(0); return(LE_OP); }
  87. ">="            { count(0); return(GE_OP); }
  88. "=="            { count(0); return(EQ_OP); }
  89. "!="            { count(0); return(NE_OP); }
  90. ";"            { count(0); return(';'); }
  91. "{"            { count(0); return('{'); }
  92. "}"            { count(0); return('}'); }
  93. ","            { count(0); return(','); }
  94. ":"            { count(0); return(':'); }
  95. "="            { count(0); return('='); }
  96. "("            { count(0); return('('); }
  97. ")"            { count(0); return(')'); }
  98. "["            { count(0); return('['); }
  99. "]"            { count(0); return(']'); }
  100. "."            { count(0); return('.'); }
  101. "&"            { count(0); return('&'); }
  102. "!"            { count(0); return('!'); }
  103. "~"            { count(0); return('~'); }
  104. "-"            { count(0); return('-'); }
  105. "+"            { count(0); return('+'); }
  106. "*"            { count(0); return('*'); }
  107. "/"            { count(0); return('/'); }
  108. "%"            { count(0); return('%'); }
  109. "<"            { count(0); return('<'); }
  110. ">"            { count(0); return('>'); }
  111. "^"            { count(0); return('^'); }
  112. "|"            { count(0); return('|'); }
  113. "?"            { count(0); return('?'); }
  114. "#"                     { count(0); cpp_symbol ();  }
  115. [ \t\v\n\f]        {  count (1); }
  116. .            { /* ignore bad characters */ }
  117.  
  118. %%
  119.  
  120. #define EATWHITE   do  {  c = input ();  }  while ( c != 0 && isspace (c))
  121.  
  122. char current_file_name[MAXPATHLEN];
  123. int line_number = 1;
  124. int column = 0, old_column = 0;
  125. int comment_counter = 0;
  126. int real_token = 0;
  127. char *comment_buffer;
  128. int comment_buffer_size = 0;
  129.  
  130. void
  131. comment()
  132. {
  133.   char c, c1;
  134.  
  135.   if (real_token)
  136.     comment_counter = 0;
  137.  
  138.   if (comment_buffer_size <= comment_counter)
  139.     {
  140.       if (comment_buffer_size == 0)
  141.         comment_buffer_size = 1024;
  142.       else
  143.        comment_buffer_size *= 2;
  144.       
  145.       grow_buffer (&comment_buffer, comment_buffer_size);
  146.     }
  147.  
  148.   do 
  149.     {
  150.       while ((c = input()) != '*' && c != 0)
  151.     {
  152.       comment_buffer[comment_counter++] = c;
  153.       if (comment_buffer_size == comment_counter)
  154.         {
  155.           comment_buffer_size *= 2;
  156.           grow_buffer (&comment_buffer, comment_buffer_size + 2);
  157.         }
  158.     }
  159.  
  160.       if (c != 0 && (c1 = input()) != '/' && c1 != 0)
  161.     comment_buffer[comment_counter++] = c;
  162.     }
  163.   while (c != 0 && c1 != 0 && c1 != '/');
  164.  
  165.   comment_buffer[comment_counter++] = '\t';
  166. }
  167.  
  168.  
  169. void 
  170. count(k)
  171.   int k;
  172. {
  173.   int i;
  174.  
  175.   if (k == 0)
  176.     {
  177.       s_u_e_last = 0;
  178.       real_token = 1;
  179.     }
  180.   old_column = column;
  181.   for (i = 0; yytext[i] != '\0'; i++)
  182.     if (yytext[i] == '\n')
  183.       {
  184.     column = 0;
  185.     ++line_number;
  186.       }
  187.     else if (yytext[i] == '\t')
  188.       column += 8 - (column % 8);
  189.     else
  190.       ++column;
  191. }
  192.  
  193. int check_type(pc)
  194. char *pc;
  195. {
  196.   int i;
  197.   extern int number_user_defined_types;
  198.   extern char **user_defined_types;
  199.  
  200.   if (s_u_e_last)
  201.     { 
  202.       s_u_e_last = 0;
  203.       return IDENTIFIER;
  204.     }
  205.  
  206.   for (i = 0; i < number_user_defined_types; i++)
  207.     if (0 == strcmp (pc, user_defined_types[i]))
  208.     { /* printf ("\tRETURN TYPENAME\t");*/
  209.       return TYPE_NAME;  }
  210.  
  211.   return IDENTIFIER;
  212. }
  213.  
  214. cpp_symbol ()
  215. {
  216.   char c, oldc;
  217.   int i;
  218.   char digit[32];
  219.  
  220.  
  221.   /*  There are a number of formats... */
  222.   /*  Here we just handle "# line_no filename"  */
  223.   EATWHITE;
  224.   i = 0;
  225.   do 
  226.     {
  227.       digit[i++] = c;
  228.       c = input ();
  229.     }
  230.   while ( isdigit (c));
  231.   digit[i] = 0;
  232.   line_number = atoi (digit);
  233.  
  234.   EATWHITE;
  235.   if ('"' == c)
  236.     c = input ();
  237.   i = 0;
  238.   do
  239.     {
  240.       current_file_name[i++] = c;
  241.       c = input ();
  242.     } while (c != '"' && !isspace (c) && c != 0);
  243.   current_file_name[i] = NULL;
  244.  
  245.   do {
  246.     while ((c = input ()) != '\n' && c != 0)
  247.       oldc = c;
  248.     } 
  249.   while (oldc == '\\');
  250.  
  251. /*  ++line_number; */
  252.   return;
  253.   
  254. }
  255.  
  256. yywrap()
  257. {
  258.   return(1);
  259. }
  260.  
  261.  
  262.  
  263.  
  264.