error.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * routines for printing error messages
  3. */
  4. #include "defs.h"
  5. #include <stdarg.h>
  6. extern FILE *inc_file;
  7. extern char inc_file_name[];
  8. void FileError(char *fmt, ...);
  9. /*
  10. * VM: print error message with file coordinates.
  11. * Do it in style acceptable to emacs.
  12. */
  13. void FileError(char *fmt, ...) {
  14. va_list args;
  15. fprintf(stderr, "%s:%d: ", (inc_file?inc_file_name:input_file_name), lineno);
  16. va_start(args, fmt);
  17. vfprintf(stderr, fmt, args);
  18. va_end(args);
  19. fprintf(stderr, "\n");
  20. }
  21. void fatal(char *msg)
  22. {
  23. fprintf(stderr, "fatal - %s\n", msg);
  24. done(2);
  25. }
  26. void no_space()
  27. {
  28. fprintf(stderr, "fatal - out of space\n");
  29. done(2);
  30. }
  31. void open_error(char *filename)
  32. {
  33. fprintf(stderr, "fatal - cannot open \"%s\"\n", filename);
  34. done(2);
  35. }
  36. void unexpected_EOF()
  37. {
  38. FileError("unexpected end-of-file");
  39. done(1);
  40. }
  41. void print_pos(char *st_line, char *st_cptr)
  42. {
  43. register char *s;
  44. if (st_line == 0) return;
  45. for (s = st_line; *s != '\n'; ++s)
  46. {
  47. if (isprint(*s) || *s == '\t')
  48. putc(*s, stderr);
  49. else
  50. putc('?', stderr);
  51. }
  52. putc('\n', stderr);
  53. for (s = st_line; s < st_cptr; ++s)
  54. {
  55. if (*s == '\t')
  56. putc('\t', stderr);
  57. else
  58. putc(' ', stderr);
  59. }
  60. putc('^', stderr);
  61. putc('\n', stderr);
  62. }
  63. int read_errs = 0;
  64. void error(int lineno, char *line, char *cptr, char *msg, ...)
  65. {
  66. char sbuf[512];
  67. va_list args;
  68. va_start(args, msg);
  69. vsprintf(sbuf, msg, args);
  70. va_end(args);
  71. FileError("%s", sbuf);
  72. read_errs++;
  73. }
  74. void syntax_error(int lineno, char *line, char *cptr) {
  75. error(lineno, line, cptr, "syntax error");
  76. exit(1);
  77. }
  78. void unterminated_comment(int lineno, char *line, char *cptr) {
  79. error(lineno, line, cptr, "unmatched /*");
  80. exit(1);
  81. }
  82. void unterminated_string(int lineno, char *line, char *cptr) {
  83. error(lineno, line, cptr, "unterminated string");
  84. exit(1);
  85. }
  86. void unterminated_text(int lineno, char *line, char *cptr) {
  87. error(lineno, line, cptr, "unmatched %%{");
  88. exit(1);
  89. }
  90. void unterminated_union(int lineno, char *line, char *cptr) {
  91. error(lineno, line, cptr, "unterminated %%union");
  92. exit(1);
  93. }
  94. void over_unionized(char *cptr) {
  95. error(lineno, line, cptr, "too many %%union declarations");
  96. exit(1);
  97. }
  98. void illegal_tag(int lineno, char *line, char *cptr) {
  99. error(lineno, line, cptr, "illegal tag");
  100. }
  101. void illegal_character(char *cptr) {
  102. error(lineno, line, cptr, "illegal character");
  103. }
  104. void used_reserved(char *s) {
  105. error(lineno, 0, 0, "illegal use of reserved symbol %s", s);
  106. }
  107. void tokenized_start(char *s) {
  108. error(lineno, 0, 0, "the start symbol %s cannot be declared to be a token", s);
  109. }
  110. void retyped_warning(char *s) {
  111. FileError("the type of %s has been redeclared", s);
  112. }
  113. void reprec_warning(char *s) {
  114. FileError("the precedence of %s has been redeclared", s);
  115. }
  116. void revalued_warning(char *s) {
  117. FileError("the value of %s has been redeclared", s);
  118. }
  119. void terminal_start(char *s) {
  120. error(lineno, 0, 0, "the start symbol %s is a token", s);
  121. }
  122. void restarted_warning() {
  123. FileError("the start symbol has been redeclared");
  124. }
  125. void no_grammar() {
  126. error(lineno, 0, 0, "no grammar has been specified");
  127. }
  128. void terminal_lhs(int lineno) {
  129. error(lineno, 0, 0, "a token appears on the lhs of a production");
  130. }
  131. void prec_redeclared() {
  132. error(lineno, 0, 0, "conflicting %%prec specifiers");
  133. }
  134. void unterminated_action(int lineno, char *line, char *cptr) {
  135. error(lineno, line, cptr, "unterminated action");
  136. }
  137. void unterminated_arglist(int lineno, char *line, char *cptr) {
  138. error(lineno, line, cptr, "unterminated argument list");
  139. }
  140. void bad_formals() {
  141. error(lineno, 0, 0, "bad formal argument list");
  142. }
  143. void dollar_warning(int a_lineno, int i) {
  144. int slineno = lineno;
  145. lineno = a_lineno;
  146. FileError("$%d references beyond the end of the current rule", i);
  147. lineno = slineno;
  148. }
  149. void dollar_error(int lineno, char *line, char *cptr) {
  150. error(lineno, line, cptr, "illegal $-name");
  151. }
  152. void untyped_lhs() {
  153. error(lineno, 0, 0, "$$ is untyped");
  154. }
  155. void untyped_rhs(int i, char *s) {
  156. error(lineno, 0, 0, "$%d (%s) is untyped", i, s);
  157. }
  158. void unknown_rhs(int i) {
  159. error(lineno, 0, 0, "$%d is untyped (out of range)", i);
  160. }
  161. void default_action_warning() {
  162. FileError("the default action assigns an undefined value to $$");
  163. }
  164. void undefined_goal(char *s) {
  165. error(lineno, 0, 0, "the start symbol %s is undefined", s);
  166. }
  167. void undefined_symbol_warning(char *s) {
  168. fprintf(stderr, "warning - the symbol %s is undefined\n", s);
  169. }