Acse.lex 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /***************************************************************************
  2. Scanner
  3. ***************************************************************************/
  4. %option noyywrap
  5. %{
  6. /*
  7. * Andrea Di Biagio
  8. * Politecnico di Milano, 2007
  9. *
  10. * Axe.lex
  11. * Formal Languages & Compilers Machine, 2007/2008
  12. *
  13. */
  14. #include <string.h>
  15. #include "axe_struct.h"
  16. #include "collections.h"
  17. #include "Acse.tab.h"
  18. #include "axe_constants.h"
  19. /* Variables declared in the lexer for error tracking */
  20. extern int line_num;
  21. extern int num_error;
  22. /* extern declaration of function yyerror */
  23. extern int yyerror(const char* errmsg);
  24. %}
  25. /*=========================================================================
  26. TOKEN DEFINITIONS
  27. =========================================================================*/
  28. DIGIT [0-9]
  29. ID [a-zA-Z_][a-zA-Z0-9_]*
  30. /*=========================================================================
  31. TOKENS
  32. =========================================================================*/
  33. %option noyywrap
  34. %x comment
  35. %%
  36. "\r\n" { ++line_num; }
  37. "\n" { ++line_num; }
  38. [ \t\f\v]+ { /* Ignore whitespace. */ }
  39. "//"[^\n]* { ++line_num; /* ignore comment lines */ }
  40. "/*" BEGIN(comment);
  41. <comment>[^*\n]*
  42. <comment>[^*\n]*\n { ++line_num; }
  43. <comment>"*"+[^*/\n]*
  44. <comment>"*"+[^*/\n]*\n { ++line_num; }
  45. <comment>"*"+"/" BEGIN(INITIAL);
  46. "{" { return LBRACE; }
  47. "}" { return RBRACE; }
  48. "[" { return LSQUARE; }
  49. "]" { return RSQUARE; }
  50. "(" { return LPAR; }
  51. ")" { return RPAR; }
  52. ";" { return SEMI; }
  53. ":" { return COLON; }
  54. "+" { return PLUS; }
  55. "-" { return MINUS; }
  56. "*" { return MUL_OP; }
  57. "/" { return DIV_OP; }
  58. "%" { return MOD_OP; }
  59. "&" { return AND_OP; }
  60. "|" { return OR_OP; }
  61. "!" { return NOT_OP; }
  62. "=" { return ASSIGN; }
  63. "<" { return LT; }
  64. ">" { return GT; }
  65. "<<" { return SHL_OP; }
  66. ">>" { return SHR_OP; }
  67. "==" { return EQ; }
  68. "!=" { return NOTEQ; }
  69. "<=" { return LTEQ; }
  70. ">=" { return GTEQ; }
  71. "&&" { return ANDAND; }
  72. "||" { return OROR; }
  73. "," { return COMMA; }
  74. "do" { return DO; }
  75. "else" { return ELSE; }
  76. "for" { return FOR; }
  77. "if" { return IF; }
  78. "int" { yylval.intval = INTEGER_TYPE; return TYPE; }
  79. "while" { return WHILE; }
  80. "return" { return RETURN; }
  81. "read" { return READ; }
  82. "write" { return WRITE; }
  83. {ID} { yylval.svalue=strdup(yytext); return IDENTIFIER; }
  84. {DIGIT}+ { yylval.intval = atoi( yytext );
  85. return(NUMBER); }
  86. . { yyerror("Error: unexpected token");
  87. num_error++;
  88. return (-1); /* invalid token */
  89. }