Year 2 compilers coureswork

C.flex 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #pragma GCC diagnostic ignored "-Wall"
  9. #pragma GCC diagnostic ignored "-Wextra"
  10. #pragma GCC diagnostic ignored "-Wconversion"
  11. #pragma GCC diagnostic ignored "-Wpedantic"
  12. #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
  13. #include <stdio.h>
  14. #include "C.tab.h"
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "token.h"
  18. TOKEN* make_string(char*);
  19. extern TOKEN* lookup_token(char*);
  20. TOKEN* make_int(char*);
  21. TOKEN* lasttok;
  22. void count(void);
  23. void comment(void);
  24. %}
  25. %%
  26. "/*" { comment(); }
  27. "auto" { count(); return(AUTO); }
  28. "break" { count(); return(BREAK); }
  29. "continue" { count(); return(CONTINUE); }
  30. "else" { count(); return(ELSE); }
  31. "extern" { count(); return(EXTERN); }
  32. "if" { count(); return(IF); }
  33. "int" { count(); return(INT); }
  34. "function" { count(); return(FUNCTION); }
  35. "return" { count(); return(RETURN); }
  36. "void" { count(); return(VOID); }
  37. "while" { count(); return(WHILE); }
  38. {L}({L}|{D})* { count(); lasttok = lookup_token(yytext);
  39. return(IDENTIFIER); }
  40. {D}+{IS}? { count(); lasttok = make_int(yytext);return(CONSTANT); }
  41. L?'(\\.|[^\\'])+' { count(); lasttok = make_int(yytext);return(CONSTANT); }
  42. L?\"(\\.|[^\\"])*\" { count(); lasttok = make_string(yytext);
  43. return(STRING_LITERAL); }
  44. "<=" { count(); return(LE_OP); }
  45. ">=" { count(); return(GE_OP); }
  46. "==" { count(); return(EQ_OP); }
  47. "!=" { count(); return(NE_OP); }
  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. "*" { count(); return('*'); }
  60. "/" { count(); return('/'); }
  61. "%" { count(); return('%'); }
  62. "<" { count(); return('<'); }
  63. ">" { count(); return('>'); }
  64. [ \t\v\n\f] { count(); }
  65. . { /* ignore bad characters */ }
  66. %%
  67. int yywrap(void)
  68. {
  69. return(1);
  70. }
  71. void comment(void)
  72. {
  73. char c, c1;
  74. loop:
  75. while ((c = input()) != '*' && c != 0)
  76. putchar(c);
  77. if ((c1 = input()) != '/' && c != 0)
  78. {
  79. unput(c1);
  80. goto loop;
  81. }
  82. if (c != 0)
  83. putchar(c1);
  84. }
  85. int column = 0;
  86. void count()
  87. {
  88. int i;
  89. for (i = 0; yytext[i] != '\0'; i++)
  90. if (yytext[i] == '\n')
  91. column = 0;
  92. else if (yytext[i] == '\t')
  93. column += 8 - (column % 8);
  94. else
  95. column++;
  96. ECHO;
  97. }
  98. TOKEN *new_token(int type)
  99. {
  100. TOKEN *ans = (TOKEN*)malloc(sizeof(TOKEN));
  101. ans->type = type;
  102. return ans;
  103. }
  104. TOKEN *make_string(char *s)
  105. {
  106. TOKEN *ans = new_token(STRING_LITERAL);
  107. int len = strlen(s);
  108. ans->lexeme = (char*)calloc(1, len-1);
  109. strncpy(ans->lexeme, s+1, len-2);
  110. return ans;
  111. }
  112. TOKEN *make_int(char *s)
  113. {
  114. int n = *s!='\'' ? atoi(s) : *(s+1);
  115. TOKEN *ans = new_token(CONSTANT);
  116. ans->value = n;
  117. asprintf(&ans->lexeme, "%d", n);
  118. return ans;
  119. }