lex.l 966 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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} { yylval = symtable_insert(yytext, DD_CONSTANT_SYMBOL); return DD_CONSTANT_SYMBOL; }
  27. \".*\" { yylval = symtable_insert(yytext, DD_CONSTANT_STRING); return DD_CONSTANT_STRING; }
  28. {number} { yylval = atoi(yytext); return DD_CONSTANT_NUMBER; }
  29. /* pass-through text */
  30. [-/*;=+{}():\.><] return yytext[0];
  31. /* ignore whitespace */
  32. [ \t] ;
  33. /* count lines */
  34. \n linenum++;
  35. %%