1 /* $Id: error.c,v 1.17 2023/05/18 21:38:35 tom Exp $ */ 2 3 /* routines for printing error messages */ 4 5 #include "defs.h" 6 7 void 8 fatal(const char *msg) 9 { 10 fprintf(stderr, "%s: f - %s\n", myname, msg); 11 done(2); 12 } 13 14 void 15 on_error(void) 16 { 17 const char *msg; 18 if (errno && (msg = strerror(errno)) != NULL) 19 fatal(msg); 20 else 21 fatal("unknown error"); 22 } 23 24 void 25 open_error(const char *filename) 26 { 27 fprintf(stderr, "%s: f - cannot open \"%s\"\n", myname, filename); 28 done(2); 29 } 30 31 void 32 missing_brace(void) 33 { 34 fprintf(stderr, "%s: e - line %d of \"%s\", missing '}'\n", 35 myname, lineno, input_file_name); 36 done(1); 37 } 38 39 void 40 unexpected_EOF(void) 41 { 42 fprintf(stderr, "%s: e - line %d of \"%s\", unexpected end-of-file\n", 43 myname, lineno, input_file_name); 44 done(1); 45 } 46 47 static void 48 print_pos(const char *st_line, const char *st_cptr) 49 { 50 const char *s; 51 52 if (st_line == 0) 53 return; 54 for (s = st_line; *s != '\n'; ++s) 55 { 56 if (isprint(UCH(*s)) || *s == '\t') 57 putc(*s, stderr); 58 else 59 putc('?', stderr); 60 } 61 putc('\n', stderr); 62 for (s = st_line; s < st_cptr; ++s) 63 { 64 if (*s == '\t') 65 putc('\t', stderr); 66 else 67 putc(' ', stderr); 68 } 69 putc('^', stderr); 70 putc('\n', stderr); 71 } 72 73 void 74 syntax_error(int st_lineno, const char *st_line, const char *st_cptr) 75 { 76 fprintf(stderr, "%s: e - line %d of \"%s\", syntax error\n", 77 myname, st_lineno, input_file_name); 78 print_pos(st_line, st_cptr); 79 done(1); 80 } 81 82 void 83 unexpected_value(const struct ainfo *a) 84 { 85 fprintf(stderr, "%s: e - line %d of \"%s\", unexpected value\n", 86 myname, a->a_lineno, input_file_name); 87 print_pos(a->a_line, a->a_cptr); 88 done(1); 89 } 90 91 void 92 unterminated_comment(const struct ainfo *a) 93 { 94 fprintf(stderr, "%s: e - line %d of \"%s\", unmatched /*\n", 95 myname, a->a_lineno, input_file_name); 96 print_pos(a->a_line, a->a_cptr); 97 done(1); 98 } 99 100 void 101 unterminated_string(const struct ainfo *a) 102 { 103 fprintf(stderr, "%s: e - line %d of \"%s\", unterminated string\n", 104 myname, a->a_lineno, input_file_name); 105 print_pos(a->a_line, a->a_cptr); 106 done(1); 107 } 108 109 void 110 unterminated_text(const struct ainfo *a) 111 { 112 fprintf(stderr, "%s: e - line %d of \"%s\", unmatched %%{\n", 113 myname, a->a_lineno, input_file_name); 114 print_pos(a->a_line, a->a_cptr); 115 done(1); 116 } 117 118 void 119 unterminated_union(const struct ainfo *a) 120 { 121 fprintf(stderr, "%s: e - line %d of \"%s\", unterminated %%union \ 122 declaration\n", myname, a->a_lineno, input_file_name); 123 print_pos(a->a_line, a->a_cptr); 124 done(1); 125 } 126 127 void 128 over_unionized(const char *u_cptr) 129 { 130 fprintf(stderr, "%s: e - line %d of \"%s\", too many %%union \ 131 declarations\n", myname, lineno, input_file_name); 132 print_pos(line, u_cptr); 133 done(1); 134 } 135 136 void 137 illegal_tag(int t_lineno, const char *t_line, const char *t_cptr) 138 { 139 fprintf(stderr, "%s: e - line %d of \"%s\", illegal tag\n", 140 myname, t_lineno, input_file_name); 141 print_pos(t_line, t_cptr); 142 done(1); 143 } 144 145 void 146 illegal_character(const char *c_cptr) 147 { 148 fprintf(stderr, "%s: e - line %d of \"%s\", illegal character\n", 149 myname, lineno, input_file_name); 150 print_pos(line, c_cptr); 151 done(1); 152 } 153 154 void 155 used_reserved(const char *s) 156 { 157 fprintf(stderr, 158 "%s: e - line %d of \"%s\", illegal use of reserved symbol \ 159 %s\n", myname, lineno, input_file_name, s); 160 done(1); 161 } 162 163 void 164 tokenized_start(const char *s) 165 { 166 fprintf(stderr, 167 "%s: e - line %d of \"%s\", the start symbol %s cannot be \ 168 declared to be a token\n", myname, lineno, input_file_name, s); 169 done(1); 170 } 171 172 void 173 retyped_warning(const char *s) 174 { 175 fprintf(stderr, "%s: w - line %d of \"%s\", the type of %s has been \ 176 redeclared\n", myname, lineno, input_file_name, s); 177 } 178 179 void 180 reprec_warning(const char *s) 181 { 182 fprintf(stderr, 183 "%s: w - line %d of \"%s\", the precedence of %s has been \ 184 redeclared\n", myname, lineno, input_file_name, s); 185 } 186 187 void 188 revalued_warning(const char *s) 189 { 190 fprintf(stderr, "%s: w - line %d of \"%s\", the value of %s has been \ 191 redeclared\n", myname, lineno, input_file_name, s); 192 } 193 194 void 195 terminal_start(const char *s) 196 { 197 fprintf(stderr, "%s: e - line %d of \"%s\", the start symbol %s is a \ 198 token\n", myname, lineno, input_file_name, s); 199 done(1); 200 } 201 202 void 203 restarted_warning(void) 204 { 205 fprintf(stderr, "%s: w - line %d of \"%s\", the start symbol has been \ 206 redeclared\n", myname, lineno, input_file_name); 207 } 208 209 void 210 no_grammar(void) 211 { 212 fprintf(stderr, "%s: e - line %d of \"%s\", no grammar has been \ 213 specified\n", myname, lineno, input_file_name); 214 done(1); 215 } 216 217 void 218 terminal_lhs(int s_lineno) 219 { 220 fprintf(stderr, "%s: e - line %d of \"%s\", a token appears on the lhs \ 221 of a production\n", myname, s_lineno, input_file_name); 222 done(1); 223 } 224 225 void 226 prec_redeclared(void) 227 { 228 fprintf(stderr, "%s: w - line %d of \"%s\", conflicting %%prec \ 229 specifiers\n", myname, lineno, input_file_name); 230 } 231 232 void 233 unterminated_action(const struct ainfo *a) 234 { 235 fprintf(stderr, "%s: e - line %d of \"%s\", unterminated action\n", 236 myname, a->a_lineno, input_file_name); 237 print_pos(a->a_line, a->a_cptr); 238 done(1); 239 } 240 241 void 242 dollar_warning(int a_lineno, int i) 243 { 244 fprintf(stderr, "%s: w - line %d of \"%s\", $%d references beyond the \ 245 end of the current rule\n", myname, a_lineno, input_file_name, i); 246 } 247 248 void 249 dollar_error(int a_lineno, const char *a_line, const char *a_cptr) 250 { 251 fprintf(stderr, "%s: e - line %d of \"%s\", illegal $-name\n", 252 myname, a_lineno, input_file_name); 253 print_pos(a_line, a_cptr); 254 done(1); 255 } 256 257 void 258 dislocations_warning(void) 259 { 260 fprintf(stderr, "%s: e - line %d of \"%s\", expected %%locations\n", 261 myname, lineno, input_file_name); 262 } 263 264 void 265 untyped_lhs(void) 266 { 267 fprintf(stderr, "%s: e - line %d of \"%s\", $$ is untyped\n", 268 myname, lineno, input_file_name); 269 done(1); 270 } 271 272 void 273 untyped_rhs(int i, const char *s) 274 { 275 fprintf(stderr, "%s: e - line %d of \"%s\", $%d (%s) is untyped\n", 276 myname, lineno, input_file_name, i, s); 277 done(1); 278 } 279 280 void 281 unknown_rhs(int i) 282 { 283 fprintf(stderr, "%s: e - line %d of \"%s\", $%d is untyped\n", 284 myname, lineno, input_file_name, i); 285 done(1); 286 } 287 288 void 289 default_action_warning(const char *s) 290 { 291 fprintf(stderr, 292 "%s: w - line %d of \"%s\", the default action for %s assigns an \ 293 undefined value to $$\n", 294 myname, lineno, input_file_name, s); 295 } 296 297 void 298 undefined_goal(const char *s) 299 { 300 fprintf(stderr, "%s: e - the start symbol %s is undefined\n", myname, s); 301 done(1); 302 } 303 304 void 305 undefined_symbol_warning(const char *s) 306 { 307 fprintf(stderr, "%s: w - the symbol %s is undefined\n", myname, s); 308 } 309 310 #if ! defined(YYBTYACC) 311 void 312 unsupported_flag_warning(const char *flag, const char *details) 313 { 314 fprintf(stderr, "%s: w - %s flag unsupported, %s\n", 315 myname, flag, details); 316 } 317 #endif 318 319 #if defined(YYBTYACC) 320 void 321 at_warning(int a_lineno, int i) 322 { 323 fprintf(stderr, "%s: w - line %d of \"%s\", @%d references beyond the \ 324 end of the current rule\n", myname, a_lineno, input_file_name, i); 325 } 326 327 void 328 at_error(int a_lineno, const char *a_line, const char *a_cptr) 329 { 330 fprintf(stderr, 331 "%s: e - line %d of \"%s\", illegal @$ or @N reference\n", 332 myname, a_lineno, input_file_name); 333 print_pos(a_line, a_cptr); 334 done(1); 335 } 336 337 void 338 unterminated_arglist(const struct ainfo *a) 339 { 340 fprintf(stderr, 341 "%s: e - line %d of \"%s\", unterminated argument list\n", 342 myname, a->a_lineno, input_file_name); 343 print_pos(a->a_line, a->a_cptr); 344 done(1); 345 } 346 347 void 348 arg_number_disagree_warning(int a_lineno, const char *a_name) 349 { 350 fprintf(stderr, "%s: w - line %d of \"%s\", number of arguments of %s " 351 "doesn't agree with previous declaration\n", 352 myname, a_lineno, input_file_name, a_name); 353 } 354 355 void 356 bad_formals(void) 357 { 358 fprintf(stderr, "%s: e - line %d of \"%s\", bad formal argument list\n", 359 myname, lineno, input_file_name); 360 print_pos(line, cptr); 361 done(1); 362 } 363 364 void 365 arg_type_disagree_warning(int a_lineno, int i, const char *a_name) 366 { 367 fprintf(stderr, "%s: w - line %d of \"%s\", type of argument %d " 368 "to %s doesn't agree with previous declaration\n", 369 myname, a_lineno, input_file_name, i, a_name); 370 } 371 372 void 373 unknown_arg_warning(int d_lineno, const char *dlr_opt, 374 const char *d_arg, 375 const char *d_line, 376 const char *d_cptr) 377 { 378 fprintf(stderr, "%s: w - line %d of \"%s\", unknown argument %s%s\n", 379 myname, d_lineno, input_file_name, dlr_opt, d_arg); 380 print_pos(d_line, d_cptr); 381 } 382 383 void 384 untyped_arg_warning(int a_lineno, const char *dlr_opt, const char *a_name) 385 { 386 fprintf(stderr, "%s: w - line %d of \"%s\", untyped argument %s%s\n", 387 myname, a_lineno, input_file_name, dlr_opt, a_name); 388 } 389 390 void 391 wrong_number_args_warning(const char *which, const char *a_name) 392 { 393 fprintf(stderr, 394 "%s: w - line %d of \"%s\", wrong number of %sarguments for %s\n", 395 myname, lineno, input_file_name, which, a_name); 396 print_pos(line, cptr); 397 } 398 399 void 400 wrong_type_for_arg_warning(int i, const char *a_name) 401 { 402 fprintf(stderr, 403 "%s: w - line %d of \"%s\", wrong type for default argument %d to %s\n", 404 myname, lineno, input_file_name, i, a_name); 405 print_pos(line, cptr); 406 } 407 408 void 409 start_requires_args(const char *a_name) 410 { 411 fprintf(stderr, 412 "%s: w - line %d of \"%s\", start symbol %s requires arguments\n", 413 myname, 0, input_file_name, a_name); 414 415 } 416 417 void 418 destructor_redeclared_warning(const struct ainfo *a) 419 { 420 fprintf(stderr, "%s: w - line %d of \"%s\", destructor redeclared\n", 421 myname, a->a_lineno, input_file_name); 422 print_pos(a->a_line, a->a_cptr); 423 } 424 #endif 425