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