At the release of 1.0.1.
[python/dscho.git] / Parser / pgenmain.c
blob1b759d5a5f5d4da4f70a7d01470b125534b72cb4
1 /***********************************************************
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3 Amsterdam, 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 #include "pgenheaders.h"
36 #include "grammar.h"
37 #include "node.h"
38 #include "parsetok.h"
39 #include "pgen.h"
41 int debugging;
43 /* Forward */
44 grammar *getgrammar PROTO((char *filename));
45 #ifdef macintosh
46 int main PROTO((int, char **));
47 char *askfile PROTO((void));
48 #endif
50 void
51 goaway(sts)
52 int sts;
54 exit(sts);
57 int
58 main(argc, argv)
59 int argc;
60 char **argv;
62 grammar *g;
63 node *n;
64 FILE *fp;
65 char *filename;
67 #ifdef macintosh
68 filename = askfile();
69 #else
70 if (argc != 2) {
71 fprintf(stderr, "usage: %s grammar\n", argv[0]);
72 goaway(2);
74 filename = argv[1];
75 #endif
76 g = getgrammar(filename);
77 fp = fopen("graminit.c", "w");
78 if (fp == NULL) {
79 perror("graminit.c");
80 goaway(1);
82 printf("Writing graminit.c ...\n");
83 printgrammar(g, fp);
84 fclose(fp);
85 fp = fopen("graminit.h", "w");
86 if (fp == NULL) {
87 perror("graminit.h");
88 goaway(1);
90 printf("Writing graminit.h ...\n");
91 printnonterminals(g, fp);
92 fclose(fp);
93 goaway(0);
96 grammar *
97 getgrammar(filename)
98 char *filename;
100 FILE *fp;
101 node *n;
102 grammar *g0, *g;
104 fp = fopen(filename, "r");
105 if (fp == NULL) {
106 perror(filename);
107 goaway(1);
109 g0 = meta_grammar();
110 n = NULL;
111 parsefile(fp, filename, g0, g0->g_start, (char *)NULL, (char *)NULL, &n);
112 fclose(fp);
113 if (n == NULL) {
114 fprintf(stderr, "Parsing error.\n");
115 goaway(1);
117 g = pgen(n);
118 if (g == NULL) {
119 printf("Bad grammar.\n");
120 goaway(1);
122 return g;
125 #ifdef macintosh
126 char *
127 askfile()
129 char buf[256];
130 static char name[256];
131 printf("Input file name: ");
132 if (fgets(buf, sizeof buf, stdin) == NULL) {
133 printf("EOF\n");
134 goaway(1);
136 /* XXX The (unsigned char *) case is needed by THINK C 3.0 */
137 if (sscanf(/*(unsigned char *)*/buf, " %s ", name) != 1) {
138 printf("No file\n");
139 goaway(1);
141 return name;
143 #endif
145 void
146 fatal(msg)
147 char *msg;
149 fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg);
150 goaway(1);
153 #ifdef macintosh
154 /* ARGSUSED */
156 guesstabsize(path)
157 char *path;
159 return 4;
161 #endif
163 /* XXX TO DO:
164 - check for duplicate definitions of names (instead of fatal err)