Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Parser generator main program */ |
| 2 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 3 | /* This expects a filename containing the grammar as argv[1] (UNIX) |
| 4 | or asks the console for such a file name (THINK C). |
| 5 | It writes its output on two files in the current directory: |
| 6 | - "graminit.c" gets the grammar as a bunch of initialized data |
| 7 | - "graminit.h" gets the grammar's non-terminals as #defines. |
| 8 | Error messages and status info during the generation process are |
| 9 | written to stdout, or sometimes to stderr. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 11 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | #include "grammar.h" |
| 13 | #include "node.h" |
| 14 | #include "parsetok.h" |
| 15 | #include "pgen.h" |
| 16 | |
| 17 | int debugging; |
| 18 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 19 | /* Forward */ |
| 20 | grammar *getgrammar PROTO((char *filename)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | #ifdef THINK_C |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 22 | int main PROTO((int, char **)); |
| 23 | char *askfile PROTO((void)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 26 | int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | main(argc, argv) |
| 28 | int argc; |
| 29 | char **argv; |
| 30 | { |
| 31 | grammar *g; |
| 32 | node *n; |
| 33 | FILE *fp; |
| 34 | char *filename; |
| 35 | |
| 36 | #ifdef THINK_C |
| 37 | filename = askfile(); |
| 38 | #else |
| 39 | if (argc != 2) { |
| 40 | fprintf(stderr, "usage: %s grammar\n", argv[0]); |
| 41 | exit(2); |
| 42 | } |
| 43 | filename = argv[1]; |
| 44 | #endif |
| 45 | g = getgrammar(filename); |
| 46 | fp = fopen("graminit.c", "w"); |
| 47 | if (fp == NULL) { |
| 48 | perror("graminit.c"); |
| 49 | exit(1); |
| 50 | } |
| 51 | printf("Writing graminit.c ...\n"); |
| 52 | printgrammar(g, fp); |
| 53 | fclose(fp); |
| 54 | fp = fopen("graminit.h", "w"); |
| 55 | if (fp == NULL) { |
| 56 | perror("graminit.h"); |
| 57 | exit(1); |
| 58 | } |
| 59 | printf("Writing graminit.h ...\n"); |
| 60 | printnonterminals(g, fp); |
| 61 | fclose(fp); |
| 62 | exit(0); |
| 63 | } |
| 64 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 65 | grammar * |
| 66 | getgrammar(filename) |
| 67 | char *filename; |
| 68 | { |
| 69 | FILE *fp; |
| 70 | node *n; |
| 71 | grammar *g0, *g; |
| 72 | |
| 73 | fp = fopen(filename, "r"); |
| 74 | if (fp == NULL) { |
| 75 | perror(filename); |
| 76 | exit(1); |
| 77 | } |
| 78 | g0 = meta_grammar(); |
| 79 | n = NULL; |
| 80 | parsefile(fp, filename, g0, g0->g_start, (char *)NULL, (char *)NULL, &n); |
| 81 | fclose(fp); |
| 82 | if (n == NULL) { |
| 83 | fprintf(stderr, "Parsing error.\n"); |
| 84 | exit(1); |
| 85 | } |
| 86 | g = pgen(n); |
| 87 | if (g == NULL) { |
| 88 | printf("Bad grammar.\n"); |
| 89 | exit(1); |
| 90 | } |
| 91 | return g; |
| 92 | } |
| 93 | |
| 94 | #ifdef THINK_C |
| 95 | char * |
| 96 | askfile() |
| 97 | { |
| 98 | char buf[256]; |
| 99 | static char name[256]; |
| 100 | printf("Input file name: "); |
| 101 | if (fgets(buf, sizeof buf, stdin) == NULL) { |
| 102 | printf("EOF\n"); |
| 103 | exit(1); |
| 104 | } |
Guido van Rossum | 706eea8 | 1990-12-20 23:11:02 +0000 | [diff] [blame] | 105 | /* XXX The (unsigned char *) case is needed by THINK C 3.0 */ |
| 106 | if (sscanf(/*(unsigned char *)*/buf, " %s ", name) != 1) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 107 | printf("No file\n"); |
| 108 | exit(1); |
| 109 | } |
| 110 | return name; |
| 111 | } |
| 112 | #endif |
| 113 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | void |
| 115 | fatal(msg) |
| 116 | char *msg; |
| 117 | { |
| 118 | fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg); |
| 119 | exit(1); |
| 120 | } |
| 121 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 122 | /* XXX TO DO: |
| 123 | - check for duplicate definitions of names (instead of fatal err) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | */ |