xref: /openbsd-src/usr.bin/yacc/error.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: error.c,v 1.5 2001/07/16 06:29:44 pvalchev Exp $	*/
2 /*	$NetBSD: error.c,v 1.4 1996/03/19 03:21:32 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1989 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Robert Paul Corbett.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)error.c	5.3 (Berkeley) 6/1/90";
43 #else
44 static char rcsid[] = "$OpenBSD: error.c,v 1.5 2001/07/16 06:29:44 pvalchev Exp $";
45 #endif
46 #endif /* not lint */
47 
48 /* routines for printing error messages  */
49 
50 #include "defs.h"
51 
52 
53 void
54 fatal(msg)
55 char *msg;
56 {
57     fprintf(stderr, "%s: f - %s\n", __progname, msg);
58     done(2);
59 }
60 
61 
62 void
63 no_space()
64 {
65     fprintf(stderr, "%s: f - out of space\n", __progname);
66     done(2);
67 }
68 
69 
70 void
71 open_error(filename)
72 char *filename;
73 {
74     fprintf(stderr, "%s: f - cannot open \"%s\"\n", __progname, filename);
75     done(2);
76 }
77 
78 
79 void
80 unexpected_EOF()
81 {
82     fprintf(stderr, "%s: e - line %d of \"%s\", unexpected end-of-file\n",
83 	    __progname, lineno, input_file_name);
84     done(1);
85 }
86 
87 
88 void
89 print_pos(st_line, st_cptr)
90 char *st_line;
91 char *st_cptr;
92 {
93     register char *s;
94 
95     if (st_line == 0) return;
96     for (s = st_line; *s != '\n'; ++s)
97     {
98 	if (isprint(*s) || *s == '\t')
99 	    putc(*s, stderr);
100 	else
101 	    putc('?', stderr);
102     }
103     putc('\n', stderr);
104     for (s = st_line; s < st_cptr; ++s)
105     {
106 	if (*s == '\t')
107 	    putc('\t', stderr);
108 	else
109 	    putc(' ', stderr);
110     }
111     putc('^', stderr);
112     putc('\n', stderr);
113 }
114 
115 void
116 syntax_error(st_lineno, st_line, st_cptr)
117 int st_lineno;
118 char *st_line;
119 char *st_cptr;
120 {
121     fprintf(stderr, "%s: e - line %d of \"%s\", syntax error\n",
122 	    __progname, st_lineno, input_file_name);
123     print_pos(st_line, st_cptr);
124     done(1);
125 }
126 
127 void
128 unterminated_comment(c_lineno, c_line, c_cptr)
129 int c_lineno;
130 char *c_line;
131 char *c_cptr;
132 {
133     fprintf(stderr, "%s: e - line %d of \"%s\", unmatched /*\n",
134 	    __progname, c_lineno, input_file_name);
135     print_pos(c_line, c_cptr);
136     done(1);
137 }
138 
139 void
140 unterminated_string(s_lineno, s_line, s_cptr)
141 int s_lineno;
142 char *s_line;
143 char *s_cptr;
144 {
145     fprintf(stderr, "%s: e - line %d of \"%s\", unterminated string\n",
146 	    __progname, s_lineno, input_file_name);
147     print_pos(s_line, s_cptr);
148     done(1);
149 }
150 
151 void
152 unterminated_text(t_lineno, t_line, t_cptr)
153 int t_lineno;
154 char *t_line;
155 char *t_cptr;
156 {
157     fprintf(stderr, "%s: e - line %d of \"%s\", unmatched %%{\n",
158 	    __progname, t_lineno, input_file_name);
159     print_pos(t_line, t_cptr);
160     done(1);
161 }
162 
163 void
164 unterminated_union(u_lineno, u_line, u_cptr)
165 int u_lineno;
166 char *u_line;
167 char *u_cptr;
168 {
169     fprintf(stderr, "%s: e - line %d of \"%s\", unterminated %%union \
170 declaration\n", __progname, u_lineno, input_file_name);
171     print_pos(u_line, u_cptr);
172     done(1);
173 }
174 
175 void
176 over_unionized(u_cptr)
177 char *u_cptr;
178 {
179     fprintf(stderr, "%s: e - line %d of \"%s\", too many %%union \
180 declarations\n", __progname, lineno, input_file_name);
181     print_pos(line, u_cptr);
182     done(1);
183 }
184 
185 void
186 illegal_tag(t_lineno, t_line, t_cptr)
187 int t_lineno;
188 char *t_line;
189 char *t_cptr;
190 {
191     fprintf(stderr, "%s: e - line %d of \"%s\", illegal tag\n",
192 	    __progname, t_lineno, input_file_name);
193     print_pos(t_line, t_cptr);
194     done(1);
195 }
196 
197 
198 void
199 illegal_character(c_cptr)
200 char *c_cptr;
201 {
202     fprintf(stderr, "%s: e - line %d of \"%s\", illegal character\n",
203 	    __progname, lineno, input_file_name);
204     print_pos(line, c_cptr);
205     done(1);
206 }
207 
208 
209 void
210 used_reserved(s)
211 char *s;
212 {
213     fprintf(stderr, "%s: e - line %d of \"%s\", illegal use of reserved symbol \
214 %s\n", __progname, lineno, input_file_name, s);
215     done(1);
216 }
217 
218 void
219 tokenized_start(s)
220 char *s;
221 {
222      fprintf(stderr, "%s: e - line %d of \"%s\", the start symbol %s cannot be \
223 declared to be a token\n", __progname, lineno, input_file_name, s);
224      done(1);
225 }
226 
227 void
228 retyped_warning(s)
229 char *s;
230 {
231     fprintf(stderr, "%s: w - line %d of \"%s\", the type of %s has been \
232 redeclared\n", __progname, lineno, input_file_name, s);
233 }
234 
235 void
236 reprec_warning(s)
237 char *s;
238 {
239     fprintf(stderr, "%s: w - line %d of \"%s\", the precedence of %s has been \
240 redeclared\n", __progname, lineno, input_file_name, s);
241 }
242 
243 void
244 revalued_warning(s)
245 char *s;
246 {
247     fprintf(stderr, "%s: w - line %d of \"%s\", the value of %s has been \
248 redeclared\n", __progname, lineno, input_file_name, s);
249 }
250 
251 void
252 terminal_start(s)
253 char *s;
254 {
255     fprintf(stderr, "%s: e - line %d of \"%s\", the start symbol %s is a \
256 token\n", __progname, lineno, input_file_name, s);
257     done(1);
258 }
259 
260 void
261 restarted_warning()
262 {
263     fprintf(stderr, "%s: w - line %d of \"%s\", the start symbol has been \
264 redeclared\n", __progname, lineno, input_file_name);
265 }
266 
267 void
268 no_grammar()
269 {
270     fprintf(stderr, "%s: e - line %d of \"%s\", no grammar has been \
271 specified\n", __progname, lineno, input_file_name);
272     done(1);
273 }
274 
275 void
276 terminal_lhs(s_lineno)
277 int s_lineno;
278 {
279     fprintf(stderr, "%s: e - line %d of \"%s\", a token appears on the lhs \
280 of a production\n", __progname, s_lineno, input_file_name);
281     done(1);
282 }
283 
284 void
285 prec_redeclared()
286 {
287     fprintf(stderr, "%s: w - line %d of  \"%s\", conflicting %%prec \
288 specifiers\n", __progname, lineno, input_file_name);
289 }
290 
291 void
292 unterminated_action(a_lineno, a_line, a_cptr)
293 int a_lineno;
294 char *a_line;
295 char *a_cptr;
296 {
297     fprintf(stderr, "%s: e - line %d of \"%s\", unterminated action\n",
298 	    __progname, a_lineno, input_file_name);
299     print_pos(a_line, a_cptr);
300     done(1);
301 }
302 
303 void
304 dollar_warning(a_lineno, i)
305 int a_lineno;
306 int i;
307 {
308     fprintf(stderr, "%s: w - line %d of \"%s\", $%d references beyond the \
309 end of the current rule\n", __progname, a_lineno, input_file_name, i);
310 }
311 
312 void
313 dollar_error(a_lineno, a_line, a_cptr)
314 int a_lineno;
315 char *a_line;
316 char *a_cptr;
317 {
318     fprintf(stderr, "%s: e - line %d of \"%s\", illegal $-name\n",
319 	    __progname, a_lineno, input_file_name);
320     print_pos(a_line, a_cptr);
321     done(1);
322 }
323 
324 
325 void
326 untyped_lhs()
327 {
328     fprintf(stderr, "%s: e - line %d of \"%s\", $$ is untyped\n",
329 	    __progname, lineno, input_file_name);
330     done(1);
331 }
332 
333 void
334 untyped_rhs(i, s)
335 int i;
336 char *s;
337 {
338     fprintf(stderr, "%s: e - line %d of \"%s\", $%d (%s) is untyped\n",
339 	    __progname, lineno, input_file_name, i, s);
340     done(1);
341 }
342 
343 void
344 unknown_rhs(i)
345 int i;
346 {
347     fprintf(stderr, "%s: e - line %d of \"%s\", $%d is untyped\n",
348 	    __progname, lineno, input_file_name, i);
349     done(1);
350 }
351 
352 void
353 default_action_warning()
354 {
355     fprintf(stderr, "%s: w - line %d of \"%s\", the default action assigns an \
356 undefined value to $$\n", __progname, lineno, input_file_name);
357 }
358 
359 void
360 undefined_goal(s)
361 char *s;
362 {
363     fprintf(stderr, "%s: e - the start symbol %s is undefined\n", __progname, s);
364     done(1);
365 }
366 
367 void
368 undefined_symbol_warning(s)
369 char *s;
370 {
371     fprintf(stderr, "%s: w - the symbol %s is undefined\n", __progname, s);
372 }
373