-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.c
More file actions
129 lines (117 loc) · 2.33 KB
/
driver.c
File metadata and controls
129 lines (117 loc) · 2.33 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
#include "util.h"
#include "globals.h"
#include "interpreter.h"
Token next_token;
extern int yylex();
extern char * yytext;
extern int yyleng;
extern TreeNode * parse();
extern int is_valid;
int is_for_ui = 0; /**< Switch for highlighting json. >*/
int mode = 0; /**< Executing mode, default to executing. >*/
/**
* Do lexical analysis with respect to the given args.
* If only lexical analysis, call this.
*/
void lexical_analyze()
{
yylex();
if(is_for_ui)
fprintf(stderr, "[");
while(next_token.type!=AT_EOF){
if(is_for_ui && next_token.type != ERROR)
{
fprintf(stderr, "{'type': 0, 'token': %d, 'lineno': %d, 'linepos': %d, 'len': %d}",
next_token.type,
next_token.lineno,
next_token.linepos,
yyleng
);
}
else if(next_token.type == ERROR)
{
if(is_for_ui)
{
fprintf(stderr, "{'type': 0, 'token': %d, 'lineno': %d, 'linepos': %d, 'len': %d}",
next_token.type,
next_token.lineno,
next_token.linepos,
yyleng
);
}
printf("unrecognized token:%s, at %d:%d\n", yytext, next_token.lineno, next_token.linepos);
}
yylex();
if(is_for_ui && next_token.type!=AT_EOF)
{
fprintf(stderr, ", ");
}
}
if(is_for_ui)
fprintf(stderr, "]\n");
}
/**
* Console args available: -ui -scanning -parsing -analyze.
* -ui: print json to stderr for code highlighting.
* -scanning: only do lexical analysis.
* -parsing: only do lexical and syntactical analysis.
* -analyze: do all analysis.
*/
int main(int argc, char ** argv){
int i = 1;
int gene_tree = 0;
for(i=1; i<argc; i++)
{
if(strcmp(argv[i], "-scanning") == 0)
{
mode = 1;
}
else if(strcmp(argv[i], "-parsing") == 0)
{
mode = 2;
}
else if(strcmp(argv[i], "-analyze") == 0)
{
mode = 3;
}
else if(strcmp(argv[i], "-ui") == 0)
{
is_for_ui = 1;
}
else if(strcmp(argv[i], "-tree") == 0)
{
gene_tree = 1;
}
}
if(mode == 1) // only lexical analysis
{
lexical_analyze();
return 0;
}
// mode = 2 or 0, do sytactical analysis
if(is_for_ui)
{
fprintf(stderr, "[{'type': 5}"); // add a empty dict
}
TreeNode* root = parse();
if(is_for_ui)
{
fprintf(stderr, "]\n");
}
if(gene_tree)
{
FILE* fp;
fp = fopen("pasing_tree.json", "w+");
print_tree_json(root, fp);
fclose(fp);
}
// mode = 0
if(mode == 0 || mode == 3)
{
if(is_valid)
{
analyze(root);
}
}
return 0;
}