-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_c.l
41 lines (34 loc) · 1.06 KB
/
mini_c.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
%option noyywrap
%{
#include <cstdio>
#include <cstring>
#include "model.hpp"
#include "mini_c.tab.h"
%}
%%
[ \r\n\t] ; // ignore all whitespace
"int" {return T_INT;}
"if" {return T_IF;}
"else" {return T_ELSE;}
"+" {return '+';}
"-" {return '-';}
"*" {return '*';}
"/" {return '/';}
"(" {return '(';}
")" {return ')';}
"{" {return '{';}
"}" {return '}';}
";" {return ';';}
"=" {return '=';}
"<" {return '<';}
"<=" {return T_LTE;}
"!=" {return T_NE;}
"==" {return T_EQ;}
">=" {return T_GTE;}
">" {return '>';}
"||" {return T_OR;}
"&&" {return T_AND;}
[a-zA-Z][0-9a-zA-Z]* {yylval.str_val = strndup(yytext, yyleng); return T_ID; }
[0-9]+ {yylval.int_val = atoi(yytext); return T_NUMBER;}
. {printf("unrecognized token '%s'\n", yytext); exit(1);}
%%