-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpycompile.l
463 lines (367 loc) · 11.9 KB
/
pycompile.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
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
458
459
460
461
462
463
/*
* @name parser.h
* @description lexer of python
* @author fadel-hasan
*/
/*
To prevent the yywrap() method from being created
because we don't need to execute anything at the end of the file*/
%option noyywrap
%option yylineno
/*
create STRING & STRING2 to work with "" or ''
STRING to work with string in ""
STRING2 to work with string in ''
STRING3 to work with string """ """
COMMENT to work with multiable commnt line like """ """
*/
%x STRING1
%x STRING2
%x STRING3
%x COMMENT
/*
Libraries needed to work with C language
*/
%{
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// #include "stack.c"
// #include "parser.tab.h"
#include "parser.hpp"
#include "python_ast_node.hpp"
%}
/* Define token types as constants */
%{
#define IDENTI 1
#define KEYWORDS 2
#define OPERATOR 4
#define Delimiters 5
int indent_stack[100];
int top = -1;
int dedent_level = 0;
void handle_dedent();
char firstChar;
char *copyyytext;
char* string_literal_value = NULL;
int lno=1;
// struct StackNode* myStack = NULL;
%}
%x DEDENTATION
/* Regular expressions to match tokens */
DIGIT [0-9]
LETTER [_a-zA-Z]
ALPHANUM {LETTER}|{DIGIT}
IDENTI {LETTER}{ALPHANUM}*
/*
integer :
example for this matched case :
7 2147483647 0o177 0b100110111
3 79228162514264337593543950336 0o377 0xdeadbeef
100_000_000_000 0b_1110_0101
floatnumber:
example for this matched case :
3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93
number :
example for this matched case :
intger or folat
*/
NUMBER {integer}|{floatnumber}
integer {decinteger}|{bininteger}|{octinteger}|{hexinteger}
decinteger ({nonzerodigit})(_?{DIGIT})*|(0)+(_?0)*
bininteger 0[b|B]((_?{bindigit})+)
octinteger 0[o|O]((_?{octdigit})+)
hexinteger 0[x|X]((_?{hexdigit})+)
nonzerodigit [1-9]
bindigit 0|1
octdigit [0-7]
hexdigit {DIGIT}|[a-fA-F]
floatnumber ({pointfloat}|{exponentfloat})
pointfloat ({digitpart}?{fraction}|{digitpart}"\.")
exponentfloat ({digitpart}|{pointfloat}){exponent}
digitpart {DIGIT}+(["_"]{DIGIT}+)*
fraction "\."{digitpart}
exponent ["e"|"E"][+|-]?{digitpart}
/*
keyword :
it is match all keywords in python:
False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
*/
KEYWORDS if|else|elif|while|for|def|class|as|is|assert|continue|break|del|except|import|in|lambda|print|finally|global|not|return|True|with|yield|False|await|pass|raise|None|and|try|from|nonlocal|async|or
/*
operators:
it is match all operators in python:
+ - * ** / // % @
<< >> & | ^ ~ :=
< > <= >= == !=
*/
OPERATORS ([=<>!]=?)|(>>)|(<<)|(\*\*)|(:=)|([-&^~\|%@+*\/])|(\/\/)
/*
Delimiters:
it is match all Delimiters in python:
( ) [ ] { }
, : . ; @ = ->
+= -= *= /= //= %= @=
&= |= ^= >>= <<= **=
*/
Delimiters ([\(\)\{\}\[\],:;.@])|(([\+\-\*/%@&\|^])|((\*{2}|<{2}|>{2})))=|(->)
%%
%{
if (top == -1) {
indent_stack[++top] = 0;
}
%}
\r?\n {
printf("newline = %s in line = %d\n",yytext,yylineno);
return NEWLINE;
}
^[ \t]*\r?\n { /* Skip blank lines */ }
^[ \t]*#.*\r?\n { /* Skip whole-line comments. */ }
#.*$ { /* Skip comments on the same line as a statement. */ }
^[^ \t\n]+ {
if (indent_stack[top]!= 0){
copyyytext=strdup(yytext);
firstChar = yytext[0];
unput(yytext[0]);
BEGIN(DEDENTATION);
}
else
{
REJECT;
}
}
^[ \t]+ {
if (indent_stack[top] < yyleng) {
indent_stack[++top] = yyleng;
return INDENT;
} else if (indent_stack[top] > yyleng) {
dedent_level=yyleng;
unput(32);/*32 for space*/
BEGIN(DEDENTATION);
}
}
<DEDENTATION>[ ] {
if (top >= 0 && indent_stack[top] != dedent_level) {
handle_dedent();
return DEDENT;
unput(32);
}
else
{
dedent_level=0;
BEGIN(INITIAL);
}
if (top == -1) {
fprintf(stderr, "Error: Incorrect indentation on line %d\n", yylineno);
exit(1);
}
}
<DEDENTATION>[^ \t\n] {
if (indent_stack[top] != 0) {
top--;
unput(yytext[0]);
return DEDENT;
}
else
{
for (int i = strlen(copyyytext) - 1; i >= 0; i--) {
unput(copyyytext[i]);
}
BEGIN(INITIAL);
}
}
<<EOF>> {
while (top >0) {
handle_dedent();
printf("dedent = %s in line = %d\n",yytext,yylineno);
return DEDENT;
}
}
[ \t] { /*
Ignore spaces that haven't been handled above. */ }
\" {
BEGIN(STRING1); // Transition to the STRING start condition when a double quote is encountered
string_literal_value = strdup(""); // Initialize the string literal value
}
<STRING1>[^\"\n\\]+ {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + yyleng + 1);
strcat(string_literal_value, yytext);
}
<STRING1>\\\n { //skip
}
<STRING1>\\\" {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);
strcat(string_literal_value, "\""); // Handle escaped double quote
}
<STRING1>\" {
printf("LITERAL_STRING : %s\n", string_literal_value);
yylval.astNode = new LiteralNode("string", "string", string_literal_value);
return STRING;
BEGIN(INITIAL); // Return to the initial start condition when a closing double quote is encountered
}
\' {
BEGIN(STRING2); // Transition to the STRING start condition when a double quote is encountered
string_literal_value = strdup(""); // Initialize the string literal value
}
<STRING2>[^\'\n\\]+ {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + yyleng + 1);
strcat(string_literal_value, yytext);
}
<STRING2>\\\n { //string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);strcat(string_literal_value, "\n");
}
<STRING2>\\\' {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);
strcat(string_literal_value, "\'"); // Handle escaped double quote
}
<STRING2>\' {
printf("LITERAL_STRING : %s\n", string_literal_value);
yylval.astNode = new LiteralNode("string", "string", string_literal_value);
return STRING;
BEGIN(INITIAL); // Return to the initial start condition when a closing double quote is encountered
}
\"{3} {
BEGIN(STRING3);
string_literal_value = strdup(""); // Initialize the string literal value
}
<STRING3>[^\\\"]+ {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + yyleng + 1);
strcat(string_literal_value, yytext);
}
<STRING3>\\n {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);
strcat(string_literal_value, "\n"); // Handle escaped double quote
}
<STRING3>\\\" {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);
strcat(string_literal_value, "\""); // Handle escaped double quote
}
<STRING3>\" {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);
strcat(string_literal_value, "\""); // Handle escaped double quote
}
<STRING3>\\ {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);
strcat(string_literal_value, "\\"); // Handle escaped double quote
}
<STRING3>\' {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);
strcat(string_literal_value, "\'"); // Handle escaped double quote
}
<STRING3>\\\' {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);
strcat(string_literal_value, "\'"); // Handle escaped double quote
}
<STRING3>\\\\ {
string_literal_value = realloc(string_literal_value, strlen(string_literal_value) + 1);
strcat(string_literal_value, "\\"); // Handle escaped double quote
}
<STRING3>\"{3} {
printf("LITERAL_STRING : %s\n", string_literal_value);
yylval.astNode = new LiteralNode("string", "string", string_literal_value);
return STRING;
BEGIN(INITIAL);
}
"!=" { return NEQ; }
">" { return GT; }
">=" { return GTE; }
"<" { return LT; }
"<=" { return LTE; }
"(" { return yytext[0]; }
")" { return yytext[0]; }
":" { return COLON; }
"=" {printf("= Assign\n");return ASSIGN;}
"+" {return yytext[0];}
"-" {return yytext[0];}
"*" {return MUL;}
"**" {return yytext[0];}
"/" {return yytext[0];}
"//" {return yytext[0];}
"%" {return yytext[0];}
"@" {return yytext[0];}
"<<" {return LSHIFT;}
">>" {return RSHIFT;}
"&" {return yytext[0];}
"|" {return yytext[0];}
"^" {return yytext[0];}
"~" {return yytext[0];}
":=" {return yytext[0];}
"==" {return EQUAL;}
"[" {return yytext[0];}
"]" {return yytext[0];}
"{" {return yytext[0];}
"}" {return yytext[0];}
"," {return yytext[0];}
"." {return yytext[0];}
";" {return yytext[0];}
"->" {return yytext[0];}
"+=" {return yytext[0];}
"-=" {return yytext[0];}
"*=" {return yytext[0];}
"/=" {return yytext[0];}
"//=" {return yytext[0];}
"%=" {return yytext[0];}
"@=" {return yytext[0];}
"&=" {return yytext[0];}
"|=" {return yytext[0];}
"^=" {return yytext[0];}
">>=" {return yytext[0];}
"<<=" {return yytext[0];}
"**=" {return yytext[0];}
"if" { return IF; }
"else" { return ELSE; }
"elif" { return ELIF; }
"while" { return WHILE; }
"for" { return FOR; }
"def" { return DEF; }
"class" { return CLASS; }
"as" { return AS; }
"is" { return IS; }
"assert" { return ASSERT; }
"continue" { return CONTINUE; }
"break" { return BREAK; }
"del" { return DEL; }
"except" { return EXCEPT; }
"import" { return IMPORT; }
"in" { return IN; }
"lambda" { return LAMBDA; }
"print" { return PRINT; }
"finally" { return FINALLY; }
"global" { return GLOBAL; }
"not" { return NOT; }
"return" { return RETURN; }
"True" { return TRUE; }
"with" { return WITH; }
"yield" { return YIELD; }
"False" { return FALSE; }
"await" { return AWAIT; }
"pass" { return PASS; }
"raise" { return RAISE; }
"None" { return NONE; }
"and" { return AND; }
"try" { return TRY; }
"from" { return FROM; }
"nonlocal" { return NONLOCAL; }
"async" { return ASYNC; }
"or" { return OR; }
"match" {return MATCH;}
"case" {return CASE;}
{IDENTI} {yylval.astNode = new IdentifierNode("IDENTIFIER", "Identifier", yytext); return IDENTIFIER;}
{NUMBER} {yylval.astNode = new NumberNode("NUMBER", "number", atoi(yytext)); return NUMBER;}
#.*$ { printf("COMMENTS3: %s in line = %d\n", yytext,yylineno); /* Skip comments on the same line as a statement. */ }
^\"{3} {
BEGIN(COMMENT);
}
<COMMENT>. {}
<COMMENT>\"{3} {
BEGIN(INITIAL);
}
%%
void handle_dedent() {
top--;
}