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 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 16 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 17 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 18 | #include "grammar.h" |
| 19 | #include "node.h" |
| 20 | #include "parsetok.h" |
| 21 | #include "pgen.h" |
| 22 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 23 | int Py_DebugFlag; |
Guido van Rossum | c095200 | 1998-04-10 19:34:15 +0000 | [diff] [blame] | 24 | int Py_VerboseFlag; |
Guido van Rossum | 79e39bd | 2002-05-31 14:32:07 +0000 | [diff] [blame] | 25 | int Py_IgnoreEnvironmentFlag; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 28 | grammar *getgrammar(char *filename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 30 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 31 | Py_Exit(int sts) |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 32 | { |
| 33 | exit(sts); |
| 34 | } |
| 35 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 36 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 37 | main(int argc, char **argv) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | { |
| 39 | grammar *g; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | FILE *fp; |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 41 | char *filename, *graminit_h, *graminit_c; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 43 | if (argc != 4) { |
| 44 | fprintf(stderr, |
| 45 | "usage: %s grammar graminit.h graminit.c\n", argv[0]); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 46 | Py_Exit(2); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | } |
| 48 | filename = argv[1]; |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 49 | graminit_h = argv[2]; |
| 50 | graminit_c = argv[3]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | g = getgrammar(filename); |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 52 | fp = fopen(graminit_c, "w"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | if (fp == NULL) { |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 54 | perror(graminit_c); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 55 | Py_Exit(1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | } |
Guido van Rossum | 25dfe2c | 2001-09-11 16:43:16 +0000 | [diff] [blame] | 57 | if (Py_DebugFlag) |
| 58 | printf("Writing %s ...\n", graminit_c); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | printgrammar(g, fp); |
| 60 | fclose(fp); |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 61 | fp = fopen(graminit_h, "w"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | if (fp == NULL) { |
Neil Schemenauer | 84521c0 | 2001-02-16 03:57:53 +0000 | [diff] [blame] | 63 | perror(graminit_h); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 64 | Py_Exit(1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | } |
Guido van Rossum | 25dfe2c | 2001-09-11 16:43:16 +0000 | [diff] [blame] | 66 | if (Py_DebugFlag) |
| 67 | printf("Writing %s ...\n", graminit_h); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | printnonterminals(g, fp); |
| 69 | fclose(fp); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 70 | Py_Exit(0); |
Guido van Rossum | fd8a393 | 1996-12-02 18:27:33 +0000 | [diff] [blame] | 71 | return 0; /* Make gcc -Wall happy */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 74 | grammar * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 75 | getgrammar(char *filename) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 76 | { |
| 77 | FILE *fp; |
| 78 | node *n; |
| 79 | grammar *g0, *g; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 80 | perrdetail err; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 81 | |
| 82 | fp = fopen(filename, "r"); |
| 83 | if (fp == NULL) { |
| 84 | perror(filename); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 85 | Py_Exit(1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 86 | } |
| 87 | g0 = meta_grammar(); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 88 | n = PyParser_ParseFile(fp, filename, g0, g0->g_start, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 89 | (char *)NULL, (char *)NULL, &err); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 90 | fclose(fp); |
| 91 | if (n == NULL) { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 92 | fprintf(stderr, "Parsing error %d, line %d.\n", |
| 93 | err.error, err.lineno); |
| 94 | if (err.text != NULL) { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 95 | size_t i; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 96 | fprintf(stderr, "%s", err.text); |
| 97 | i = strlen(err.text); |
| 98 | if (i == 0 || err.text[i-1] != '\n') |
| 99 | fprintf(stderr, "\n"); |
| 100 | for (i = 0; i < err.offset; i++) { |
| 101 | if (err.text[i] == '\t') |
| 102 | putc('\t', stderr); |
| 103 | else |
| 104 | putc(' ', stderr); |
| 105 | } |
| 106 | fprintf(stderr, "^\n"); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 107 | PyObject_FREE(err.text); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 108 | } |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 109 | Py_Exit(1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 110 | } |
| 111 | g = pgen(n); |
| 112 | if (g == NULL) { |
| 113 | printf("Bad grammar.\n"); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 114 | Py_Exit(1); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 115 | } |
| 116 | return g; |
| 117 | } |
| 118 | |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 119 | /* Can't happen in pgen */ |
| 120 | PyObject* |
| 121 | PyErr_Occurred() |
| 122 | { |
| 123 | return 0; |
| 124 | } |
| 125 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | void |
Tim Peters | 7c321a8 | 2002-07-09 02:57:01 +0000 | [diff] [blame] | 127 | Py_FatalError(const char *msg) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | { |
| 129 | fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 130 | Py_Exit(1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 133 | /* No-nonsense my_readline() for tokenizer.c */ |
| 134 | |
| 135 | char * |
Skip Montanaro | 35f63a8 | 2002-10-27 01:59:21 +0000 | [diff] [blame] | 136 | PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt) |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 137 | { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 138 | size_t n = 1000; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 139 | char *p = (char *)PyMem_MALLOC(n); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 140 | char *q; |
| 141 | if (p == NULL) |
| 142 | return NULL; |
| 143 | fprintf(stderr, "%s", prompt); |
Skip Montanaro | 35f63a8 | 2002-10-27 01:59:21 +0000 | [diff] [blame] | 144 | q = fgets(p, n, sys_stdin); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 145 | if (q == NULL) { |
| 146 | *p = '\0'; |
| 147 | return p; |
| 148 | } |
| 149 | n = strlen(p); |
| 150 | if (n > 0 && p[n-1] != '\n') |
| 151 | p[n-1] = '\n'; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame^] | 152 | return (char *)PyMem_REALLOC(p, n+1); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 153 | } |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 154 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 155 | /* No-nonsense fgets */ |
| 156 | char * |
| 157 | Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) |
| 158 | { |
| 159 | return fgets(buf, n, stream); |
| 160 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 161 | |
| 162 | |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 163 | #include <stdarg.h> |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 164 | |
| 165 | void |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 166 | PySys_WriteStderr(const char *format, ...) |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 167 | { |
| 168 | va_list va; |
| 169 | |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 170 | va_start(va, format); |
Guido van Rossum | 47fbc40 | 1998-08-25 18:12:36 +0000 | [diff] [blame] | 171 | vfprintf(stderr, format, va); |
| 172 | va_end(va); |
| 173 | } |