2 /* Parser generator main program */
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. */
13 - check for duplicate definitions of names (instead of fatal err)
17 #include "pgenheaders.h"
25 int Py_IgnoreEnvironmentFlag
;
28 grammar
*getgrammar(char *filename
);
37 main(int argc
, char **argv
)
41 char *filename
, *graminit_h
, *graminit_c
;
45 "usage: %s grammar graminit.h graminit.c\n", argv
[0]);
51 g
= getgrammar(filename
);
52 fp
= fopen(graminit_c
, "w");
58 printf("Writing %s ...\n", graminit_c
);
61 fp
= fopen(graminit_h
, "w");
67 printf("Writing %s ...\n", graminit_h
);
68 printnonterminals(g
, fp
);
71 return 0; /* Make gcc -Wall happy */
75 getgrammar(char *filename
)
82 fp
= fopen(filename
, "r");
88 n
= PyParser_ParseFile(fp
, filename
, g0
, g0
->g_start
,
89 (char *)NULL
, (char *)NULL
, &err
);
92 fprintf(stderr
, "Parsing error %d, line %d.\n",
93 err
.error
, err
.lineno
);
94 if (err
.text
!= NULL
) {
96 fprintf(stderr
, "%s", 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')
106 fprintf(stderr
, "^\n");
107 PyObject_FREE(err
.text
);
113 printf("Bad grammar.\n");
119 /* Can't happen in pgen */
127 Py_FatalError(const char *msg
)
129 fprintf(stderr
, "pgen: FATAL ERROR: %s\n", msg
);
133 /* No-nonsense my_readline() for tokenizer.c */
136 PyOS_Readline(FILE *sys_stdin
, FILE *sys_stdout
, char *prompt
)
139 char *p
= (char *)PyMem_MALLOC(n
);
143 fprintf(stderr
, "%s", prompt
);
144 q
= fgets(p
, n
, sys_stdin
);
150 if (n
> 0 && p
[n
-1] != '\n')
152 return (char *)PyMem_REALLOC(p
, n
+1);
155 /* No-nonsense fgets */
157 Py_UniversalNewlineFgets(char *buf
, int n
, FILE *stream
, PyObject
*fobj
)
159 return fgets(buf
, n
, stream
);
166 PySys_WriteStderr(const char *format
, ...)
170 va_start(va
, format
);
171 vfprintf(stderr
, format
, va
);