-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.c
50 lines (38 loc) · 868 Bytes
/
Main.c
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
# include "Syntax.h"
# include "Parser.h"
# include "Lexer.h"
# include "Interpreter.h"
pt_node *buildAST(const char *expr) {
extern int yyparse(pt_node **, yyscan_t);
FILE *file;
yyscan_t scanner;
pt_node *node = NULL;
YY_BUFFER_STATE state;
if(yylex_init(&scanner)) {
fprintf(stderr, "Failed to initialize parser.\n");
exit(-1);
}
file = fopen(expr, "r");
if(file == NULL) {
perror("fopen");
exit(-1);
}
state = yy_create_buffer(file, 1024, scanner);
yy_switch_to_buffer(state, scanner);
if(yyparse(&node, scanner)) {
fprintf(stderr, "Error parsing input.\n");
exit(-1);
}
yy_delete_buffer(state, scanner);
yylex_destroy(scanner);
return node;
}
int main(int argc, char **argv) {
pt_node *node;
if(argc < 2) {
fprintf(stderr, "Argument required.\n");
return -1;
}
node = buildAST(argv[1]);
return visitRoot(node);
}