home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / bison-1.25-src.tgz / tar.out / fsf / bison / main.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  6KB  |  245 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, "fatal error: %s\n", s);
  134.   else
  135.     fprintf(stderr, "\"%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. #ifndef __amigaos__
  167.   /* The ADE contains at least one program (f2c) where warnings are
  168.      generated by the grammar files, so don't abort for now.  I'm not
  169.      even sure why a warnings were changed to be errors, other than
  170.      to try and force people to fix their grammar files. -fnf */
  171.   failure = 1;
  172. #endif
  173. }
  174.  
  175. /* Print a warning message containing the string for the integer X1.
  176.    The message is given by the format FMT.  */
  177.  
  178. void
  179. warni(fmt, x1)
  180.      char *fmt;
  181.      int x1;
  182. {
  183.   char buffer[200];
  184.   sprintf(buffer, fmt, x1);
  185.   warn(buffer);
  186. }
  187.  
  188. /* Print a warning message containing the string X1.
  189.    The message is given by the format FMT.  */
  190.  
  191. void
  192. warns(fmt, x1)
  193.      char *fmt, *x1;
  194. {
  195.   char buffer[200];
  196.   sprintf(buffer, fmt, x1);
  197.   warn(buffer);
  198. }
  199.  
  200. /* Print a warning message containing the two strings X1 and X2.
  201.     The message is given by the format FMT.  */
  202.  
  203. void
  204. warnss(fmt, x1, x2)
  205.      char *fmt, *x1, *x2;
  206. {
  207.   char buffer[200];
  208.   sprintf(buffer, fmt, x1, x2);
  209.   warn(buffer);
  210. }
  211.  
  212. /* Print a warning message containing the 3 strings X1, X2, X3.
  213.    The message is given by the format FMT.  */
  214.  
  215. void
  216. warnsss(fmt, x1, x2, x3)
  217.      char *fmt, *x1, *x2, *x3;
  218. {
  219.   char buffer[200];
  220.   sprintf(buffer, fmt, x1, x2, x3);
  221.   warn(buffer);
  222. }
  223.  
  224. /* Print a message for the fatal occurence of more than MAXSHORT
  225.    instances of whatever is denoted by the string S.  */
  226.  
  227. void
  228. toomany(s)
  229.      char *s;
  230. {
  231.   char buffer[200];
  232.   sprintf(buffer, "limit of %d exceeded, too many %s", MAXSHORT, s);
  233.   fatal(buffer);
  234. }
  235.  
  236. /* Abort for an internal error denoted by string S.  */
  237.  
  238. void
  239. berror(s)
  240.      char *s;
  241. {
  242.   fprintf(stderr, "internal error, %s\n", s);
  243.   abort();
  244. }
  245.