]> git.saurik.com Git - bison.git/blame - src/main.c
* reader.c (copy_at): New function.
[bison.git] / src / main.c
CommitLineData
54bd0db4 1/* Top level entry point of bison,
e87b5700 2 Copyright (C) 1984, 1986, 1989, 1992, 1995, 2000 Free Software Foundation, Inc.
54bd0db4 3
e87b5700 4 This file is part of Bison, the GNU Compiler Compiler.
54bd0db4 5
e87b5700
AD
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
54bd0db4 10
e87b5700
AD
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
54bd0db4 15
e87b5700
AD
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
54bd0db4
RS
20
21
22#include <stdio.h>
23#include "system.h"
a98ad01e 24#include "machine.h" /* for MAXSHORT */
54bd0db4
RS
25
26extern int lineno;
27extern int verboseflag;
d2729d44 28extern char *infile;
54bd0db4
RS
29
30/* Nonzero means failure has been detected; don't write a parser file. */
31int failure;
32
a98ad01e 33/* The name this program was run with, for messages. */
54bd0db4
RS
34char *program_name;
35
d2729d44
JT
36char *printable_version PARAMS((int));
37char *int_to_string PARAMS((int));
38void fatal PARAMS((char *));
39void fatals PARAMS((char *, char *));
40void warn PARAMS((char *));
41void warni PARAMS((char *, int));
42void warns PARAMS((char *, char *));
43void warnss PARAMS((char *, char *, char *));
44void warnsss PARAMS((char *, char *, char *, char *));
45void toomany PARAMS((char *));
46void berror PARAMS((char *));
47
48extern void getargs PARAMS((int, char *[]));
49extern void openfiles PARAMS((void));
50extern void reader PARAMS((void));
51extern void reduce_grammar PARAMS((void));
52extern void set_derives PARAMS((void));
53extern void set_nullable PARAMS((void));
54extern void generate_states PARAMS((void));
55extern void lalr PARAMS((void));
56extern void initialize_conflicts PARAMS((void));
57extern void verbose PARAMS((void));
58extern void terse PARAMS((void));
59extern void output PARAMS((void));
60extern void done PARAMS((int));
54bd0db4
RS
61
62
63/* VMS complained about using `int'. */
a98ad01e 64
54bd0db4 65int
d2729d44 66main (int argc, char *argv[])
54bd0db4
RS
67{
68 program_name = argv[0];
29340571
RS
69 setlocale (LC_ALL, "");
70 bindtextdomain (PACKAGE, LOCALEDIR);
71 textdomain (PACKAGE);
72
54bd0db4
RS
73 failure = 0;
74 lineno = 0;
75 getargs(argc, argv);
76 openfiles();
77
78 /* read the input. Copy some parts of it to fguard, faction, ftable and fattrs.
79 In file reader.c.
80 The other parts are recorded in the grammar; see gram.h. */
81 reader();
a98ad01e
RS
82 if (failure)
83 done(failure);
54bd0db4
RS
84
85 /* find useless nonterminals and productions and reduce the grammar. In
86 file reduce.c */
87 reduce_grammar();
88
89 /* record other info about the grammar. In files derives and nullable. */
90 set_derives();
91 set_nullable();
92
93 /* convert to nondeterministic finite state machine. In file LR0.
94 See state.h for more info. */
95 generate_states();
96
97 /* make it deterministic. In file lalr. */
98 lalr();
99
100 /* Find and record any conflicts: places where one token of lookahead is not
101 enough to disambiguate the parsing. In file conflicts.
fe01694d 102 Also resolve s/r conflicts based on precedence declarations. */
54bd0db4
RS
103 initialize_conflicts();
104
105 /* print information about results, if requested. In file print. */
106 if (verboseflag)
107 verbose();
108 else
109 terse();
110
111 /* output the tables and the parser to ftable. In file output. */
112 output();
113 done(failure);
42f95d63 114 return failure;
54bd0db4 115}
a98ad01e 116\f
54bd0db4
RS
117/* functions to report errors which prevent a parser from being generated */
118
a98ad01e
RS
119
120/* Return a string containing a printable version of C:
121 either C itself, or the corresponding \DDD code. */
122
123char *
d2729d44 124printable_version (int c)
a98ad01e
RS
125{
126 static char buf[10];
127 if (c < ' ' || c >= '\177')
128 sprintf(buf, "\\%o", c);
129 else
130 {
131 buf[0] = c;
132 buf[1] = '\0';
133 }
134 return buf;
135}
136
137/* Generate a string from the integer I.
138 Return a ptr to internal memory containing the string. */
139
140char *
d2729d44 141int_to_string (int i)
a98ad01e
RS
142{
143 static char buf[20];
144 sprintf(buf, "%d", i);
145 return buf;
146}
147
e87b5700
AD
148/* Display filename and lino if available. */
149
a889d44c 150static void
e87b5700 151banner (void)
a889d44c
JT
152{
153 if (infile == 0)
e87b5700 154 fprintf(stderr, "%s: ", program_name);
a889d44c 155 else
e87b5700
AD
156 fprintf(stderr, "%s:%d: ", infile, lineno);
157}
158
159static void
160fatal_banner (void)
161{
162 banner ();
163 fputs (_("fatal error: "), stderr);
a889d44c
JT
164}
165
a98ad01e
RS
166/* Print the message S for a fatal error. */
167
54bd0db4 168void
d2729d44 169fatal (char *s)
54bd0db4 170{
a889d44c
JT
171 fatal_banner ();
172 fputs (s, stderr);
173 fputc ('\n', stderr);
174 done (1);
54bd0db4
RS
175}
176
177
a98ad01e
RS
178/* Print a message for a fatal error. Use FMT to construct the message
179 and incorporate string X1. */
54bd0db4
RS
180
181void
d2729d44 182fatals (char *fmt, char *x1)
54bd0db4 183{
a889d44c
JT
184 fatal_banner ();
185 fprintf (stderr, fmt, x1);
186 fputc ('\n', stderr);
187 done (1);
188}
189
190static void
191warn_banner (void)
192{
e87b5700 193 banner ();
a889d44c 194 failure = 1;
54bd0db4
RS
195}
196
a98ad01e 197/* Print a warning message S. */
54bd0db4
RS
198
199void
d2729d44 200warn (char *s)
a98ad01e 201{
a889d44c
JT
202 warn_banner ();
203 fputs (s, stderr);
204 fputc ('\n', stderr);
a98ad01e
RS
205}
206
207/* Print a warning message containing the string for the integer X1.
208 The message is given by the format FMT. */
209
210void
d2729d44 211warni (char *fmt, int x1)
54bd0db4 212{
a889d44c
JT
213 warn_banner ();
214 fprintf (stderr, fmt, x1);
215 fputc ('\n', stderr);
a98ad01e
RS
216}
217
218/* Print a warning message containing the string X1.
219 The message is given by the format FMT. */
220
221void
d2729d44 222warns (char *fmt, char *x1)
a98ad01e 223{
a889d44c
JT
224 warn_banner ();
225 fprintf (stderr, fmt, x1);
226 fputc ('\n', stderr);
a98ad01e
RS
227}
228
229/* Print a warning message containing the two strings X1 and X2.
230 The message is given by the format FMT. */
231
232void
d2729d44 233warnss (char *fmt, char *x1, char *x2)
a98ad01e 234{
a889d44c
JT
235 warn_banner ();
236 fprintf (stderr, fmt, x1, x2);
237 fputc ('\n', stderr);
a98ad01e
RS
238}
239
240/* Print a warning message containing the 3 strings X1, X2, X3.
241 The message is given by the format FMT. */
242
243void
d2729d44 244warnsss (char *fmt, char *x1, char *x2, char *x3)
a98ad01e 245{
a889d44c
JT
246 warn_banner ();
247 fprintf (stderr, fmt, x1, x2, x3);
248 fputc ('\n', stderr);
a98ad01e
RS
249}
250
251/* Print a message for the fatal occurence of more than MAXSHORT
252 instances of whatever is denoted by the string S. */
54bd0db4 253
a98ad01e 254void
d2729d44 255toomany (char *s)
a98ad01e 256{
a889d44c
JT
257 fatal_banner ();
258 fprintf (stderr, _("too many %s (max %d)"), s, MAXSHORT);
259 fputc ('\n', stderr);
260 done (1);
54bd0db4
RS
261}
262
a98ad01e 263/* Abort for an internal error denoted by string S. */
54bd0db4
RS
264
265void
d2729d44 266berror (char *s)
54bd0db4 267{
319ce533 268 fprintf(stderr, _("%s: internal error: %s\n"), program_name, s);
54bd0db4
RS
269 abort();
270}