1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Parser generator main program */
34 /* This expects a filename containing the grammar as argv[1] (UNIX)
35 or asks the console for such a file name (THINK C).
36 It writes its output on two files in the current directory:
37 - "graminit.c" gets the grammar as a bunch of initialized data
38 - "graminit.h" gets the grammar's non-terminals as #defines.
39 Error messages and status info during the generation process are
40 written to stdout, or sometimes to stderr. */
43 - check for duplicate definitions of names (instead of fatal err)
46 #include "pgenheaders.h"
56 grammar
*getgrammar
Py_PROTO((char *filename
));
58 int main
Py_PROTO((int, char **));
59 char *askfile
Py_PROTO((void));
82 fprintf(stderr
, "usage: %s grammar\n", argv
[0]);
87 g
= getgrammar(filename
);
88 fp
= fopen("graminit.c", "w");
93 printf("Writing graminit.c ...\n");
96 fp
= fopen("graminit.h", "w");
101 printf("Writing graminit.h ...\n");
102 printnonterminals(g
, fp
);
105 return 0; /* Make gcc -Wall happy */
117 fp
= fopen(filename
, "r");
123 n
= PyParser_ParseFile(fp
, filename
, g0
, g0
->g_start
,
124 (char *)NULL
, (char *)NULL
, &err
);
127 fprintf(stderr
, "Parsing error %d, line %d.\n",
128 err
.error
, err
.lineno
);
129 if (err
.text
!= NULL
) {
131 fprintf(stderr
, "%s", err
.text
);
132 i
= strlen(err
.text
);
133 if (i
== 0 || err
.text
[i
-1] != '\n')
134 fprintf(stderr
, "\n");
135 for (i
= 0; i
< err
.offset
; i
++) {
136 if (err
.text
[i
] == '\t')
141 fprintf(stderr
, "^\n");
148 printf("Bad grammar.\n");
159 static char name
[256];
160 printf("Input file name: ");
161 if (fgets(buf
, sizeof buf
, stdin
) == NULL
) {
165 /* XXX The (unsigned char *) case is needed by THINK C 3.0 */
166 if (sscanf(/*(unsigned char *)*/buf
, " %s ", name
) != 1) {
178 fprintf(stderr
, "pgen: FATAL ERROR: %s\n", msg
);
192 /* No-nonsense my_readline() for tokenizer.c */
195 PyOS_Readline(prompt
)
203 fprintf(stderr
, "%s", prompt
);
204 q
= fgets(p
, n
, stdin
);
210 if (n
> 0 && p
[n
-1] != '\n')
212 return realloc(p
, n
+1);