Mercurial > cpython-lru_cache-weakref
annotate Parser/pgenmain.c @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 1c9a84e0981d |
| children | afd54f895dc3 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2060
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2044
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
146890653a57
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2044
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
| 145 | 9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
| 145 | 11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
15 permission. |
| 145 | 16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2060
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
| 145 | 29 |
| 30 ******************************************************************/ | |
| 31 | |
| 6 | 32 /* Parser generator main program */ |
| 33 | |
| 86 | 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. */ | |
| 6 | 41 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
42 /* XXX TO DO: |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
43 - check for duplicate definitions of names (instead of fatal err) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
44 */ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
45 |
| 86 | 46 #include "pgenheaders.h" |
| 6 | 47 #include "grammar.h" |
| 48 #include "node.h" | |
| 49 #include "parsetok.h" | |
| 50 #include "pgen.h" | |
| 51 | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
52 int Py_DebugFlag; |
| 6 | 53 |
| 86 | 54 /* Forward */ |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
55 grammar *getgrammar Py_PROTO((char *filename)); |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
56 #ifdef THINK_C |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
57 int main Py_PROTO((int, char **)); |
|
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
58 char *askfile Py_PROTO((void)); |
| 6 | 59 #endif |
| 60 | |
|
1319
cd397266dc1c
* Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
61 void |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
62 Py_Exit(sts) |
|
1319
cd397266dc1c
* Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
63 int sts; |
|
cd397266dc1c
* Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
64 { |
|
cd397266dc1c
* Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
65 exit(sts); |
|
cd397266dc1c
* Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
66 } |
|
cd397266dc1c
* Makefile: added all: and default: targets.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
67 |
| 86 | 68 int |
| 6 | 69 main(argc, argv) |
| 70 int argc; | |
| 71 char **argv; | |
| 72 { | |
| 73 grammar *g; | |
| 74 FILE *fp; | |
| 75 char *filename; | |
| 76 | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
77 #ifdef THINK_C |
| 6 | 78 filename = askfile(); |
| 79 #else | |
| 80 if (argc != 2) { | |
| 81 fprintf(stderr, "usage: %s grammar\n", argv[0]); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
82 Py_Exit(2); |
| 6 | 83 } |
| 84 filename = argv[1]; | |
| 85 #endif | |
| 86 g = getgrammar(filename); | |
| 87 fp = fopen("graminit.c", "w"); | |
| 88 if (fp == NULL) { | |
| 89 perror("graminit.c"); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
90 Py_Exit(1); |
| 6 | 91 } |
| 92 printf("Writing graminit.c ...\n"); | |
| 93 printgrammar(g, fp); | |
| 94 fclose(fp); | |
| 95 fp = fopen("graminit.h", "w"); | |
| 96 if (fp == NULL) { | |
| 97 perror("graminit.h"); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
98 Py_Exit(1); |
| 6 | 99 } |
| 100 printf("Writing graminit.h ...\n"); | |
| 101 printnonterminals(g, fp); | |
| 102 fclose(fp); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
103 Py_Exit(0); |
| 4577 | 104 return 0; /* Make gcc -Wall happy */ |
| 6 | 105 } |
| 106 | |
| 86 | 107 grammar * |
| 108 getgrammar(filename) | |
| 109 char *filename; | |
| 110 { | |
| 111 FILE *fp; | |
| 112 node *n; | |
| 113 grammar *g0, *g; | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
114 perrdetail err; |
| 86 | 115 |
| 116 fp = fopen(filename, "r"); | |
| 117 if (fp == NULL) { | |
| 118 perror(filename); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
119 Py_Exit(1); |
| 86 | 120 } |
| 121 g0 = meta_grammar(); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
122 n = PyParser_ParseFile(fp, filename, g0, g0->g_start, |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
123 (char *)NULL, (char *)NULL, &err); |
| 86 | 124 fclose(fp); |
| 125 if (n == NULL) { | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
126 fprintf(stderr, "Parsing error %d, line %d.\n", |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
127 err.error, err.lineno); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
128 if (err.text != NULL) { |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
129 int i; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
130 fprintf(stderr, "%s", err.text); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
131 i = strlen(err.text); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
132 if (i == 0 || err.text[i-1] != '\n') |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
133 fprintf(stderr, "\n"); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
134 for (i = 0; i < err.offset; i++) { |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
135 if (err.text[i] == '\t') |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
136 putc('\t', stderr); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
137 else |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
138 putc(' ', stderr); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
139 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
140 fprintf(stderr, "^\n"); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
141 free(err.text); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
142 } |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
143 Py_Exit(1); |
| 86 | 144 } |
| 145 g = pgen(n); | |
| 146 if (g == NULL) { | |
| 147 printf("Bad grammar.\n"); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
148 Py_Exit(1); |
| 86 | 149 } |
| 150 return g; | |
| 151 } | |
| 152 | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
153 #ifdef THINK_C |
| 86 | 154 char * |
| 155 askfile() | |
| 156 { | |
| 157 char buf[256]; | |
| 158 static char name[256]; | |
| 159 printf("Input file name: "); | |
| 160 if (fgets(buf, sizeof buf, stdin) == NULL) { | |
| 161 printf("EOF\n"); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
162 Py_Exit(1); |
| 86 | 163 } |
|
93
9e051fc7073e
Changess for THINK C 4.0.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
164 /* XXX The (unsigned char *) case is needed by THINK C 3.0 */ |
|
9e051fc7073e
Changess for THINK C 4.0.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
165 if (sscanf(/*(unsigned char *)*/buf, " %s ", name) != 1) { |
| 86 | 166 printf("No file\n"); |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
167 Py_Exit(1); |
| 86 | 168 } |
| 169 return name; | |
| 170 } | |
| 171 #endif | |
| 172 | |
| 6 | 173 void |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
174 Py_FatalError(msg) |
| 6 | 175 char *msg; |
| 176 { | |
| 177 fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg); | |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
178 Py_Exit(1); |
| 6 | 179 } |
| 180 | |
|
751
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
181 #ifdef macintosh |
|
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
182 /* ARGSUSED */ |
|
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
183 int |
|
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
184 guesstabsize(path) |
|
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
185 char *path; |
|
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
186 { |
|
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
187 return 4; |
|
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
188 } |
|
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
189 #endif |
|
0d494bc24207
Added guesstabsize() for the mac
Guido van Rossum <guido@python.org>
parents:
293
diff
changeset
|
190 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
191 /* No-nonsense my_readline() for tokenizer.c */ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
192 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
193 char * |
|
5379
1c9a84e0981d
Another directory quickly renamed.
Guido van Rossum <guido@python.org>
parents:
4577
diff
changeset
|
194 PyOS_Readline(prompt) |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
195 char *prompt; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
196 { |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
197 int n = 1000; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
198 char *p = malloc(n); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
199 char *q; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
200 if (p == NULL) |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
201 return NULL; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
202 fprintf(stderr, "%s", prompt); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
203 q = fgets(p, n, stdin); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
204 if (q == NULL) { |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
205 *p = '\0'; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
206 return p; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
207 } |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
208 n = strlen(p); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
209 if (n > 0 && p[n-1] != '\n') |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
210 p[n-1] = '\n'; |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
211 return realloc(p, n+1); |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
212 } |
