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