lex.l 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. %{
  2. #include <stdio.h>
  3. #include "yacc.tab.h"
  4. #include "symtable.h"
  5. extern int yylex();
  6. int linenum = 1;
  7. %}
  8. symbol [a-zA-Z][a-zA-Z0-9_]*
  9. number [0-9]+
  10. %option noyywrap
  11. %%
  12. /* ignore comments */
  13. #.* ;
  14. /* keywords */
  15. int { return DD_KEYWORD_INT; }
  16. float { return DD_KEYWORD_FLOAT; }
  17. struct { return DD_KEYWORD_STRUCT; }
  18. override { return DD_KEYWORD_OVERRIDE; }
  19. if { return DD_KEYWORD_IF; }
  20. void { return DD_KEYWORD_VOID; }
  21. /* operators */
  22. \=\= { return DD_OPERATOR_EQ; }
  23. \<\= { return DD_OPERATOR_LE; }
  24. \>\= { return DD_OPERATOR_GE; }
  25. /* constants and variables */
  26. {symbol} {
  27. yylval = symtable_insert(yytext, DD_CONSTANT_SYMBOL);
  28. if (yylval == -1) {
  29. fprintf(stderr, "lex: unable to insert symbol to symbol table: %s\n", yytext);
  30. exit(-1);
  31. }
  32. return DD_CONSTANT_SYMBOL;
  33. }
  34. \".*\" {
  35. yylval = symtable_insert(yytext, DD_CONSTANT_STRING);
  36. if (yylval == -1) {
  37. fprintf(stderr, "lex: unable to insert string symbol to symbol table: %s\n", yytext);
  38. exit(-1);
  39. }
  40. return DD_CONSTANT_STRING;
  41. }
  42. {number} { yylval = atoi(yytext); return DD_CONSTANT_NUMBER; }
  43. /* pass-through text */
  44. [-/*;=+():\.><,] return yytext[0];
  45. [{] { symtable_push(); return yytext[0]; }
  46. [}] { symtable_pop (); return yytext[0]; }
  47. /* ignore whitespace */
  48. [ \t] ;
  49. /* count lines */
  50. \n linenum++;
  51. %%