forked from eXpOSNitc/spl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spl.l
269 lines (221 loc) · 3.85 KB
/
spl.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
%{
#include "y.tab.h"
#include "data.h"
int linecount=1;
struct define
{
char *name;
int value;
struct define *next;
};
struct tree
{
char nodetype; /* +,-,*,/,%,=,<,>
?-if statement, I-ireturn, L-load
S-store, P-strcmp, Y-strcpy, w-while,
R-register //value=0-15, 20-BP, 21-SP, 22-IP, 23-PTBR, 24-PTLR, 25-EFR
e-double equals, l-lessthan or equals
g-greaterthan or equals !-not equal
a-AND o-OR x-NOT
c-number, i-identifier,
n-nonterminal
b-break t-continue m-addresing expr
h-halt C-checkpoint I-ireturn
1-read 2-print s-STRING
3-inline
*/
char *name;
int value;
struct define *entry;
struct tree *ptr1,*ptr2,*ptr3;
};
void create_node(char, char*, int); //nodetype,name,value
%}
%%
"alias" {
return ALIAS;
}
"define" {
return DEFINE;
}
"do" {
return DO;
}
"else" {
return ELSE;
}
"endif" {
return ENDIF;
}
"endwhile" {
return ENDWHILE;
}
"if" {
create_node('?',NULL,-1);
return IF;
}
"ireturn" {
create_node('I',NULL,-1);
return IRETURN;
}
"load" {
create_node('L',NULL,-1);
return LOAD;
}
"store" {
create_node('S',NULL,-1);
return STORE;
}
"then" {
return THEN;
}
"while" {
create_node('w',NULL,-1);
return WHILE;
}
"break" {
create_node('b',NULL,-1);
return BREAK;
}
"continue" {
create_node('t',NULL,-1);
return CONTINUE;
}
"halt" {
create_node('h',NULL,-1);
return HALT;
}
"breakpoint" {
create_node('C',NULL,-1);
return CHKPT;
}
"read" {
create_node('1',NULL,-1);
return READ;
}
"print" {
create_node('2',NULL,-1);
return PRINT;
}
"inline" {
create_node('3',NULL,-1);
return INLINE;
}
[R]([0-7]) {
create_node('R',NULL,atoi(yytext+1) + R0);
return REG;
}
[S]([0-9]) {
create_node('R',NULL,atoi(yytext+1) + S0);
return REG;
}
(S1)([0-5]) {
create_node('R',NULL,atoi(yytext+1) + S0);
return REG;
}
"BP" {
create_node('R',NULL,BP_REG);
return REG;
}
"SP" {
create_node('R',NULL,SP_REG);
return REG;
}
"IP" {
create_node('R',NULL,IP_REG);
return REG;
}
"PTBR" {
create_node('R',NULL,PTBR_REG);
return REG;
}
"PTLR" {
create_node('R',NULL,PTLR_REG);
return REG;
}
"EFR" {
create_node('R',NULL,EFR_REG);
return REG;
}
(==) {
create_node('e',NULL,-1);
return RELOP;
}
(<=) {
create_node('l',NULL,-1);
return RELOP;
}
(>=) {
create_node('g',NULL,-1);
return RELOP;
}
"!=" {
create_node('!',NULL,-1);
return RELOP;
}
"&&" {
create_node('a',NULL,-1);
return LOGOP;
}
"||" {
create_node('o',NULL,-1);
return LOGOP;
}
"!" {
create_node('x',NULL,-1);
return NOTOP;
}
[+-] {
create_node(*yytext,NULL,-1);
return ARITHOP1;
}
[*/%] {
create_node(*yytext,NULL,-1);
return ARITHOP2;
}
[<>] {
create_node(*yytext,NULL,-1);
return RELOP;
}
"=" {
create_node('=',NULL,-1);
return ASSIGNOP;
}
[0-9]+ {
create_node('c',NULL,atoi(yytext));
return NUM;
}
[\"][^\"]*[\"] {
create_node('s',yytext,-1);
return STRING;
}
[a-zA-Z][a-zA-Z0-9_]* {
create_node('i',yytext,-1);
return ID;
}
[(),;\[\]\{\}&] {
return *yytext;
}
[/][/].*\n {
linecount++;
}
[\n] {
linecount++;
}
[ \t] {
}
. {
}
%%
void create_node(char nodetype, char *name, int value)
{
yylval.n=malloc(sizeof(struct tree));
yylval.n->nodetype=nodetype;
if(name!=NULL)
yylval.n->name=strdup(name);
yylval.n->value=value;
yylval.n->entry=NULL;
yylval.n->ptr1=NULL;
yylval.n->ptr2=NULL;
yylval.n->ptr3=NULL;
}