-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathal.l
More file actions
457 lines (428 loc) · 14.8 KB
/
al.l
File metadata and controls
457 lines (428 loc) · 14.8 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
%{
//Τμήμα ορισμών
/*includes*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "parser.hpp"
/*Define yylex*/
#define YY_DECL int yylex(void)
#define MAX_STR_CONST 2048
/*For keeping track of nested comments*/
int comment_nesting = 0;
/*Variables for keeping strings*/
char string_buf[MAX_STR_CONST];
char *string_buf_ptr;
/*token struct*/
struct alpha_token_t {
int token_num;
char* content;
char* category;
int line_no;
struct alpha_token_t *next;
};
/*head of a list that will store all our token*/
struct alpha_token_t *token_head = NULL;
/*Fuctions to use*/
/*Add a new token to the list of tokens*/
void add_new_token(char* content, char* category, int line_no) {
struct alpha_token_t *tmp=token_head;
struct alpha_token_t *new_token = (struct alpha_token_t*)malloc(sizeof(struct alpha_token_t));
//printf("(token:\'%s\" \"%s\" \"lineno:%d\")", content, category, line_no);
/*Initialize fields*/
new_token->line_no = line_no;
new_token->next = NULL;
new_token->content = strdup(content);
new_token->category = strdup(category);
if(tmp == NULL){
/*new_token token is the first*/
new_token->token_num = 1;
token_head = new_token;
return;
}
while( tmp->next != NULL){
tmp=tmp->next;
}
/*tmp now points to the last token in the list*/
new_token->token_num = tmp->token_num + 1;
tmp->next = new_token;
return;
}
/*Prints all tokens. If dest equals 0 prints tokens to stdout, else if its 1 to an output file*/
void print_all_tokens(int dest, char* output_file){
struct alpha_token_t* to_print = token_head;
printf("--------------------------------------------------------\n");
printf("%6s: %6s %20s %20s\n", "Line#", "Token#", "Category", "Content");
switch(dest){
case 0: {
while(to_print != NULL) {
printf("%6d: %5d# %20s %20s\n", to_print->line_no, to_print->token_num, to_print->category, to_print->content);
to_print = to_print->next;
}
break;
}
case 1:{
FILE* fp;
if( !( fp = fopen(output_file,"w") ) ){
printf("An error occured while openning the output file\n");
exit(-1);
}
while( to_print != NULL){
fprintf(fp, "%d: #%d \t %s \t %s\n", to_print->line_no, to_print->token_num, to_print->category, to_print->content);
to_print = to_print->next;
}
break;
}
}
printf("--------------------------------------------------------\n");
}
%}
/*options*/
%option noyywrap
%option yylineno
/*Regular expressions*/
/*Whitespaces*/
new_line [\n]
whitespace [\t\r' ']
EOFile "<<EOF>>"
/*Keywords*/
AND "and"
OR "or"
NOT "not"
TRUE "true"
FALSE "false"
nil "nil"
if "if"
else "else"
while "while"
for "for"
break "break"
continue "continue"
function "function"
return "return"
local "local"
/*Operators*/
assign "="
plus "+"
minus "-"
mul "*"
division "/"
mod "%"
increment "++"
decrement "--"
b_equals "=="
b_not_equal "!="
b_greater ">"
b_greater_eq ">="
b_less "<"
b_less_eq "<="
/*Constants-Identifiers*/
digit [0-9]
letter [a-zA-Z]
underscore "_"
integer {digit}+
real ({digit})+\.({digit})+
id {letter}({digit}|{letter}|{underscore})*
/*String have to be implemented in code*/
string \"
/*Punstuation marks*/
left_curly \{
right_curly \}
left_bracket \[
right_bracket \]
left_parenthesis \(
right_parenthesis \)
semicolon \;
comma \,
colon \:
double_colon \:\:
dot \.
double_dot \.\.
/*Comments*/
start_comment "/*"
end_comment "*/"
line_comment "//".*
/*Conditions for comments and strings*/
%x SC_COMMENT SRULE
%%
{whitespace}* {/*Ignore it*/}
{new_line}+ {
// return new_line;
}
{AND} {
add_new_token("and", "KEYWORD", yylineno);
return AND;
}
{OR} {
add_new_token("or", "KEYWORD", yylineno);
return OR;
}
{NOT} {
add_new_token("not", "KEYWORD", yylineno);
return NOT;
}
{TRUE} {
add_new_token("true", "KEYWORD", yylineno);
return TRUE;
}
{FALSE} {
add_new_token("false", "KEYWORD", yylineno);
return FALSE;
}
{nil} {
add_new_token("nil", "KEYWORD", yylineno);
return NIL;
}
{if} {
add_new_token("if", "KEYWORD", yylineno);
return IF;
}
{else} {
add_new_token("else", "KEYWORD", yylineno);
return ELSE;
}
{while} {
add_new_token("while", "KEYWORD", yylineno);
return WHILE;
}
{for} {
add_new_token("for", "KEYWORD", yylineno);
return FOR;
}
{break} {
add_new_token("break", "KEYWORD", yylineno);
return BREAK;
}
{continue} {
add_new_token("continue", "KEYWORD", yylineno);
return CONTINUE;
}
{function} {
add_new_token("function", "KEYWORD", yylineno);
return function;
}
{return} {
add_new_token("return", "KEYWORD", yylineno);
return RETURN;
}
{local} {
add_new_token("local", "KEYWORD", yylineno);
return local;
}
{assign} {
add_new_token("=", "OPERATOR", yylineno);
return assign;
}
{plus} {
add_new_token("+", "OPERATOR", yylineno);
return plus;
}
{minus} {
add_new_token("-", "OPERATOR", yylineno);
return minus;
}
{mul} {
add_new_token("*", "OPERATOR", yylineno);
return mul;
}
{division} {
add_new_token("/", "OPERATOR", yylineno);
return division;
}
{mod} {
add_new_token("%", "OPERATOR", yylineno);
return mod;
}
{increment} {
add_new_token("++", "OPERATOR", yylineno);
return increment;
}
{decrement} {
add_new_token("--", "OPERATOR", yylineno);
return decrement;
}
{b_equals} {
add_new_token("==", "OPERATOR", yylineno);
return b_equals;
}
{b_not_equal} {
add_new_token("!=", "OPERATOR", yylineno);
return b_not_equal;
}
{b_greater} {
add_new_token(">", "OPERATOR", yylineno);
return b_greater;
}
{b_greater_eq} {
add_new_token(">=", "OPERATOR", yylineno);
return b_greater_eq;
}
{b_less} {
add_new_token("<", "OPERATOR", yylineno);
return b_less;
}
{b_less_eq} {
add_new_token("<=", "OPERATOR", yylineno);
return b_less_eq;
}
{integer} {
add_new_token(yytext, "INT_CONST", yylineno);
yylval.intValue = atoi(yytext);
return integer;
}
{real} {
add_new_token(yytext, "REAL_CONST", yylineno);
yylval.realValue = atof(yytext);
return real;
}
{id} {
add_new_token(yytext, "IDENTIFIER", yylineno);
yylval.stringValue = strdup(yytext);
return id;
}
{left_curly} {
add_new_token("{", "PUNCTUATION_MARK", yylineno);
return left_curly;
}
{right_curly} {
add_new_token("}", "PUNCTUATION_MARK", yylineno);
return right_curly;
}
{left_bracket} {
add_new_token("[", "PUNCTUATION_MARK", yylineno);
return left_bracket;
}
{right_bracket} {
add_new_token("]", "PUNCTUATION_MARK", yylineno);
return right_bracket;
}
{left_parenthesis} {
add_new_token("(", "PUNCTUATION_MARK", yylineno);
return left_parenthesis;
}
{right_parenthesis} {
add_new_token(")", "PUNCTUATION_MARK", yylineno);
return right_parenthesis;
}
{semicolon} {
add_new_token(";", "PUNCTUATION_MARK", yylineno);
return semicolon;
}
{comma} {
add_new_token(",", "PUNCTUATION_MARK", yylineno);
return comma;
}
{colon} {
add_new_token(":", "PUNCTUATION_MARK", yylineno);
return colon;
}
{double_colon} {
add_new_token("::", "PUNCTUATION_MARK", yylineno);
return double_colon;
}
{dot} {
add_new_token(".", "PUNCTUATION_MARK", yylineno);
return dot;
}
{double_dot} {
add_new_token("..", "PUNCTUATION_MARK", yylineno);
return double_dot;
}
{start_comment} {
BEGIN(SC_COMMENT);
}
<SC_COMMENT>{start_comment} {
++comment_nesting;
add_new_token("/**/", "NESTED_COMMENT", yylineno);
}
<SC_COMMENT>"*"+"/" {
if (comment_nesting)
--comment_nesting;
else{
BEGIN(INITIAL);
add_new_token("/**/", "MULTILINE_COMMENT", yylineno);
}
}
<SC_COMMENT>"*"+ {/*Ignore it*/}
<SC_COMMENT>[^/*\n]+ {/*Ignore it*/}
<SC_COMMENT>[/] {/*Ignore it*/}
<SC_COMMENT>\n {/*Ignore it*/}
<SC_COMMENT><<EOF>> { BEGIN(INITIAL);
printf("ERROR: comment doesn't end lineno:%d\n",yylineno);
}
{line_comment} {
add_new_token("//", "ONE_LINE_COMMENT", yylineno);
}
{string} {
string_buf_ptr = string_buf;
BEGIN(SRULE);
}
<SRULE>\" {
BEGIN(INITIAL);
*string_buf_ptr = '\0';
add_new_token(string_buf, "string", yylineno);
yylval.stringValue = strdup(string_buf);
return STRING;
}
<SRULE>\\[0-7]{1,3} {
int result;
(void) sscanf( yytext + 1, "%o", &result );
if ( result > 0xff )
*string_buf_ptr++ = result;
}
<SRULE>\\[0-9]+ {
printf("Bad escape sequence\n");
}
<SRULE>\\n {
*string_buf_ptr++ = '\n';
}
<SRULE>\\t {
*string_buf_ptr++ = '\t';
}
<SRULE>\\r {
*string_buf_ptr++ = '\r';
}
<SRULE>\\b {
*string_buf_ptr++ = '\b';
}
<SRULE>\\f {
*string_buf_ptr++ = '\f';
}
<SRULE>\\(.|\n) {
*string_buf_ptr++ = yytext[1];
printf("[%s]\n", string_buf);
}
<SRULE>[^\\\n\"]+ {
char *yptr = yytext;
while ( *yptr ) *string_buf_ptr++ = *yptr++;
}
<SRULE><<EOF>> { printf("ERROR: string doesn't end lineno:%d\n",yylineno);}
. { printf("ERROR: Other token\n");}
%%
/*int main(int argc, char *argv[]){
FILE* fp;
if( argc < 2 || argc > 3 ){
printf("Invalid arguments number given %d\n", argc);
exit(-1);
}
*/
/*Read text from file*/
/* if( !( fp = fopen(argv[1],"r") ) ){
printf("An error occured while openning the file\n");
exit(-1);
}
*/
/*Set the open file as the input file of lex*/
// yyin = fp;
/*Call lex function*/
/* yylex();
if( argc == 2 ) {
*/ /*Output to stdout*/
/* print_all_tokens(0, NULL);
}
if( argc == 3 ){
*/ /*Output to file*/
/* print_all_tokens(1, argv[2]);
}
return 0;
}*/