-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexical_analysis.h
293 lines (268 loc) · 7.94 KB
/
lexical_analysis.h
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef lexical_analysis_h
#define lexical_analysis_h
#include "global_variable.h"
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
// "string int real else if return while "
// matching the second line of enum (for initializing)
// "in out void main"
// matching the third line
// token list.
extern enum {
// integer constant, floating point constant, function, system operation, identifier
Inum = 128, Fnum, Cstr, Fun, Var, Sys, Id,
String, Int, Real, Else, If, Return, While,
In, Out, Void, Main,
// the former are pre-defined identifier
Assign, Cond, Bor, Band, Or, Xor, And, Eq, Ne, Lt, Gt, Le, Ge,
Shl, Shr, Add, Sub, Mul, Div, Mod, Inc, Dec, Brak
// these are operators
};
struct identifier {
int token;
int hash; // the hash value of token
string name; // the name of identifier
// later processing
int type; // int, real, cstring
unsigned int addr; // address
int class_;
int id_size;
} ; // current parsed idntifier
extern identifier *id_list = NULL, *cur_id = NULL, **ed;
void get_next_token() {
char *last_pos;
while ((token = *src)) {
if (*src == '\n' || *src == ' ') {
if (*src == '\n')
++line;
++src;
continue;
}
if (isalpha(token) || token == '_') { // id
last_pos = src; // ??
int hash = token;
string new_name;
while (isalpha(*src) || isdigit(*src) || *src == '_') {
new_name.push_back(*src);
hash = hash * 147 + *src;
src++;
}
// writing in the empty space closest from begin.
//for (cur_id = id_list; cur_id->token > 0; cur_id++) { // tokens begin with 128, 0 -> unused
for (cur_id = ed[depth]; cur_id >= id_list; --cur_id) {
if (cur_id->hash == hash && new_name == cur_id->name) {
token = cur_id->token;
return;
}
}
cur_id = ++ed[depth];
cur_id->name = new_name;
cur_id->hash = hash;
token = cur_id->token = Id; // is a new identifier
return ;
}
// dealing with integer value and floating-point value
else if (isdigit(token)) { // numbers
char tmp[100];
int size = 0;
bool is_float = false;
while (isdigit(*src) || *src == '.') {
if (*src == '.') {
is_float = true;
}
tmp[size++] = *src++;
}
// token_val unused?
if (is_float) {
token = Fnum;
ftoken_val = atof(tmp);
}
else {
token = Inum;
token_val = atoi(tmp);
}
return ;
}
// strings
else if (token == '"') {
last_pos = data_;
++src;
while (*src != 0 && *src != '"') {
token_val = *src++;
// supporting only '\n' and '\t' for now
if (token_val == '\\') {
token_val = *src++;
if (token_val == 'n') {
token_val = '\n';
}
if (token_val == 't') {
token_val = '\t';
}
}
*data_++ = token_val;
}
src++;
token = Cstr;
token_val = (long)last_pos; // return the address of string
*data_ = '\0';
return ;
}
else if (token == '/') { // comment?
if (*(src + 1) == '/') {
while (*src != 0 && *src != '\n') {
++src;
}
continue;
}
else {
token = Div;
}
}
else if (token == '=') {
if (*(src + 1) == '=') { // ==
++src;
token = Eq;
}
else {
token = Assign;
}
}
else if (token == '+') {
if (*(src + 1) == '+') {
++src;
token = Inc;
}
else {
token = Add;
}
}
else if (token == '-') {
if (*(src + 1) == '-') { // "--"
++src;
token = Dec;
}
else { // "-"
token = Sub;
}
}
else if (token == '!') {
if (*(src + 1) == '=') {
++src;
token = Ne;
}
// not adding "!id" yet
}
else if (token == '<') {
if (*(src + 1) == '=') {
++src;
token = Le;
}
else if (*(src + 1) == '<') { // >>
++src;
token = Shl;
}
else {
token = Lt;
}
}
else if (token == '>') {
if (*(src + 1) == '=') {
++src;
token = Ge;
}
else if (*(src + 1) == '>') { // >>
++src;
token = Shr;
}
else {
token = Gt;
}
}
else if (token == '&') {
if (*(src + 1) == '&') {
++src;
token = And;
}
else {
token = Band;
}
}
else if (token == '|') {
if (*(src + 1) == '|') {
++src;
token = Or;
}
else {
token = Bor;
}
}
else if (token == '^') {
token = Xor;
}
else if (token == '%') {
token = Mod;
}
else if (token == '*') {
token = Mul;
}
else if (token == '?') { // ????
token = Cond;
}
else if (token == '[') { // ????
token = Brak;
}
// ???
else if (token == '~' || token == ';' || token == '{' || token == '}' || token == '(' || token == ')' || token == ']' || token == ',' || token == ':') {
// directly return the character as token;
}
++src;
return ;
}
return ;
}
void id_list_inintialize() {
// supporting only 1000 different identifer.
if ((id_list = (identifier *)malloc(sizeof(identifier) * 1000)) == NULL) {
printf("failed to allocate memory for identifier list.\n");
return ;
}
memset(id_list, 0, sizeof(identifier) * 1000);
src = (char *)"string int real else if return while in out void main";
for (int tok = String; tok <= Main; tok++) {
get_next_token();
cur_id->token = tok;
} // loading predefined identifier into id_list.
}
// testing part:
// WARNING: will cause side effect of src and id_list
void test() {
static const string mat[] = {
"Inum", "Fnum", "Cstr", "Fun", "Sys", "Id",
"String", "Int", "Real", "Else", "If", "Return", "While",
"In", "Out", "Void", "Main",
// the former are pre-defined identifier
"Assign", "Cond", "Bor", "Band", "Or", "Xor", "And", "Eq", "Ne", "Lt", "Gt", "Le", "Ge",
"Shl", "Shr", "Add", "Sub", "Mul", "Div", "Mod", "Inc", "Dec", "Brak"
};
while (*src != 0) {
get_next_token();
if (token == 0)
break;
if (token >= 128) {
if (Id <= token && token <= While)
printf("<%s>\n", cur_id->name.data());
else {
printf("<%s, %ld, %lf>\n", mat[token-128].data(), token_val, ftoken_val);
if (token == Cstr) {
cout << (char *)token_val << endl;
}
}
}
else
printf("<%c>\n", token);
ftoken_val = token = token_val = 0; // delete pre
}
}
#endif /* lexical_analysis_hpp */