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 #include "Python.h"
17 #include "pgenheaders.h"
18 #include "grammar.h"
19 #include "node.h"
20 #include "parsetok.h"
21 #include "pgen.h"
22
23 int Py_DebugFlag;
24 int Py_VerboseFlag;
25 int Py_IgnoreEnvironmentFlag;
26
27 /* Forward */
28 grammar *getgrammar(char *filename);
29
30 void
Py_Exit(int sts)31 Py_Exit(int sts)
32 {
33 exit(sts);
34 }
35
36 int
main(int argc,char ** argv)37 main(int argc, char **argv)
38 {
39 grammar *g;
40 FILE *fp;
41 char *filename, *graminit_h, *graminit_c;
42
43 if (argc != 4) {
44 fprintf(stderr,
45 "usage: %s grammar graminit.h graminit.c\n", argv[0]);
46 Py_Exit(2);
47 }
48 filename = argv[1];
49 graminit_h = argv[2];
50 graminit_c = argv[3];
51 g = getgrammar(filename);
52 fp = fopen(graminit_c, "w");
53 if (fp == NULL) {
54 perror(graminit_c);
55 Py_Exit(1);
56 }
57 if (Py_DebugFlag)
58 printf("Writing %s ...\n", graminit_c);
59 printgrammar(g, fp);
60 fclose(fp);
61 fp = fopen(graminit_h, "w");
62 if (fp == NULL) {
63 perror(graminit_h);
64 Py_Exit(1);
65 }
66 if (Py_DebugFlag)
67 printf("Writing %s ...\n", graminit_h);
68 printnonterminals(g, fp);
69 fclose(fp);
70 freegrammar(g);
71 Py_Exit(0);
72 return 0; /* Make gcc -Wall happy */
73 }
74
75 grammar *
getgrammar(char * filename)76 getgrammar(char *filename)
77 {
78 FILE *fp;
79 node *n;
80 grammar *g0, *g;
81 perrdetail err;
82
83 fp = fopen(filename, "r");
84 if (fp == NULL) {
85 perror(filename);
86 Py_Exit(1);
87 }
88 g0 = meta_grammar();
89 n = PyParser_ParseFile(fp, filename, g0, g0->g_start,
90 (char *)NULL, (char *)NULL, &err);
91 fclose(fp);
92 if (n == NULL) {
93 fprintf(stderr, "Parsing error %d, line %d.\n",
94 err.error, err.lineno);
95 if (err.text != NULL) {
96 size_t i;
97 fprintf(stderr, "%s", err.text);
98 i = strlen(err.text);
99 if (i == 0 || err.text[i-1] != '\n')
100 fprintf(stderr, "\n");
101 for (i = 0; i < err.offset; i++) {
102 if (err.text[i] == '\t')
103 putc('\t', stderr);
104 else
105 putc(' ', stderr);
106 }
107 fprintf(stderr, "^\n");
108 PyObject_FREE(err.text);
109 }
110 Py_Exit(1);
111 }
112 g = pgen(n);
113 if (g == NULL) {
114 printf("Bad grammar.\n");
115 Py_Exit(1);
116 }
117 return g;
118 }
119
120 /* Can't happen in pgen */
121 PyObject*
PyErr_Occurred()122 PyErr_Occurred()
123 {
124 return 0;
125 }
126
127 void
Py_FatalError(const char * msg)128 Py_FatalError(const char *msg)
129 {
130 fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg);
131 Py_Exit(1);
132 }
133
134 /* No-nonsense my_readline() for tokenizer.c */
135
136 char *
PyOS_Readline(FILE * sys_stdin,FILE * sys_stdout,char * prompt)137 PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
138 {
139 size_t n = 1000;
140 char *p = (char *)PyMem_MALLOC(n);
141 char *q;
142 if (p == NULL)
143 return NULL;
144 fprintf(stderr, "%s", prompt);
145 q = fgets(p, n, sys_stdin);
146 if (q == NULL) {
147 *p = '\0';
148 return p;
149 }
150 n = strlen(p);
151 if (n > 0 && p[n-1] != '\n')
152 p[n-1] = '\n';
153 return (char *)PyMem_REALLOC(p, n+1);
154 }
155
156 /* No-nonsense fgets */
157 char *
Py_UniversalNewlineFgets(char * buf,int n,FILE * stream,PyObject * fobj)158 Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
159 {
160 return fgets(buf, n, stream);
161 }
162
163
164 #include <stdarg.h>
165
166 void
PySys_WriteStderr(const char * format,...)167 PySys_WriteStderr(const char *format, ...)
168 {
169 va_list va;
170
171 va_start(va, format);
172 vfprintf(stderr, format, va);
173 va_end(va);
174 }
175