2 /* Parser generator main program */
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. */
13 - check for duplicate definitions of names (instead of fatal err)
16 #include "pgenheaders.h"
26 grammar
*getgrammar(char *filename
);
28 int main(int, char **);
39 main(int argc
, char **argv
)
43 char *filename
, *graminit_h
, *graminit_c
;
47 graminit_h
= askfile();
48 graminit_c
= askfile();
52 "usage: %s grammar graminit.h graminit.c\n", argv
[0]);
59 g
= getgrammar(filename
);
60 fp
= fopen(graminit_c
, "w");
65 printf("Writing %s ...\n", graminit_c
);
68 fp
= fopen(graminit_h
, "w");
73 printf("Writing %s ...\n", graminit_h
);
74 printnonterminals(g
, fp
);
77 return 0; /* Make gcc -Wall happy */
81 getgrammar(char *filename
)
88 fp
= fopen(filename
, "r");
94 n
= PyParser_ParseFile(fp
, filename
, g0
, g0
->g_start
,
95 (char *)NULL
, (char *)NULL
, &err
);
98 fprintf(stderr
, "Parsing error %d, line %d.\n",
99 err
.error
, err
.lineno
);
100 if (err
.text
!= NULL
) {
102 fprintf(stderr
, "%s", err
.text
);
103 i
= strlen(err
.text
);
104 if (i
== 0 || err
.text
[i
-1] != '\n')
105 fprintf(stderr
, "\n");
106 for (i
= 0; i
< err
.offset
; i
++) {
107 if (err
.text
[i
] == '\t')
112 fprintf(stderr
, "^\n");
119 printf("Bad grammar.\n");
130 static char name
[256];
131 printf("Input file name: ");
132 if (fgets(buf
, sizeof buf
, stdin
) == NULL
) {
136 /* XXX The (unsigned char *) case is needed by THINK C 3.0 */
137 if (sscanf(/*(unsigned char *)*/buf
, " %s ", name
) != 1) {
146 Py_FatalError(char *msg
)
148 fprintf(stderr
, "pgen: FATAL ERROR: %s\n", msg
);
155 guesstabsize(char *path
)
161 /* No-nonsense my_readline() for tokenizer.c */
164 PyOS_Readline(char *prompt
)
167 char *p
= PyMem_MALLOC(n
);
171 fprintf(stderr
, "%s", prompt
);
172 q
= fgets(p
, n
, stdin
);
178 if (n
> 0 && p
[n
-1] != '\n')
180 return PyMem_REALLOC(p
, n
+1);
186 PySys_WriteStderr(const char *format
, ...)
190 va_start(va
, format
);
191 vfprintf(stderr
, format
, va
);