-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.l
More file actions
105 lines (84 loc) · 1.47 KB
/
python.l
File metadata and controls
105 lines (84 loc) · 1.47 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
%{
#include <stdio.h>
#include "sds.h"
%}
%s start_state
%s import
%s function_definition
LP "("
RP ")"
NL "\n"
COLON ":"
RETURN "return"
FROM_IMPORT "from"[ ]+
ID [a-zA-Z][_0-9a-zA-Z.]+
IMPORT "import"[ ]+
COMMA ","
WS [ ]+
AS [ ]+"as"[ ]+
DEF "def"[ ]+
%%
{FROM_IMPORT} BEGIN(import);
<import>{ID}{WS}+ {
sds s = sdsnew(yytext);
sdstrim(s," ");
printf("FROM:%s:",s);
sdsfree(s);
}
<import>{IMPORT}({ID}|({COMMA}{ID})+) {
sds s = sdsnew(yytext);
sdstrim(s," ");
sdsrange(s,6,-1);
sdstrim(s," ");
printf("IMPORT:%s:",s);
sdsfree(s);
}
<import>{AS}{ID}|({COMMA}{ID}{AS}{ID})+ {
sds s = sdsnew(yytext);
sdstrim(s," ");
sdsrange(s,2,-1);
sdstrim(s," ");
printf("AS:%s:",s);
sdsfree(s);
BEGIN(start_state);
}
{DEF} {
sds s = sdsnew(yytext);
sdstrim(s," ");
sdsrange(s,3,-1);
sdstrim(s," ");
printf("FUNCTION_DEF:%s:",s);
sdsfree(s);
BEGIN(function_definition);
}
<function_definition>{ID}{WS}* {
sds s = sdsnew(yytext);
sdstrim(s," ");
printf("NAME:%s:",s);
sdsfree(s);
}
<function_definition>{LP}({ID}({COMMA}{ID})*)?{RP}{COLON} {
sds s = sdsnew(yytext);
sdstrim(s," ");
sdsrange(s,1,-3);
printf("PARAMETERS:%s:",s);
sdsfree(s);
BEGIN(start_state);
}
. { BEGIN(start_state); };
%%
int
main(int argc, char **argv) {
if (argc > 1) {
FILE *file;
file = fopen(argv[1], "r");
if (!file) {
fprintf(stderr, "could not open %s\n", argv[1]);
exit(1);
}
yyin = file;
}
yylex();
//printf("%d",line_no);
return 0;
}