main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* Top level entry point of bison,
  2. Copyright (C) 1984, 1986, 1989, 1992 Free Software Foundation, Inc.
  3. Modified (1992) from bison-1.19 by
  4. Wilfred J. Hansen (wjh+@cmu.edu)
  5. Andrew Consortium, Carnegie Mellon University
  6. This file is part of Bison, the GNU Compiler Compiler.
  7. Bison is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  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. You should have received a copy of the GNU General Public License
  16. along with Bison; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  18. #include <stdio.h>
  19. #include "system.h"
  20. #include "machine.h" /* JF for MAXSHORT */
  21. extern int lineno;
  22. extern int verboseflag;
  23. /* Nonzero means failure has been detected; don't write a parser file. */
  24. int failure;
  25. /* The name this program was run with, for messages. */
  26. char *program_name;
  27. extern void getargs(), openfiles(), reader(), reduce_grammar();
  28. extern void set_derives(), set_nullable(), generate_states();
  29. extern void lalr(), initialize_conflicts(), verbose(), terse();
  30. extern void output(), done();
  31. /* VMS complained about using `int'. */
  32. int
  33. main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37. program_name = argv[0];
  38. failure = 0;
  39. lineno = 0;
  40. getargs(argc, argv);
  41. openfiles();
  42. /* read the input. Copy some parts of it to fguard, faction, ftable and fattrs.
  43. In file reader.c.
  44. The other parts are recorded in the grammar; see gram.h. */
  45. reader();
  46. if (failure)
  47. done(failure);
  48. /* find useless nonterminals and productions and reduce the grammar. In
  49. file reduce.c */
  50. reduce_grammar();
  51. /* record other info about the grammar. In files derives and nullable. */
  52. set_derives();
  53. set_nullable();
  54. /* convert to nondeterministic finite state machine. In file LR0.
  55. See state.h for more info. */
  56. generate_states();
  57. /* make it deterministic. In file lalr. */
  58. lalr();
  59. /* Find and record any conflicts: places where one token of lookahead is not
  60. enough to disambiguate the parsing. In file conflicts.
  61. Currently this does not do anything to resolve them;
  62. the trivial form of conflict resolution that exists is done in output. */
  63. initialize_conflicts();
  64. /* print information about results, if requested. In file print. */
  65. if (verboseflag)
  66. verbose();
  67. else
  68. terse();
  69. /* output the tables and the parser to ftable. In file output. */
  70. output();
  71. done(failure);
  72. }
  73. /* functions to report errors which prevent a parser from being generated */
  74. /* generate a string from an integer. return internal memory containing string */
  75. char *
  76. intToString(i)
  77. int i;
  78. {
  79. static char buf[20];
  80. sprintf(buf, "%d", i);
  81. return buf;
  82. }
  83. void
  84. fatal(s)
  85. char *s;
  86. {
  87. extern char *infile;
  88. if (infile == 0)
  89. fprintf(stderr, "fatal error: %s\n", s);
  90. else
  91. fprintf(stderr, "\"%s\", line %d: %s\n", infile, lineno, s);
  92. done(1);
  93. }
  94. void
  95. fatals(fmt, x1)
  96. char *fmt, *x1;
  97. {
  98. char buffer[200];
  99. sprintf(buffer, fmt, x1);
  100. fatal(buffer);
  101. }
  102. void
  103. warn(s)
  104. char *s;
  105. {
  106. extern char *infile;
  107. if (infile == 0)
  108. fprintf(stderr, "error: %s\n", s);
  109. else
  110. fprintf(stderr, "(\"%s\", line %d) error: %s\n", infile, lineno, s);
  111. }
  112. void
  113. warni(fmt, x1)
  114. char *fmt;
  115. int x1;
  116. {
  117. char buffer[200];
  118. sprintf(buffer, fmt, x1);
  119. warn(buffer);
  120. }
  121. void
  122. warns(fmt, x1)
  123. char *fmt, *x1;
  124. {
  125. char buffer[200];
  126. sprintf(buffer, fmt, x1);
  127. warn(buffer);
  128. }
  129. void
  130. warnss(fmt, x1, x2)
  131. char *fmt, *x1, *x2;
  132. {
  133. char buffer[200];
  134. sprintf(buffer, fmt, x1, x2);
  135. warn(buffer);
  136. }
  137. void
  138. warnsss(fmt, x1, x2, x3)
  139. char *fmt, *x1, *x2, *x3;
  140. {
  141. char buffer[200];
  142. sprintf(buffer, fmt, x1, x2, x3);
  143. warn(buffer);
  144. }
  145. void
  146. toomany(s)
  147. char *s;
  148. {
  149. char buffer[200];
  150. /* JF new msg */
  151. sprintf(buffer, "limit of %d exceeded, too many %s", MAXSHORT, s);
  152. fatal(buffer);
  153. }
  154. void
  155. berror(s)
  156. char *s;
  157. {
  158. fprintf(stderr, "internal error, %s\n", s);
  159. abort();
  160. }