-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.lex
72 lines (58 loc) · 1.3 KB
/
parser.lex
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
%{
#include <stdlib.h>
#include <string.h>
#include "parser.tab.h"
int line=1;
%}
DIGIT [0-9]
ID [a-z][a-z0-9]*
%%
"$-"{DIGIT}+ {
yylval.i=-atoi( yytext+2 );
return TOKIMM;
}
"$"{DIGIT}+ {
yylval.i=atoi( yytext+1 );
return TOKIMM;
}
{DIGIT}+ {
yylval.i=atoi( yytext );
return TOKNUMBER ;
}
"nop" { return TOKNOP;}
"mov" { return TOKMOV;}
"sw" { return TOKSW;}
"lw" { return TOKLW;}
"push" { return TOKPUSH;}
"pop" { return TOKPOP;}
"print" { return TOKPRINT;}
"read" { return TOKREAD;}
"add" { return TOKADD;}
"sub" { return TOKSUB;}
"div" { return TOKDIV;}
"mul" { return TOKMUL;}
"cmp" { return TOKCMP;}
"jmp" { return TOKJMP;}
"jmpe" { return TOKJMPE;}
"jmpl" { return TOKJMPL;}
"hlt" { return TOKHLT;}
"call" { return TOKCALL;}
"ret" { return TOKRET;}
"and" { return TOKAND;}
"%"{ID} {yylval.strval=strdup(yytext); return TOKREG;}
{ID}":" { yytext[strlen(yytext)-1]=0;yylval.strval=strdup(yytext);return TOKLABEL;}
{ID} {yylval.strval=strdup(yytext);return TOKID;}
"(" {return TOKOPAREN;}
")" {return TOKCPAREN;}
\n {line++;return TOKCR;}
[ \t]+ /* eat up whitespace */
"," {return TOKCOMA;}
. printf( "Unrecognized character: %s\n", yytext );
%%
int yyerror(const char *msg) {
printf("Line %d: %s\n",line,msg);
exit(-1);
}
int yywrap() {
return 1;
}