blob: 52b8380c33f76ab3929fbd2ac59d3a78bcc513c9 [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
Victor Stinner60fe4992010-12-28 23:05:20 +000016#define PGEN
17
Jack Jansen7b8c7542002-04-14 20:12:41 +000018#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000019#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020#include "grammar.h"
21#include "node.h"
22#include "parsetok.h"
23#include "pgen.h"
24
Guido van Rossum86bea461997-04-29 21:03:06 +000025int Py_DebugFlag;
Guido van Rossumc0952001998-04-10 19:34:15 +000026int Py_VerboseFlag;
Guido van Rossum79e39bd2002-05-31 14:32:07 +000027int Py_IgnoreEnvironmentFlag;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossum3f5da241990-12-20 15:06:42 +000029/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000030grammar *getgrammar(char *filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
Brett Cannonb94767f2011-02-22 20:15:44 +000032void Py_Exit(int) _Py_NO_RETURN;
33
Guido van Rossumf1dc5661993-07-05 10:31:29 +000034void
Thomas Wouters23c9e002000-07-22 19:20:54 +000035Py_Exit(int sts)
Guido van Rossumf1dc5661993-07-05 10:31:29 +000036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 exit(sts);
Guido van Rossumf1dc5661993-07-05 10:31:29 +000038}
39
Guido van Rossum3f5da241990-12-20 15:06:42 +000040int
Thomas Wouters23c9e002000-07-22 19:20:54 +000041main(int argc, char **argv)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 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 Rossum85a5fbb1990-10-14 12:07:46 +000076}
77
Guido van Rossum3f5da241990-12-20 15:06:42 +000078grammar *
Thomas Wouters23c9e002000-07-22 19:20:54 +000079getgrammar(char *filename)
Guido van Rossum3f5da241990-12-20 15:06:42 +000080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 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 Rossum3f5da241990-12-20 15:06:42 +0000121}
122
Neal Norwitz40d37812005-10-02 01:48:49 +0000123/* Can't happen in pgen */
124PyObject*
125PyErr_Occurred()
126{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 return 0;
Neal Norwitz40d37812005-10-02 01:48:49 +0000128}
129
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130void
Tim Peters7c321a82002-07-09 02:57:01 +0000131Py_FatalError(const char *msg)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg);
134 Py_Exit(1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000137/* No-nonsense my_readline() for tokenizer.c */
138
139char *
Skip Montanaro35f63a82002-10-27 01:59:21 +0000140PyOS_Readline(FILE *sys_stdin, FILE *sys_stdout, char *prompt)
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 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 Rossum1d5735e1994-08-30 08:27:36 +0000157}
Guido van Rossum47fbc401998-08-25 18:12:36 +0000158
Jack Jansen7b8c7542002-04-14 20:12:41 +0000159/* No-nonsense fgets */
160char *
161Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 return fgets(buf, n, stream);
Jack Jansen7b8c7542002-04-14 20:12:41 +0000164}
Jack Jansen7b8c7542002-04-14 20:12:41 +0000165
166
Guido van Rossum47fbc401998-08-25 18:12:36 +0000167#include <stdarg.h>
Guido van Rossum47fbc401998-08-25 18:12:36 +0000168
169void
Guido van Rossum47fbc401998-08-25 18:12:36 +0000170PySys_WriteStderr(const char *format, ...)
Guido van Rossum47fbc401998-08-25 18:12:36 +0000171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 va_list va;
Guido van Rossum47fbc401998-08-25 18:12:36 +0000173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174 va_start(va, format);
175 vfprintf(stderr, format, va);
176 va_end(va);
Guido van Rossum47fbc401998-08-25 18:12:36 +0000177}