1 | n/a | |
---|
2 | n/a | /* Parser generator main program */ |
---|
3 | n/a | |
---|
4 | n/a | /* This expects a filename containing the grammar as argv[1] (UNIX) |
---|
5 | n/a | or asks the console for such a file name (THINK C). |
---|
6 | n/a | It writes its output on two files in the current directory: |
---|
7 | n/a | - "graminit.c" gets the grammar as a bunch of initialized data |
---|
8 | n/a | - "graminit.h" gets the grammar's non-terminals as #defines. |
---|
9 | n/a | Error messages and status info during the generation process are |
---|
10 | n/a | written to stdout, or sometimes to stderr. */ |
---|
11 | n/a | |
---|
12 | n/a | /* XXX TO DO: |
---|
13 | n/a | - check for duplicate definitions of names (instead of fatal err) |
---|
14 | n/a | */ |
---|
15 | n/a | |
---|
16 | n/a | #define PGEN |
---|
17 | n/a | |
---|
18 | n/a | #include "Python.h" |
---|
19 | n/a | #include "pgenheaders.h" |
---|
20 | n/a | #include "grammar.h" |
---|
21 | n/a | #include "node.h" |
---|
22 | n/a | #include "parsetok.h" |
---|
23 | n/a | #include "pgen.h" |
---|
24 | n/a | |
---|
25 | n/a | int Py_DebugFlag; |
---|
26 | n/a | int Py_VerboseFlag; |
---|
27 | n/a | int Py_IgnoreEnvironmentFlag; |
---|
28 | n/a | |
---|
29 | n/a | /* Forward */ |
---|
30 | n/a | grammar *getgrammar(const char *filename); |
---|
31 | n/a | |
---|
32 | n/a | void Py_Exit(int) _Py_NO_RETURN; |
---|
33 | n/a | |
---|
34 | n/a | void |
---|
35 | n/a | Py_Exit(int sts) |
---|
36 | n/a | { |
---|
37 | n/a | exit(sts); |
---|
38 | n/a | } |
---|
39 | n/a | |
---|
40 | n/a | #ifdef WITH_THREAD |
---|
41 | n/a | /* Needed by obmalloc.c */ |
---|
42 | n/a | int PyGILState_Check(void) |
---|
43 | n/a | { return 1; } |
---|
44 | n/a | #endif |
---|
45 | n/a | |
---|
46 | n/a | void _PyMem_DumpTraceback(int fd, const void *ptr) |
---|
47 | n/a | {} |
---|
48 | n/a | |
---|
49 | n/a | int |
---|
50 | n/a | main(int argc, char **argv) |
---|
51 | n/a | { |
---|
52 | n/a | grammar *g; |
---|
53 | n/a | FILE *fp; |
---|
54 | n/a | char *filename, *graminit_h, *graminit_c; |
---|
55 | n/a | |
---|
56 | n/a | if (argc != 4) { |
---|
57 | n/a | fprintf(stderr, |
---|
58 | n/a | "usage: %s grammar graminit.h graminit.c\n", argv[0]); |
---|
59 | n/a | Py_Exit(2); |
---|
60 | n/a | } |
---|
61 | n/a | filename = argv[1]; |
---|
62 | n/a | graminit_h = argv[2]; |
---|
63 | n/a | graminit_c = argv[3]; |
---|
64 | n/a | g = getgrammar(filename); |
---|
65 | n/a | fp = fopen(graminit_c, "w"); |
---|
66 | n/a | if (fp == NULL) { |
---|
67 | n/a | perror(graminit_c); |
---|
68 | n/a | Py_Exit(1); |
---|
69 | n/a | } |
---|
70 | n/a | if (Py_DebugFlag) |
---|
71 | n/a | printf("Writing %s ...\n", graminit_c); |
---|
72 | n/a | printgrammar(g, fp); |
---|
73 | n/a | fclose(fp); |
---|
74 | n/a | fp = fopen(graminit_h, "w"); |
---|
75 | n/a | if (fp == NULL) { |
---|
76 | n/a | perror(graminit_h); |
---|
77 | n/a | Py_Exit(1); |
---|
78 | n/a | } |
---|
79 | n/a | if (Py_DebugFlag) |
---|
80 | n/a | printf("Writing %s ...\n", graminit_h); |
---|
81 | n/a | printnonterminals(g, fp); |
---|
82 | n/a | fclose(fp); |
---|
83 | n/a | freegrammar(g); |
---|
84 | n/a | Py_Exit(0); |
---|
85 | n/a | return 0; /* Make gcc -Wall happy */ |
---|
86 | n/a | } |
---|
87 | n/a | |
---|
88 | n/a | grammar * |
---|
89 | n/a | getgrammar(const char *filename) |
---|
90 | n/a | { |
---|
91 | n/a | FILE *fp; |
---|
92 | n/a | node *n; |
---|
93 | n/a | grammar *g0, *g; |
---|
94 | n/a | perrdetail err; |
---|
95 | n/a | |
---|
96 | n/a | fp = fopen(filename, "r"); |
---|
97 | n/a | if (fp == NULL) { |
---|
98 | n/a | perror(filename); |
---|
99 | n/a | Py_Exit(1); |
---|
100 | n/a | } |
---|
101 | n/a | g0 = meta_grammar(); |
---|
102 | n/a | n = PyParser_ParseFile(fp, filename, g0, g0->g_start, |
---|
103 | n/a | (char *)NULL, (char *)NULL, &err); |
---|
104 | n/a | fclose(fp); |
---|
105 | n/a | if (n == NULL) { |
---|
106 | n/a | fprintf(stderr, "Parsing error %d, line %d.\n", |
---|
107 | n/a | err.error, err.lineno); |
---|
108 | n/a | if (err.text != NULL) { |
---|
109 | n/a | size_t len; |
---|
110 | n/a | int i; |
---|
111 | n/a | fprintf(stderr, "%s", err.text); |
---|
112 | n/a | len = strlen(err.text); |
---|
113 | n/a | if (len == 0 || err.text[len-1] != '\n') |
---|
114 | n/a | fprintf(stderr, "\n"); |
---|
115 | n/a | for (i = 0; i < err.offset; i++) { |
---|
116 | n/a | if (err.text[i] == '\t') |
---|
117 | n/a | putc('\t', stderr); |
---|
118 | n/a | else |
---|
119 | n/a | putc(' ', stderr); |
---|
120 | n/a | } |
---|
121 | n/a | fprintf(stderr, "^\n"); |
---|
122 | n/a | PyObject_FREE(err.text); |
---|
123 | n/a | } |
---|
124 | n/a | Py_Exit(1); |
---|
125 | n/a | } |
---|
126 | n/a | g = pgen(n); |
---|
127 | n/a | PyNode_Free(n); |
---|
128 | n/a | if (g == NULL) { |
---|
129 | n/a | printf("Bad grammar.\n"); |
---|
130 | n/a | Py_Exit(1); |
---|
131 | n/a | } |
---|
132 | n/a | return g; |
---|
133 | n/a | } |
---|
134 | n/a | |
---|
135 | n/a | /* Can't happen in pgen */ |
---|
136 | n/a | PyObject* |
---|
137 | n/a | PyErr_Occurred() |
---|
138 | n/a | { |
---|
139 | n/a | return 0; |
---|
140 | n/a | } |
---|
141 | n/a | |
---|
142 | n/a | void |
---|
143 | n/a | Py_FatalError(const char *msg) |
---|
144 | n/a | { |
---|
145 | n/a | fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg); |
---|
146 | n/a | Py_Exit(1); |
---|
147 | n/a | } |
---|
148 | n/a | |
---|
149 | n/a | /* No-nonsense my_readline() for tokenizer.c */ |
---|
150 | n/a | |
---|
151 | n/a | char * |
---|
152 | n/a | PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, const char *prompt) |
---|
153 | n/a | { |
---|
154 | n/a | size_t n = 1000; |
---|
155 | n/a | char *p = (char *)PyMem_MALLOC(n); |
---|
156 | n/a | char *q; |
---|
157 | n/a | if (p == NULL) |
---|
158 | n/a | return NULL; |
---|
159 | n/a | fprintf(stderr, "%s", prompt); |
---|
160 | n/a | q = fgets(p, n, sys_stdin); |
---|
161 | n/a | if (q == NULL) { |
---|
162 | n/a | *p = '\0'; |
---|
163 | n/a | return p; |
---|
164 | n/a | } |
---|
165 | n/a | n = strlen(p); |
---|
166 | n/a | if (n > 0 && p[n-1] != '\n') |
---|
167 | n/a | p[n-1] = '\n'; |
---|
168 | n/a | return (char *)PyMem_REALLOC(p, n+1); |
---|
169 | n/a | } |
---|
170 | n/a | |
---|
171 | n/a | /* No-nonsense fgets */ |
---|
172 | n/a | char * |
---|
173 | n/a | Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) |
---|
174 | n/a | { |
---|
175 | n/a | return fgets(buf, n, stream); |
---|
176 | n/a | } |
---|
177 | n/a | |
---|
178 | n/a | |
---|
179 | n/a | #include <stdarg.h> |
---|
180 | n/a | |
---|
181 | n/a | void |
---|
182 | n/a | PySys_WriteStderr(const char *format, ...) |
---|
183 | n/a | { |
---|
184 | n/a | va_list va; |
---|
185 | n/a | |
---|
186 | n/a | va_start(va, format); |
---|
187 | n/a | vfprintf(stderr, format, va); |
---|
188 | n/a | va_end(va); |
---|
189 | n/a | } |
---|