Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Parser generator main program */ |
| 3 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 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. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 12 | /* XXX TO DO: |
| 13 | - check for duplicate definitions of names (instead of fatal err) |
| 14 | */ |
| 15 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 16 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17 | #include "grammar.h" |
| 18 | #include "node.h" |
| 19 | #include "parsetok.h" |
| 20 | #include "pgen.h" |
| 21 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 22 | int Py_DebugFlag; |
Guido van Rossum | c095200 | 1998-04-10 19:34:15 +0000 | [diff] [blame] | 23 | int Py_VerboseFlag; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 26 | grammar *getgrammar(char *filename); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 27 | #ifdef THINK_C |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 28 | int main(int, char **); |
| 29 | char *askfile(void); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 32 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 33 | Py_Exit(int sts) |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 34 | { |
| 35 | exit(sts); |
| 36 | } |
| 37 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 38 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 39 | main(int argc, char **argv) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | { |
| 41 | grammar *g; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | FILE *fp; |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 43 | char *filename, *graminit_h, *graminit_c; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 45 | #ifdef THINK_C |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | filename = askfile(); |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 47 | graminit_h = askfile(); |
| 48 | graminit_c = askfile(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | #else |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 50 | if (argc != 4) { |
| 51 | fprintf(stderr, |
| 52 | "usage: %s grammar graminit.h graminit.c\n", argv[0]); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 53 | Py_Exit(2); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | } |
| 55 | filename = argv[1]; |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 56 | graminit_h = argv[2]; |
| 57 | graminit_c = argv[3]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | #endif |
| 59 | g = getgrammar(filename); |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 60 | fp = fopen(graminit_c, "w"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | if (fp == NULL) { |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 62 | perror(graminit_c); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 63 | Py_Exit(1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | } |
Guido van Rossum | 25dfe2c | 2001-09-11 16:43:16 +0000 | [diff] [blame^] | 65 | if (Py_DebugFlag) |
| 66 | printf("Writing %s ...\n", graminit_c); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | printgrammar(g, fp); |
| 68 | fclose(fp); |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 69 | fp = fopen(graminit_h, "w"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | if (fp == NULL) { |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 71 | perror(graminit_h); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 72 | Py_Exit(1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | } |
Guido van Rossum | 25dfe2c | 2001-09-11 16:43:16 +0000 | [diff] [blame^] | 74 | if (Py_DebugFlag) |
| 75 | printf("Writing %s ...\n", graminit_h); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | printnonterminals(g, fp); |
| 77 | fclose(fp); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 78 | Py_Exit(0); |
Guido van Rossum | fd8a393 | 1996-12-02 18:27:33 +0000 | [diff] [blame] | 79 | return 0; /* Make gcc -Wall happy */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 82 | grammar * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 83 | getgrammar(char *filename) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 84 | { |
| 85 | FILE *fp; |
| 86 | node *n; |
| 87 | grammar *g0, *g; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 88 | perrdetail err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 89 | |
| 90 | fp = fopen(filename, "r"); |
| 91 | if (fp == NULL) { |
| 92 | perror(filename); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 93 | Py_Exit(1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 94 | } |
| 95 | g0 = meta_grammar(); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 96 | n = PyParser_ParseFile(fp, filename, g0, g0->g_start, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 97 | (char *)NULL, (char *)NULL, &err); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 98 | fclose(fp); |
| 99 | if (n == NULL) { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 100 | fprintf(stderr, "Parsing error %d, line %d.\n", |
| 101 | err.error, err.lineno); |
| 102 | if (err.text != NULL) { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 103 | size_t i; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 104 | fprintf(stderr, "%s", err.text); |
| 105 | i = strlen(err.text); |
| 106 | if (i == 0 || err.text[i-1] != '\n') |
| 107 | fprintf(stderr, "\n"); |
| 108 | for (i = 0; i < err.offset; i++) { |
| 109 | if (err.text[i] == '\t') |
| 110 | putc('\t', stderr); |
| 111 | else |
| 112 | putc(' ', stderr); |
| 113 | } |
| 114 | fprintf(stderr, "^\n"); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 115 | PyMem_DEL(err.text); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 116 | } |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 117 | Py_Exit(1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 118 | } |
| 119 | g = pgen(n); |
| 120 | if (g == NULL) { |
| 121 | printf("Bad grammar.\n"); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 122 | Py_Exit(1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 123 | } |
| 124 | return g; |
| 125 | } |
| 126 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 127 | #ifdef THINK_C |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 128 | char * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 129 | askfile(void) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 130 | { |
| 131 | char buf[256]; |
| 132 | static char name[256]; |
| 133 | printf("Input file name: "); |
| 134 | if (fgets(buf, sizeof buf, stdin) == NULL) { |
| 135 | printf("EOF\n"); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 136 | Py_Exit(1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 137 | } |
Guido van Rossum | 706eea8 | 1990-12-20 23:11:02 +0000 | [diff] [blame] | 138 | /* XXX The (unsigned char *) case is needed by THINK C 3.0 */ |
| 139 | if (sscanf(/*(unsigned char *)*/buf, " %s ", name) != 1) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 140 | printf("No file\n"); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 141 | Py_Exit(1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 142 | } |
| 143 | return name; |
| 144 | } |
| 145 | #endif |
| 146 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 148 | Py_FatalError(char *msg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | { |
| 150 | fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 151 | Py_Exit(1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Guido van Rossum | 7ebb23c | 1992-03-27 17:31:35 +0000 | [diff] [blame] | 154 | #ifdef macintosh |
| 155 | /* ARGSUSED */ |
| 156 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 157 | guesstabsize(char *path) |
Guido van Rossum | 7ebb23c | 1992-03-27 17:31:35 +0000 | [diff] [blame] | 158 | { |
| 159 | return 4; |
| 160 | } |
| 161 | #endif |
| 162 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 163 | /* No-nonsense my_readline() for tokenizer.c */ |
| 164 | |
| 165 | char * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 166 | PyOS_Readline(char *prompt) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 167 | { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 168 | size_t n = 1000; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 169 | char *p = PyMem_MALLOC(n); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 170 | char *q; |
| 171 | if (p == NULL) |
| 172 | return NULL; |
| 173 | fprintf(stderr, "%s", prompt); |
| 174 | q = fgets(p, n, stdin); |
| 175 | if (q == NULL) { |
| 176 | *p = '\0'; |
| 177 | return p; |
| 178 | } |
| 179 | n = strlen(p); |
| 180 | if (n > 0 && p[n-1] != '\n') |
| 181 | p[n-1] = '\n'; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 182 | return PyMem_REALLOC(p, n+1); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 183 | } |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 185 | #include <stdarg.h> |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 186 | |
| 187 | void |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 188 | PySys_WriteStderr(const char *format, ...) |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 189 | { |
| 190 | va_list va; |
| 191 | |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 192 | va_start(va, format); |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 193 | vfprintf(stderr, format, va); |
| 194 | va_end(va); |
| 195 | } |