-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpython_driver.c
More file actions
55 lines (46 loc) · 1016 Bytes
/
python_driver.c
File metadata and controls
55 lines (46 loc) · 1016 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "dparse.h"
extern D_ParserTables parser_tables_gram;
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return 1;
}
FILE *f = fopen(argv[1], "rb");
if (!f) {
perror("fopen");
return 1;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
char *buf = malloc(fsize + 1);
if (!buf) {
perror("malloc");
return 1;
}
if (fread(buf, 1, fsize, f) != fsize) {
fprintf(stderr, "Failed to read file\n");
free(buf);
return 1;
}
buf[fsize] = 0;
fclose(f);
D_Parser *p = new_D_Parser(&parser_tables_gram, 0);
p->save_parse_tree = 1;
p->loc.pathname = argv[1];
if (dparse(p, buf, fsize) && !p->syntax_errors) {
printf("Parse successful\n");
} else {
printf("Parse failed\n");
free_D_Parser(p);
free(buf);
return 1;
}
free_D_Parser(p);
free(buf);
return 0;
}