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