Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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. |
| 14 | |
| 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. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Parser generator main program */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 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. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | #include "grammar.h" |
| 37 | #include "node.h" |
| 38 | #include "parsetok.h" |
| 39 | #include "pgen.h" |
| 40 | |
| 41 | int debugging; |
| 42 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 43 | /* Forward */ |
| 44 | grammar *getgrammar PROTO((char *filename)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | #ifdef THINK_C |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | int main PROTO((int, char **)); |
| 47 | char *askfile PROTO((void)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | #endif |
| 49 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 50 | int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | main(argc, argv) |
| 52 | int argc; |
| 53 | char **argv; |
| 54 | { |
| 55 | grammar *g; |
| 56 | node *n; |
| 57 | FILE *fp; |
| 58 | char *filename; |
| 59 | |
| 60 | #ifdef THINK_C |
| 61 | filename = askfile(); |
| 62 | #else |
| 63 | if (argc != 2) { |
| 64 | fprintf(stderr, "usage: %s grammar\n", argv[0]); |
| 65 | exit(2); |
| 66 | } |
| 67 | filename = argv[1]; |
| 68 | #endif |
| 69 | g = getgrammar(filename); |
| 70 | fp = fopen("graminit.c", "w"); |
| 71 | if (fp == NULL) { |
| 72 | perror("graminit.c"); |
| 73 | exit(1); |
| 74 | } |
| 75 | printf("Writing graminit.c ...\n"); |
| 76 | printgrammar(g, fp); |
| 77 | fclose(fp); |
| 78 | fp = fopen("graminit.h", "w"); |
| 79 | if (fp == NULL) { |
| 80 | perror("graminit.h"); |
| 81 | exit(1); |
| 82 | } |
| 83 | printf("Writing graminit.h ...\n"); |
| 84 | printnonterminals(g, fp); |
| 85 | fclose(fp); |
| 86 | exit(0); |
| 87 | } |
| 88 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 89 | grammar * |
| 90 | getgrammar(filename) |
| 91 | char *filename; |
| 92 | { |
| 93 | FILE *fp; |
| 94 | node *n; |
| 95 | grammar *g0, *g; |
| 96 | |
| 97 | fp = fopen(filename, "r"); |
| 98 | if (fp == NULL) { |
| 99 | perror(filename); |
| 100 | exit(1); |
| 101 | } |
| 102 | g0 = meta_grammar(); |
| 103 | n = NULL; |
| 104 | parsefile(fp, filename, g0, g0->g_start, (char *)NULL, (char *)NULL, &n); |
| 105 | fclose(fp); |
| 106 | if (n == NULL) { |
| 107 | fprintf(stderr, "Parsing error.\n"); |
| 108 | exit(1); |
| 109 | } |
| 110 | g = pgen(n); |
| 111 | if (g == NULL) { |
| 112 | printf("Bad grammar.\n"); |
| 113 | exit(1); |
| 114 | } |
| 115 | return g; |
| 116 | } |
| 117 | |
| 118 | #ifdef THINK_C |
| 119 | char * |
| 120 | askfile() |
| 121 | { |
| 122 | char buf[256]; |
| 123 | static char name[256]; |
| 124 | printf("Input file name: "); |
| 125 | if (fgets(buf, sizeof buf, stdin) == NULL) { |
| 126 | printf("EOF\n"); |
| 127 | exit(1); |
| 128 | } |
Guido van Rossum | 706eea8 | 1990-12-20 23:11:02 +0000 | [diff] [blame] | 129 | /* XXX The (unsigned char *) case is needed by THINK C 3.0 */ |
| 130 | if (sscanf(/*(unsigned char *)*/buf, " %s ", name) != 1) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 131 | printf("No file\n"); |
| 132 | exit(1); |
| 133 | } |
| 134 | return name; |
| 135 | } |
| 136 | #endif |
| 137 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | void |
| 139 | fatal(msg) |
| 140 | char *msg; |
| 141 | { |
| 142 | fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg); |
| 143 | exit(1); |
| 144 | } |
| 145 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 146 | /* XXX TO DO: |
| 147 | - check for duplicate definitions of names (instead of fatal err) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | */ |