blob: 88fa7f19b84a84480feeb9d85a8eca3ae90df6e9 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Parser generator main program */
3
Guido van Rossum3f5da241990-12-20 15:06:42 +00004/* 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 Rossum85a5fbb1990-10-14 12:07:46 +000011
Guido van Rossum1d5735e1994-08-30 08:27:36 +000012/* XXX TO DO:
13 - check for duplicate definitions of names (instead of fatal err)
14*/
15
Jack Jansen7b8c7542002-04-14 20:12:41 +000016#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018#include "grammar.h"
19#include "node.h"
20#include "parsetok.h"
21#include "pgen.h"
22
Guido van Rossum86bea461997-04-29 21:03:06 +000023int Py_DebugFlag;
Guido van Rossumc0952001998-04-10 19:34:15 +000024int Py_VerboseFlag;
Guido van Rossum79e39bd2002-05-31 14:32:07 +000025int Py_IgnoreEnvironmentFlag;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000026
Guido van Rossum3f5da241990-12-20 15:06:42 +000027/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000028grammar *getgrammar(char *filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029
Guido van Rossumf1dc5661993-07-05 10:31:29 +000030void
Thomas Wouters23c9e002000-07-22 19:20:54 +000031Py_Exit(int sts)
Guido van Rossumf1dc5661993-07-05 10:31:29 +000032{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000033 exit(sts);
Guido van Rossumf1dc5661993-07-05 10:31:29 +000034}
35
Guido van Rossum3f5da241990-12-20 15:06:42 +000036int
Thomas Wouters23c9e002000-07-22 19:20:54 +000037main(int argc, char **argv)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000038{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000039 grammar *g;
40 FILE *fp;
41 char *filename, *graminit_h, *graminit_c;
42
43 if (argc != 4) {
44 fprintf(stderr,
45 "usage: %s grammar graminit.h graminit.c\n", argv[0]);
46 Py_Exit(2);
47 }
48 filename = argv[1];
49 graminit_h = argv[2];
50 graminit_c = argv[3];
51 g = getgrammar(filename);
52 fp = fopen(graminit_c, "w");
53 if (fp == NULL) {
54 perror(graminit_c);
55 Py_Exit(1);
56 }
57 if (Py_DebugFlag)
58 printf("Writing %s ...\n", graminit_c);
59 printgrammar(g, fp);
60 fclose(fp);
61 fp = fopen(graminit_h, "w");
62 if (fp == NULL) {
63 perror(graminit_h);
64 Py_Exit(1);
65 }
66 if (Py_DebugFlag)
67 printf("Writing %s ...\n", graminit_h);
68 printnonterminals(g, fp);
69 fclose(fp);
70 Py_Exit(0);
71 return 0; /* Make gcc -Wall happy */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072}
73
Guido van Rossum3f5da241990-12-20 15:06:42 +000074grammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +000075getgrammar(char *filename)
Guido van Rossum3f5da241990-12-20 15:06:42 +000076{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000077 FILE *fp;
78 node *n;
79 grammar *g0, *g;
80 perrdetail err;
81
82 fp = fopen(filename, "r");
83 if (fp == NULL) {
84 perror(filename);
85 Py_Exit(1);
86 }
87 g0 = meta_grammar();
88 n = PyParser_ParseFile(fp, filename, g0, g0->g_start,
89 (char *)NULL, (char *)NULL, &err);
90 fclose(fp);
91 if (n == NULL) {
92 fprintf(stderr, "Parsing error %d, line %d.\n",
93 err.error, err.lineno);
94 if (err.text != NULL) {
95 size_t i;
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");
107 PyObject_FREE(err.text);
108 }
109 Py_Exit(1);
110 }
111 g = pgen(n);
112 if (g == NULL) {
113 printf("Bad grammar.\n");
114 Py_Exit(1);
115 }
116 return g;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117}
118
Neal Norwitz40d37812005-10-02 01:48:49 +0000119/* Can't happen in pgen */
120PyObject*
121PyErr_Occurred()
122{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000123 return 0;
Neal Norwitz40d37812005-10-02 01:48:49 +0000124}
125
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000126void
Tim Peters7c321a82002-07-09 02:57:01 +0000127Py_FatalError(const char *msg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000129 fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg);
130 Py_Exit(1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131}
132
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000133/* No-nonsense my_readline() for tokenizer.c */
134
135char *
Skip Montanaro35f63a82002-10-27 01:59:21 +0000136PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000137{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000138 size_t n = 1000;
139 char *p = (char *)PyMem_MALLOC(n);
140 char *q;
141 if (p == NULL)
142 return NULL;
143 fprintf(stderr, "%s", prompt);
144 q = fgets(p, n, sys_stdin);
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';
152 return (char *)PyMem_REALLOC(p, n+1);
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000153}
Guido van Rossum47fbc401998-08-25 18:12:36 +0000154
Jack Jansen7b8c7542002-04-14 20:12:41 +0000155/* No-nonsense fgets */
156char *
157Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
158{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 return fgets(buf, n, stream);
Jack Jansen7b8c7542002-04-14 20:12:41 +0000160}
Jack Jansen7b8c7542002-04-14 20:12:41 +0000161
162
Guido van Rossum47fbc401998-08-25 18:12:36 +0000163#include <stdarg.h>
Guido van Rossum47fbc401998-08-25 18:12:36 +0000164
165void
Guido van Rossum47fbc401998-08-25 18:12:36 +0000166PySys_WriteStderr(const char *format, ...)
Guido van Rossum47fbc401998-08-25 18:12:36 +0000167{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000168 va_list va;
Guido van Rossum47fbc401998-08-25 18:12:36 +0000169
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 va_start(va, format);
171 vfprintf(stderr, format, va);
172 va_end(va);
Guido van Rossum47fbc401998-08-25 18:12:36 +0000173}