(py-electric-colon): use a save-excursion instead of a progn in
[python/dscho.git] / Parser / pgenmain.c
blob77743e6cc361f7e596154abc38973eb1bf6c6013
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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 not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Parser generator main program */
27 /* This expects a filename containing the grammar as argv[1] (UNIX)
28 or asks the console for such a file name (THINK C).
29 It writes its output on two files in the current directory:
30 - "graminit.c" gets the grammar as a bunch of initialized data
31 - "graminit.h" gets the grammar's non-terminals as #defines.
32 Error messages and status info during the generation process are
33 written to stdout, or sometimes to stderr. */
35 /* XXX TO DO:
36 - check for duplicate definitions of names (instead of fatal err)
39 #include "pgenheaders.h"
40 #include "grammar.h"
41 #include "node.h"
42 #include "parsetok.h"
43 #include "pgen.h"
45 int debugging;
47 /* Forward */
48 grammar *getgrammar PROTO((char *filename));
49 #ifdef THINK_C
50 int main PROTO((int, char **));
51 char *askfile PROTO((void));
52 #endif
54 void
55 goaway(sts)
56 int sts;
58 exit(sts);
61 int
62 main(argc, argv)
63 int argc;
64 char **argv;
66 grammar *g;
67 FILE *fp;
68 char *filename;
70 #ifdef THINK_C
71 filename = askfile();
72 #else
73 if (argc != 2) {
74 fprintf(stderr, "usage: %s grammar\n", argv[0]);
75 goaway(2);
77 filename = argv[1];
78 #endif
79 g = getgrammar(filename);
80 fp = fopen("graminit.c", "w");
81 if (fp == NULL) {
82 perror("graminit.c");
83 goaway(1);
85 printf("Writing graminit.c ...\n");
86 printgrammar(g, fp);
87 fclose(fp);
88 fp = fopen("graminit.h", "w");
89 if (fp == NULL) {
90 perror("graminit.h");
91 goaway(1);
93 printf("Writing graminit.h ...\n");
94 printnonterminals(g, fp);
95 fclose(fp);
96 goaway(0);
99 grammar *
100 getgrammar(filename)
101 char *filename;
103 FILE *fp;
104 node *n;
105 grammar *g0, *g;
106 perrdetail err;
108 fp = fopen(filename, "r");
109 if (fp == NULL) {
110 perror(filename);
111 goaway(1);
113 g0 = meta_grammar();
114 n = parsefile(fp, filename, g0, g0->g_start,
115 (char *)NULL, (char *)NULL, &err);
116 fclose(fp);
117 if (n == NULL) {
118 fprintf(stderr, "Parsing error %d, line %d.\n",
119 err.error, err.lineno);
120 if (err.text != NULL) {
121 int i;
122 fprintf(stderr, "%s", err.text);
123 i = strlen(err.text);
124 if (i == 0 || err.text[i-1] != '\n')
125 fprintf(stderr, "\n");
126 for (i = 0; i < err.offset; i++) {
127 if (err.text[i] == '\t')
128 putc('\t', stderr);
129 else
130 putc(' ', stderr);
132 fprintf(stderr, "^\n");
133 free(err.text);
135 goaway(1);
137 g = pgen(n);
138 if (g == NULL) {
139 printf("Bad grammar.\n");
140 goaway(1);
142 return g;
145 #ifdef THINK_C
146 char *
147 askfile()
149 char buf[256];
150 static char name[256];
151 printf("Input file name: ");
152 if (fgets(buf, sizeof buf, stdin) == NULL) {
153 printf("EOF\n");
154 goaway(1);
156 /* XXX The (unsigned char *) case is needed by THINK C 3.0 */
157 if (sscanf(/*(unsigned char *)*/buf, " %s ", name) != 1) {
158 printf("No file\n");
159 goaway(1);
161 return name;
163 #endif
165 void
166 fatal(msg)
167 char *msg;
169 fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg);
170 goaway(1);
173 #ifdef macintosh
174 /* ARGSUSED */
176 guesstabsize(path)
177 char *path;
179 return 4;
181 #endif
183 /* No-nonsense my_readline() for tokenizer.c */
185 char *
186 my_readline(prompt)
187 char *prompt;
189 int n = 1000;
190 char *p = malloc(n);
191 char *q;
192 if (p == NULL)
193 return NULL;
194 fprintf(stderr, "%s", prompt);
195 q = fgets(p, n, stdin);
196 if (q == NULL) {
197 *p = '\0';
198 return p;
200 n = strlen(p);
201 if (n > 0 && p[n-1] != '\n')
202 p[n-1] = '\n';
203 return realloc(p, n+1);