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