12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- %{
- #include <stdio.h>
- #include "yacc.tab.h"
- #include "symtable.h"
- extern int yylex();
- int linenum = 1;
- %}
- symbol [a-zA-Z][a-zA-Z0-9_]*
- number [0-9]+
- %option noyywrap
- %%
- /* ignore comments */
- #.* ;
- /* keywords */
- int { return DD_KEYWORD_INT; }
- float { return DD_KEYWORD_FLOAT; }
- struct { return DD_KEYWORD_STRUCT; }
- override { return DD_KEYWORD_OVERRIDE; }
- if { return DD_KEYWORD_IF; }
- void { return DD_KEYWORD_VOID; }
- /* operators */
- \=\= { return DD_OPERATOR_EQ; }
- \<\= { return DD_OPERATOR_LE; }
- \>\= { return DD_OPERATOR_GE; }
- /* constants and variables */
- {symbol} {
- yylval = symtable_insert(yytext, DD_CONSTANT_SYMBOL);
- if (yylval == -1) {
- fprintf(stderr, "lex: unable to insert symbol to symbol table: %s\n", yytext);
- exit(-1);
- }
- return DD_CONSTANT_SYMBOL;
- }
- \".*\" {
- yylval = symtable_insert(yytext, DD_CONSTANT_STRING);
- if (yylval == -1) {
- fprintf(stderr, "lex: unable to insert string symbol to symbol table: %s\n", yytext);
- exit(-1);
- }
- return DD_CONSTANT_STRING;
- }
- {number} { yylval = atoi(yytext); return DD_CONSTANT_NUMBER; }
- /* pass-through text */
- [-/*;=+():\.><,] return yytext[0];
- [{] { symtable_push(); return yytext[0]; }
- [}] { symtable_pop (); return yytext[0]; }
- /* ignore whitespace */
- [ \t] ;
- /* count lines */
- \n linenum++;
- %%
|