forked from vivekzhere/spsil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspsil.l
203 lines (164 loc) · 2.76 KB
/
spsil.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
%{
#include "y.tab.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, w-while, p-write
R-register //value=0-15,20-SP,21-BP,22-IP
e-double equals, l-lessthan or equals
g-greaterthan or equals !-not equal
a-AND o-OR x-NOT
c-number, i-identifier, s-string
n-nonterminal
*/
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;
}
[R]([1][0-5]|[0-9]) {
create_node('R',NULL,atoi(yytext+1));
return REG;
}
"BP" {
create_node('R',NULL,20);
return REG;
}
"SP" {
create_node('R',NULL,21);
return REG;
}
"IP" {
create_node('R',NULL,22);
return REG;
}
[-]?[0-9]+ {
create_node('c',NULL,atoi(yytext));
return NUM;
}
(=) {
create_node('=',NULL,-1);
return ASSIGNOP;
}
[-+] {
create_node(*yytext,NULL,-1);
return ARITHOP1;
}
[*/%] {
create_node(*yytext,NULL,-1);
return ARITHOP2;
}
[<>] {
create_node(*yytext,NULL,-1);
return RELOP;
}
(==) {
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;
}
[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;
}