-
Notifications
You must be signed in to change notification settings - Fork 0
/
minic.l
47 lines (38 loc) · 1.51 KB
/
minic.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
42
43
44
45
46
47
%option noyywrap yylineno
%{
#include <stdio.h>
#include <string.h>
#include "minic.tab.h"
#include "defs.h"
%}
%%
[ \t\n]+ { /* skip */ }
"int" { yylval.i = INT; return _TYPE; }
"unsigned" { yylval.i = UINT; return _TYPE; }
"if" { return _IF; }
"else" { return _ELSE; }
"return" { return _RETURN; }
"(" { return _LPAREN; }
")" { return _RPAREN; }
"{" { return _LBRACKET; }
"}" { return _RBRACKET; }
";" { return _SEMICOLON; }
"=" { return _ASSIGN; }
"+" { yylval.i = ADD; return _AROP; }
"-" { yylval.i = SUB; return _AROP; }
"<" { yylval.i = LT; return _RELOP; }
">" { yylval.i = GT; return _RELOP; }
"<=" { yylval.i = LE; return _RELOP; }
">=" { yylval.i = GE; return _RELOP; }
"==" { yylval.i = EQ; return _RELOP; }
"!=" { yylval.i = NE; return _RELOP; }
[a-zA-Z][a-zA-Z0-9]* { yylval.s = strdup(yytext);
return _ID; }
[+-]?[0-9]{1,10} { yylval.s = strdup(yytext);
return _INT_NUMBER;}
[0-9]{1,10}[uU] { yylval.s = strdup(yytext);
yylval.s[yyleng-1] = 0;
return _UINT_NUMBER;}
\/\/.* { /* skip */ }
. { printf("line %d: LEXICAL ERROR on char %c\n", yylineno, *yytext);}
%%