home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnubison.zip / main.c < prev    next >
C/C++ Source or Header  |  1996-06-20  |  5KB  |  239 lines

  1. /* Top level entry point of bison,
  2.    Copyright (C) 1984, 1986, 1989, 1992, 1995 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  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.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "system.h"
  23. #include "machine.h"    /* for MAXSHORT */
  24.  
  25. extern    int lineno;
  26. extern    int verboseflag;
  27.  
  28. /* Nonzero means failure has been detected; don't write a parser file.  */
  29. int failure;
  30.  
  31. /* The name this program was run with, for messages.  */
  32. char *program_name;
  33.  
  34. extern void getargs(), openfiles(), reader(), reduce_grammar();
  35. extern void set_derives(), set_nullable(), generate_states();
  36. extern void lalr(), initialize_conflicts(), verbose(), terse();
  37. extern void output(), done();
  38.  
  39.  
  40. /* VMS complained about using `int'.  */
  41.  
  42. int
  43. main(argc, argv)
  44.      int argc;
  45.      char *argv[];
  46. {
  47.   program_name = argv[0];
  48.   failure = 0;
  49.   lineno = 0;
  50.   getargs(argc, argv);
  51.   openfiles();
  52.  
  53.   /* read the input.  Copy some parts of it to fguard, faction, ftable and fattrs.
  54.      In file reader.c.
  55.      The other parts are recorded in the grammar; see gram.h.  */
  56.   reader();
  57.   if (failure)
  58.     done(failure);
  59.  
  60.   /* find useless nonterminals and productions and reduce the grammar.  In
  61.      file reduce.c */
  62.   reduce_grammar();
  63.  
  64.   /* record other info about the grammar.  In files derives and nullable.  */
  65.   set_derives();
  66.   set_nullable();
  67.  
  68.   /* convert to nondeterministic finite state machine.  In file LR0.
  69.      See state.h for more info.  */
  70.   generate_states();
  71.  
  72.   /* make it deterministic.  In file lalr.  */
  73.   lalr();
  74.  
  75.   /* Find and record any conflicts: places where one token of lookahead is not
  76.      enough to disambiguate the parsing.  In file conflicts.
  77.      Also resolve s/r conflicts based on precedence declarations.  */
  78.   initialize_conflicts();
  79.  
  80.   /* print information about results, if requested.  In file print. */
  81.   if (verboseflag)
  82.     verbose();
  83.   else
  84.     terse();
  85.  
  86.   /* output the tables and the parser to ftable.  In file output. */
  87.   output();
  88.   done(failure);
  89. }
  90.  
  91. /* functions to report errors which prevent a parser from being generated */
  92.  
  93.  
  94. /* Return a string containing a printable version of C:
  95.    either C itself, or the corresponding \DDD code.  */
  96.  
  97. char *
  98. printable_version(c)
  99.      char c;
  100. {
  101.   static char buf[10];
  102.   if (c < ' ' || c >= '\177')
  103.     sprintf(buf, "\\%o", c);
  104.   else
  105.     {
  106.       buf[0] = c;
  107.       buf[1] = '\0';
  108.     }
  109.   return buf;
  110. }
  111.  
  112. /* Generate a string from the integer I.
  113.    Return a ptr to internal memory containing the string.  */
  114.  
  115. char *
  116. int_to_string(i)
  117.      int i;
  118. {
  119.   static char buf[20];
  120.   sprintf(buf, "%d", i);
  121.   return buf;
  122. }
  123.  
  124. /* Print the message S for a fatal error.  */
  125.  
  126. void
  127. fatal(s)
  128.      char *s;
  129. {
  130.   extern char *infile;
  131.  
  132.   if (infile == 0)
  133.     fprintf(stderr, "\nfatal error: %s\n", s);
  134.   else
  135.     fprintf(stderr, "\n\"%s\", line %d: %s\n", infile, lineno, s);
  136.   done(1);
  137. }
  138.  
  139.  
  140. /* Print a message for a fatal error.  Use FMT to construct the message
  141.    and incorporate string X1.  */
  142.  
  143. void
  144. fatals(fmt, x1)
  145.      char *fmt, *x1;
  146. {
  147.   char buffer[200];
  148.   sprintf(buffer, fmt, x1);
  149.   fatal(buffer);
  150. }
  151.  
  152. /* Print a warning message S.  */
  153.  
  154. void
  155. warn(s)
  156.      char *s;
  157. {
  158.   extern char *infile;
  159.  
  160.   if (infile == 0)
  161.     fprintf(stderr, "error: %s\n", s);
  162.   else
  163.     fprintf(stderr, "(\"%s\", line %d) error: %s\n", 
  164.         infile, lineno, s);
  165.  
  166.   failure = 1;
  167. }
  168.  
  169. /* Print a warning message containing the string for the integer X1.
  170.    The message is given by the format FMT.  */
  171.  
  172. void
  173. warni(fmt, x1)
  174.      char *fmt;
  175.      int x1;
  176. {
  177.   char buffer[200];
  178.   sprintf(buffer, fmt, x1);
  179.   warn(buffer);
  180. }
  181.  
  182. /* Print a warning message containing the string X1.
  183.    The message is given by the format FMT.  */
  184.  
  185. void
  186. warns(fmt, x1)
  187.      char *fmt, *x1;
  188. {
  189.   char buffer[200];
  190.   sprintf(buffer, fmt, x1);
  191.   warn(buffer);
  192. }
  193.  
  194. /* Print a warning message containing the two strings X1 and X2.
  195.     The message is given by the format FMT.  */
  196.  
  197. void
  198. warnss(fmt, x1, x2)
  199.      char *fmt, *x1, *x2;
  200. {
  201.   char buffer[200];
  202.   sprintf(buffer, fmt, x1, x2);
  203.   warn(buffer);
  204. }
  205.  
  206. /* Print a warning message containing the 3 strings X1, X2, X3.
  207.    The message is given by the format FMT.  */
  208.  
  209. void
  210. warnsss(fmt, x1, x2, x3)
  211.      char *fmt, *x1, *x2, *x3;
  212. {
  213.   char buffer[200];
  214.   sprintf(buffer, fmt, x1, x2, x3);
  215.   warn(buffer);
  216. }
  217.  
  218. /* Print a message for the fatal occurence of more than MAXSHORT
  219.    instances of whatever is denoted by the string S.  */
  220.  
  221. void
  222. toomany(s)
  223.      char *s;
  224. {
  225.   char buffer[200];
  226.   sprintf(buffer, "limit of %d exceeded, too many %s", MAXSHORT, s);
  227.   fatal(buffer);
  228. }
  229.  
  230. /* Abort for an internal error denoted by string S.  */
  231.  
  232. void
  233. berror(s)
  234.      char *s;
  235. {
  236.   fprintf(stderr, "internal error, %s\n", s);
  237.   abort();
  238. }
  239.