This repository has been archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.l
264 lines (242 loc) · 5.59 KB
/
scanner.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
%{
#ifdef WIN32
#define YY_NO_UNISTD_H
int isatty(int i) { return 0;}
#endif
#include "scanner.h"
#include "parser.h"
#define YY_DECL int yylex(void* yyval)
unsigned int flag = 0;
#define EOF_NUMBER 0
%}
%option PREFIX="alpha_yy"
%option noyywrap
%option yylineno
/* Flex macros */
id [a-zA-Z][a-zA-Z_0-9]*
string \"
multiline_comment "/*"
comment "//".*
delimiter [\r\n \t\v]*
number [0-9]+
float {number}\.{number}?
/* KEYWORDS */
IF "if"
ELSE "else"
WHILE "while"
FOR "for"
FUNCTION "function"
RETURN "return"
BREAK "break"
CONTINUE "continue"
AND "and"
NOT "not"
OR "or"
LOCAL "local"
TRUE "true"
FALSE "false"
NIL "nil"
/* PUNCTUATIONS */
CURL_O "\{"
CURL_C "\}"
BRAC_O "\["
BRAC_C "\]"
ANGL_O "\("
ANGL_C "\)"
SEMI "\;"
COMMA "\,"
COLON "\:"
DCOLON "\:\:"
DOT "\."
DOTDOT "\.\."
/* OPERATIONS */
ASSIGN "\="
PLUS "\+"
MINUS "\-"
MUL "\*"
DIV "\/"
PERC "\%"
EQUALS "\=\="
NEQUALS "\!\="
INCR "\+\+"
DECR "\-\-"
GREATER "\>"
LESS "\<"
GREATER_E "\>\="
LESS_E "\<\="
%%
{IF} {return IF;} //return IF;
{ELSE} {return ELSE;}
{WHILE} {return WHILE;}
{FOR} {return FOR;}
{FUNCTION} {return FUNCTION;}
{RETURN} {return RETURN;}
{BREAK} {return BREAK;}
{CONTINUE} {return CONTINUE;}
{AND} {return AND;}
{NOT} {return NOT;}
{OR} {return OR;}
{LOCAL} {return LOCAL;}
{TRUE} {return TRUE;}
{FALSE} {return FALSE;}
{NIL} {return NIL;}
{CURL_O} {return CURL_O;}
{CURL_C} {return CURL_C;}
{BRAC_O} {return BRAC_O;}
{BRAC_C} {return BRAC_C;}
{ANGL_O} {return ANGL_O;}
{ANGL_C} {return ANGL_C;}
{SEMI} {return SEMI;}
{COMMA} {return COMMA;}
{COLON} {return COLON;}
{DCOLON} {return DCOLON;}
{DOT} {return DOT;}
{DOTDOT} {return DOTDOT;}
{ASSIGN} {return ASSIGN;}
{PLUS} {return PLUS;}
{MINUS} {return MINUS;}
{MUL} {return MUL;}
{DIV} {return DIV;}
{PERC} {return PERC;}
{EQUALS} {return EQUALS;}
{NEQUALS} {return NEQUALS;}
{INCR} {return INCR;}
{DECR} {return DECR;}
{GREATER} {return GREATER;}
{LESS} {return LESS;}
{GREATER_E} {return GREATER_E;}
{LESS_E} {return LESS_E;}
{id} {
alpha_yylval.stringValue = strdup(yytext);
return ID;
}
{string} {
char* buffer = (char*)malloc(sizeof(char)*1026);
int filled = 0;
int reallocs = 1;
char c;
while ((c = input()) != '"') {
if (c == 0) {
fprintf(stderr, "EOF during string\n");
exit(EXIT_FAILURE);
}
if (filled == reallocs*1024) {
buffer = (char*)realloc(buffer, reallocs*1024 + 1026);
reallocs++;
}
if (c != '\\') {
buffer[filled++] = c;
continue;
}
c = input();
if (c == 0) {
fprintf(stderr, "EOF during escaping in string\n");
exit(EXIT_FAILURE);
} else if (c == '\\'){
buffer[filled] = '\\';
} else if (c == 'n') {
buffer[filled] = '\n';
} else if (c == 't') {
buffer[filled] = '\t';
} else if (c == '"') {
buffer[filled] = '\"';
} else {
fprintf(stderr, "Unknown escaped character in string, line %d\n", yylineno );
exit(EXIT_FAILURE);
}
filled++;
}
buffer[filled] = '\0';
alpha_yylval.stringValue = strdup(buffer);
free(buffer);
return STRING;
}
{multiline_comment} {
char c, *buffer, temp[128];
int balanced = 1, i ;
Stack *stack = Stack_init();
Stack *reverse = Stack_init();
Stack_Node *node;
comment_node *comment_n;
comment_n = (comment_node *) malloc(sizeof(comment_node));
comment_n->line_open = yylineno;
comment_n->line_close = -1;
comment_n->isBlock = 1;
Stack_append(stack, (void *)comment_n);
while (1) {
c = input();
if (c == EOF_NUMBER) {
break;
}
if (c == '/') {
c = input();
if (c != '*') {
unput(c);
continue;
}
balanced++;
comment_n = (comment_node *) malloc(sizeof(comment_node));
comment_n->line_open = yylineno;
comment_n->line_close = -1;
comment_n->isBlock = 1;
Stack_append(stack, (void *)comment_n);
} else if (c == '*') {
c = input();
if (c != '/') {
unput(c);
continue;
}
node = stack->top;
for (i=0; i<stack->size; i++) {
comment_n = (comment_node *)(node->content);
if (comment_n->isBlock == 1 && comment_n->line_close == -1){
comment_n->line_close = yylineno;
break;
}
node = node->next;
}
balanced--;
if (balanced == 0) break;
}
}
while (Stack_isEmpty(stack) == 0)Stack_append(reverse, Stack_pop(stack));
while (Stack_isEmpty(reverse) == 0) {
comment_n = Stack_pop(reverse);
if (comment_n->isBlock == 0) {
sprintf(temp , "%d" ,comment_n->line_open);
//new_token((Queue *) alpha_yylval, comment_n->line_open, temp, comment, LINECOMM);
continue;
}
if (comment_n->line_close == -1){
sprintf(temp , "%d-EOF" , comment_n->line_open);
flag=1;
} else {
sprintf(temp , "%d-%d" , comment_n->line_open, comment_n->line_close);
}
//new_token((Queue *) alpha_yylval, comment_n->line_open, temp, comment, BLOCKCOMM);
//free(comment_n);
}
//free(yytext);
Stack_destroy(stack);
Stack_destroy(reverse);
}
{comment} {
}
{number} {
alpha_yylval.intValue = atoi(yytext);
return INTNUM;
}
{float} {
alpha_yylval.floatValue = atof(yytext);
return REALNUM;
}
{delimiter} {
}
. {
fprintf(stderr, "Unexpected identifier \"%s\" in line %d.\n", yytext, yylineno);
exit(EXIT_FAILURE);
}
<<EOF>> {
return 0;
}
%%