Year 2 compilers coureswork

C.flex 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. D [0-9]
  2. L [a-zA-Z_]
  3. H [a-fA-F0-9]
  4. E [Ee][+-]?{D}+
  5. FS (f|F|l|L)
  6. IS (u|U|l|L)*
  7. %{
  8. #include <stdio.h>
  9. #include "C.tab.h"
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include "token.h"
  13. TOKEN* make_string(char*);
  14. extern TOKEN* lookup_token(char*);
  15. TOKEN* make_int(char*);
  16. TOKEN* lasttok;
  17. void count(void);
  18. void comment(void);
  19. %}
  20. %%
  21. "/*" { comment(); }
  22. "auto" { count(); return(AUTO); }
  23. "break" { count(); return(BREAK); }
  24. "continue" { count(); return(CONTINUE); }
  25. "else" { count(); return(ELSE); }
  26. "extern" { count(); return(EXTERN); }
  27. "if" { count(); return(IF); }
  28. "int" { count(); return(INT); }
  29. "function" { count(); return(FUNCTION); }
  30. "return" { count(); return(RETURN); }
  31. "void" { count(); return(VOID); }
  32. "while" { count(); return(WHILE); }
  33. {L}({L}|{D})* { count(); lasttok = lookup_token(yytext);
  34. return(IDENTIFIER); }
  35. {D}+{IS}? { count(); lasttok = make_int(yytext);return(CONSTANT); }
  36. L?'(\\.|[^\\'])+' { count(); lasttok = make_int(yytext);return(CONSTANT); }
  37. L?\"(\\.|[^\\"])*\" { count(); lasttok = make_string(yytext);
  38. return(STRING_LITERAL); }
  39. "<=" { count(); return(LE_OP); }
  40. ">=" { count(); return(GE_OP); }
  41. "==" { count(); return(EQ_OP); }
  42. "!=" { count(); return(NE_OP); }
  43. ";" { count(); return(';'); }
  44. "{" { count(); return('{'); }
  45. "}" { count(); return('}'); }
  46. "," { count(); return(','); }
  47. ":" { count(); return(':'); }
  48. "=" { count(); return('='); }
  49. "(" { count(); return('('); }
  50. ")" { count(); return(')'); }
  51. "!" { count(); return('!'); }
  52. "-" { count(); return('-'); }
  53. "+" { count(); return('+'); }
  54. "*" { count(); return('*'); }
  55. "/" { count(); return('/'); }
  56. "%" { count(); return('%'); }
  57. "<" { count(); return('<'); }
  58. ">" { count(); return('>'); }
  59. [ \t\v\n\f] { count(); }
  60. . { /* ignore bad characters */ }
  61. %%
  62. int yywrap(void)
  63. {
  64. return(1);
  65. }
  66. void comment(void)
  67. {
  68. char c, c1;
  69. loop:
  70. while ((c = input()) != '*' && c != 0)
  71. putchar(c);
  72. if ((c1 = input()) != '/' && c != 0)
  73. {
  74. unput(c1);
  75. goto loop;
  76. }
  77. if (c != 0)
  78. putchar(c1);
  79. }
  80. int column = 0;
  81. void count()
  82. {
  83. int i;
  84. for (i = 0; yytext[i] != '\0'; i++)
  85. if (yytext[i] == '\n')
  86. column = 0;
  87. else if (yytext[i] == '\t')
  88. column += 8 - (column % 8);
  89. else
  90. column++;
  91. ECHO;
  92. }
  93. TOKEN *new_token(int type)
  94. {
  95. TOKEN *ans = (TOKEN*)malloc(sizeof(TOKEN));
  96. ans->type = type;
  97. return ans;
  98. }
  99. TOKEN *make_string(char *s)
  100. {
  101. TOKEN *ans = new_token(STRING_LITERAL);
  102. int len = strlen(s);
  103. ans->lexeme = (char*)calloc(1, len-1);
  104. strncpy(ans->lexeme, s+1, len-2);
  105. return ans;
  106. }
  107. TOKEN *make_int(char *s)
  108. {
  109. int n = *s!='\'' ? atoi(s) : *(s+1);
  110. TOKEN *ans = new_token(CONSTANT);
  111. ans->value = n;
  112. return ans;
  113. }