]> git.saurik.com Git - bison.git/blame - src/main.c
[__sun && __i386]: Include alloca.h.
[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;
27
28/* Nonzero means failure has been detected; don't write a parser file. */
29int failure;
30
a98ad01e 31/* The name this program was run with, for messages. */
54bd0db4
RS
32char *program_name;
33
34extern void getargs(), openfiles(), reader(), reduce_grammar();
35extern void set_derives(), set_nullable(), generate_states();
36extern void lalr(), initialize_conflicts(), verbose(), terse();
37extern void output(), done();
38
39
40/* VMS complained about using `int'. */
a98ad01e 41
54bd0db4
RS
42int
43main(argc, argv)
a98ad01e
RS
44 int argc;
45 char *argv[];
54bd0db4
RS
46{
47 program_name = argv[0];
29340571
RS
48 setlocale (LC_ALL, "");
49 bindtextdomain (PACKAGE, LOCALEDIR);
50 textdomain (PACKAGE);
51
54bd0db4
RS
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();
a98ad01e
RS
61 if (failure)
62 done(failure);
54bd0db4
RS
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.
fe01694d 81 Also resolve s/r conflicts based on precedence declarations. */
54bd0db4
RS
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}
a98ad01e 94\f
54bd0db4
RS
95/* functions to report errors which prevent a parser from being generated */
96
a98ad01e
RS
97
98/* Return a string containing a printable version of C:
99 either C itself, or the corresponding \DDD code. */
100
101char *
102printable_version(c)
103 char c;
104{
105 static char buf[10];
106 if (c < ' ' || c >= '\177')
107 sprintf(buf, "\\%o", c);
108 else
109 {
110 buf[0] = c;
111 buf[1] = '\0';
112 }
113 return buf;
114}
115
116/* Generate a string from the integer I.
117 Return a ptr to internal memory containing the string. */
118
119char *
120int_to_string(i)
121 int i;
122{
123 static char buf[20];
124 sprintf(buf, "%d", i);
125 return buf;
126}
127
128/* Print the message S for a fatal error. */
129
54bd0db4
RS
130void
131fatal(s)
a98ad01e 132 char *s;
54bd0db4
RS
133{
134 extern char *infile;
135
136 if (infile == 0)
29340571 137 fprintf(stderr, _("fatal error: %s\n"), s);
54bd0db4 138 else
29340571 139 fprintf(stderr, _("\"%s\", line %d: %s\n"), infile, lineno, s);
54bd0db4
RS
140 done(1);
141}
142
143
a98ad01e
RS
144/* Print a message for a fatal error. Use FMT to construct the message
145 and incorporate string X1. */
54bd0db4
RS
146
147void
a98ad01e
RS
148fatals(fmt, x1)
149 char *fmt, *x1;
54bd0db4
RS
150{
151 char buffer[200];
a98ad01e 152 sprintf(buffer, fmt, x1);
54bd0db4
RS
153 fatal(buffer);
154}
155
a98ad01e 156/* Print a warning message S. */
54bd0db4
RS
157
158void
a98ad01e
RS
159warn(s)
160 char *s;
161{
162 extern char *infile;
163
164 if (infile == 0)
29340571 165 fprintf(stderr, _("error: %s\n"), s);
a98ad01e 166 else
29340571 167 fprintf(stderr, _("(\"%s\", line %d) error: %s\n"),
a98ad01e
RS
168 infile, lineno, s);
169
170 failure = 1;
171}
172
173/* Print a warning message containing the string for the integer X1.
174 The message is given by the format FMT. */
175
176void
177warni(fmt, x1)
178 char *fmt;
179 int x1;
54bd0db4
RS
180{
181 char buffer[200];
a98ad01e
RS
182 sprintf(buffer, fmt, x1);
183 warn(buffer);
184}
185
186/* Print a warning message containing the string X1.
187 The message is given by the format FMT. */
188
189void
190warns(fmt, x1)
191 char *fmt, *x1;
192{
193 char buffer[200];
194 sprintf(buffer, fmt, x1);
195 warn(buffer);
196}
197
198/* Print a warning message containing the two strings X1 and X2.
199 The message is given by the format FMT. */
200
201void
202warnss(fmt, x1, x2)
203 char *fmt, *x1, *x2;
204{
205 char buffer[200];
206 sprintf(buffer, fmt, x1, x2);
207 warn(buffer);
208}
209
210/* Print a warning message containing the 3 strings X1, X2, X3.
211 The message is given by the format FMT. */
212
213void
214warnsss(fmt, x1, x2, x3)
215 char *fmt, *x1, *x2, *x3;
216{
217 char buffer[200];
218 sprintf(buffer, fmt, x1, x2, x3);
219 warn(buffer);
220}
221
222/* Print a message for the fatal occurence of more than MAXSHORT
223 instances of whatever is denoted by the string S. */
54bd0db4 224
a98ad01e
RS
225void
226toomany(s)
227 char *s;
228{
229 char buffer[200];
29340571 230 sprintf(buffer, _("limit of %d exceeded, too many %s"), MAXSHORT, s);
54bd0db4
RS
231 fatal(buffer);
232}
233
a98ad01e 234/* Abort for an internal error denoted by string S. */
54bd0db4
RS
235
236void
237berror(s)
a98ad01e 238 char *s;
54bd0db4 239{
29340571 240 fprintf(stderr, _("internal error, %s\n"), s);
54bd0db4
RS
241 abort();
242}