home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / program / language / bison / Old_Bison / C / Reader < prev    next >
Encoding:
Text File  |  1990-05-24  |  29.1 KB  |  1,449 lines

  1. /* Input parser for bison
  2.    Copyright (C) 1984, 1986, 1989 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 1, 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. /* read in the grammar specification and record it in the format described in gram.h.
  22.   All actions are copied into the faction file forming a switch statement to
  23.   decide which action to execute.
  24.  
  25.   The entry point is reader().  */
  26.  
  27. #include <stdio.h>
  28. #include <ctype.h>
  29. #include "system.h"
  30. #include "files.h"
  31. #include "new.h"
  32. #include "symtab.h"
  33. #include "lex.h"
  34. #include "gram.h"
  35.  
  36.  
  37. #define    LTYPESTR    "\n#ifndef YYLTYPE\ntypedef\n  struct yyltype\n\
  38.     {\n      int timestamp;\n      int first_line;\n      int first_column;\
  39. \n      int last_line;\n      int last_column;\n      char *text;\n   }\n\
  40.   yyltype;\n\n#define YYLTYPE yyltype\n#endif\n\n"
  41.  
  42. /* Number of slots allocated (but not necessarily used yet) in `rline'  */
  43. int rline_allocated;
  44.  
  45. extern int definesflag;
  46. extern int nolinesflag;
  47. extern bucket *symval;
  48. extern int numval;
  49. extern int failure;
  50. extern int expected_conflicts;
  51.  
  52.  
  53. typedef struct symbol_list
  54.   {
  55.     struct symbol_list *next;
  56.     bucket *sym;
  57.     bucket *ruleprec;
  58.   }
  59. symbol_list;
  60.  
  61. static char *get_type_name(int, symbol_list *);
  62. static void copy_action(symbol_list *, int);
  63.  
  64. int lineno;
  65. symbol_list *grammar;
  66. int start_flag;
  67. bucket *startval;
  68. char **tags;
  69.  
  70. static int typed;  /* nonzero if %union has been seen.  */
  71.  
  72. static int lastprec;  /* incremented for each %left, %right or %nonassoc seen */
  73.  
  74. static int gensym_count;  /* incremented for each generated symbol */
  75.  
  76. static bucket *errtoken;
  77.  
  78. /* Nonzero if any action uses the @n construct.  */
  79. static int yylsp_needed;
  80.  
  81. void reader(void)
  82. {
  83.   start_flag = 0;
  84.   startval = NULL;  /* start symbol not specified yet. */
  85.  
  86.   /* Nowadays translations is always set to 1,
  87.      since we give `error' a user-token-number
  88.      to satisfy the Posix demand for YYERRCODE==256.  */
  89.   translations = 1;
  90.  
  91.   nsyms = 1;
  92.   nvars = 0;
  93.   nrules = 0;
  94.   nitems = 0;
  95.   rline_allocated = 10;
  96.   rline = NEW2(rline_allocated, short);
  97.  
  98.   typed = 0;
  99.   lastprec = 0;
  100.  
  101.   gensym_count = 0;
  102.  
  103.   pure_parser = 0;
  104.   yylsp_needed = 0;
  105.  
  106.   grammar = NULL;
  107.  
  108.   init_lex();
  109.   lineno = 1;
  110.  
  111.   /* initialize the symbol table.  */
  112.   tabinit();
  113.   /* construct the error token */
  114.   errtoken = getsym("error");
  115.   errtoken->class = STOKEN;
  116.   errtoken->user_token_number = 256; /* Value specified by posix.  */
  117.   /* construct a token that represents all undefined literal tokens. */
  118.   /* it is always token number 2.  */
  119.   getsym("$illegal.")->class = STOKEN;
  120.   /* Read the declaration section.  Copy %{ ... %} groups to ftable and fdefines file.
  121.      Also notice any %token, %left, etc. found there.  */
  122.   fprintf(ftable, "/*  A Bison parser, made from %s  */\n\n", infile);
  123.   if (fdefines)
  124.     fprintf(fdefines, "/*  A Bison parser, made from %s  */\n\n", infile);
  125.   read_declarations();
  126.   /* output the definition of YYLTYPE into the fattrs and fdefines files.  */
  127.   /* fattrs winds up in the .tab.c file, before bison.simple.  */
  128.   fprintf(fattrs, LTYPESTR);
  129.   /* start writing the action file, if it is needed.  */
  130.   output_headers();
  131.   /* read in the grammar, build grammar in list form.  write out actions.  */
  132.   readgram();
  133.   /* Now we know whether we need the line-number stack.
  134.      If we do, write its type into the .tab.h file.  */
  135.   if (yylsp_needed)
  136.     {
  137.       if (fdefines)
  138.     fprintf(fdefines, LTYPESTR);
  139.     }
  140.   /* write closing delimiters for actions.  */
  141.   output_trailers();
  142.   if (yylsp_needed)
  143.     fprintf(ftable, "#define YYLSP_NEEDED\n\n");
  144.   /* assign the symbols their symbol numbers.
  145.      Write #defines for the token symbols into fdefines if requested.  */
  146.   packsymbols();
  147.   /* convert the grammar into the format described in gram.h.  */
  148.   packgram();
  149.   /* free the symbol table data structure
  150.      since symbols are now all referred to by symbol number.  */
  151.   free_symtab();
  152. }
  153.  
  154.  
  155.  
  156. /* read from finput until %% is seen.  Discard the %%.
  157. Handle any % declarations,
  158. and copy the contents of any %{ ... %} groups to fattrs.  */
  159.  
  160. void read_declarations (void)
  161. {
  162.   register int c;
  163.   register int tok;
  164.  
  165.   for (;;)
  166.     {
  167.       c = skip_white_space();
  168.  
  169.       if (c == '%')
  170.     {
  171.       tok = parse_percent_token();
  172.  
  173.       switch (tok)
  174.         {
  175.         case TWO_PERCENTS:
  176.           return;
  177.  
  178.         case PERCENT_LEFT_CURLY:
  179.           copy_definition();
  180.           break;
  181.  
  182.         case TOKEN:
  183.           parse_token_decl (STOKEN, SNTERM);
  184.           break;
  185.     
  186.         case NTERM:
  187.           parse_token_decl (SNTERM, STOKEN);
  188.           break;
  189.     
  190.         case TYPE:
  191.           parse_type_decl();
  192.           break;
  193.     
  194.         case START:
  195.           parse_start_decl();
  196.           break;
  197.     
  198.         case UNION:
  199.           parse_union_decl();
  200.           break;
  201.     
  202.         case EXPECT:
  203.           parse_expect_decl();
  204.           break;
  205.     
  206.         case LEFT:
  207.           parse_assoc_decl(LEFT_ASSOC);
  208.           break;
  209.  
  210.         case RIGHT:
  211.           parse_assoc_decl(RIGHT_ASSOC);
  212.           break;
  213.  
  214.         case NONASSOC:
  215.           parse_assoc_decl(NON_ASSOC);
  216.           break;
  217.  
  218.         case PURE_PARSER:
  219.           pure_parser = 1;
  220.           break;
  221.  
  222.         default:
  223.           fatal("junk after `%%' in definition section");
  224.         }
  225.     }
  226.       else if (c == EOF)
  227.         fatal("no input grammar");
  228.       else /* JF changed msg */
  229.         fatals("Unrecognized char `%c' in declaration section",c);
  230.  
  231.     }
  232. }
  233.  
  234.  
  235. /* copy the contents of a %{ ... %} into the definitions file.
  236. The %{ has already been read.  Return after reading the %}.  */
  237.  
  238. void copy_definition (void)
  239. {
  240.   register int c;
  241.   register int match;
  242.   register int ended;
  243.   register int after_percent;  /* -1 while reading a character if prev char was % */
  244.  
  245.   if (!nolinesflag)
  246.     fprintf(fattrs, "#line %d \"%s\"\n", lineno, infile);
  247.  
  248.   after_percent = 0;
  249.  
  250.   c = getc(finput);
  251.  
  252.   for (;;)
  253.     {
  254.       switch (c)
  255.     {
  256.     case '\n':
  257.       putc(c, fattrs);
  258.       lineno++;
  259.       break;
  260.  
  261.     case '%':
  262.           after_percent = -1;
  263.       break;
  264.           
  265.     case '\'':
  266.     case '"':
  267.       match = c;
  268.       putc(c, fattrs);
  269.       c = getc(finput);
  270.  
  271.       while (c != match)
  272.         {
  273.           if (c == EOF || c == '\n')
  274.         fatal("unterminated string");
  275.  
  276.           putc(c, fattrs);
  277.           
  278.           if (c == '\\')
  279.         {
  280.           c = getc(finput);
  281.           if (c == EOF)
  282.             fatal("unterminated string");
  283.           putc(c, fattrs);
  284.           if (c == '\n')
  285.             lineno++;
  286.         }
  287.  
  288.           c = getc(finput);
  289.         }
  290.  
  291.       putc(c, fattrs);
  292.       break;
  293.  
  294.     case '/':
  295.       putc(c, fattrs);
  296.       c = getc(finput);
  297.       if (c != '*')
  298.         continue;
  299.  
  300.       putc(c, fattrs);
  301.       c = getc(finput);
  302.  
  303.       ended = 0;
  304.       while (!ended)
  305.         {
  306.           if (c == '*')
  307.         {
  308.           while (c == '*')
  309.             {
  310.               putc(c, fattrs);
  311.               c = getc(finput);
  312.             }
  313.  
  314.           if (c == '/')
  315.             {
  316.               putc(c, fattrs);
  317.               ended = 1;
  318.             }
  319.         }
  320.           else if (c == '\n')
  321.         {
  322.           lineno++;
  323.           putc(c, fattrs);
  324.           c = getc(finput);
  325.         }
  326.           else if (c == EOF)
  327.         fatal("unterminated comment in `%%{' definition");
  328.           else
  329.         {
  330.           putc(c, fattrs);
  331.           c = getc(finput);
  332.         }
  333.         }
  334.  
  335.       break;
  336.  
  337.     case EOF:
  338.       fatal("unterminated `%%{' definition");
  339.  
  340.     default:
  341.       putc(c, fattrs);
  342.     }
  343.  
  344.       c = getc(finput);
  345.  
  346.       if (after_percent)
  347.     {
  348.       if (c == '}')
  349.         return;
  350.       putc('%', fattrs);
  351.     }
  352.       after_percent = 0;
  353.  
  354.     }
  355.  
  356. }
  357.  
  358.  
  359.  
  360. /* parse what comes after %token or %nterm.
  361. For %token, what_is is STOKEN and what_is_not is SNTERM.
  362. For %nterm, the arguments are reversed.  */
  363.  
  364. void parse_token_decl (int what_is, int what_is_not)
  365. {
  366. /*   register int start_lineno; JF */
  367.   register int token = 0;
  368.   register int prev;
  369.   register char *typename = 0;
  370.   int k;
  371.   extern char token_buffer[];
  372.  
  373.   for (;;)
  374.     {
  375.       if(ungetc(skip_white_space(), finput) == '%')
  376.     return;
  377.  
  378.       prev = token;
  379.  
  380.       token = lex();
  381.       if (token == COMMA)
  382.     continue;
  383.       if (token == TYPENAME)
  384.     {
  385.       k = strlen(token_buffer);
  386.       if (typename) free (typename);
  387.       typename = NEW2(k + 1, char);
  388.       strcpy(typename, token_buffer);
  389.     }
  390.       else if (token == IDENTIFIER)
  391.     {
  392.       if (symval->class == what_is_not)
  393.         fatals("symbol %s redefined", symval->tag);
  394.       symval->class = what_is;
  395.       if (what_is == SNTERM)
  396.         symval->value = nvars++;
  397.  
  398.       if (typename)
  399.         {
  400.           if (symval->type_name == NULL)
  401.         symval->type_name = typename;
  402.           else
  403.         fatals("type redeclaration for %s", symval->tag);
  404.         }
  405.     }
  406.       else if (prev == IDENTIFIER && token == NUMBER)
  407.         {
  408.       symval->user_token_number = numval;
  409.       translations = 1;
  410.         }
  411.       else
  412.     fatal("invalid text in %%token or %%nterm declaration");
  413.     }
  414.  
  415. }
  416.  
  417.  
  418.  
  419. /* parse what comes after %start */
  420.  
  421. void parse_start_decl (void)
  422. {
  423.   if (start_flag)
  424.     fatal("multiple %%start declarations");
  425.   start_flag = 1;
  426.   if (lex() != IDENTIFIER)
  427.     fatal("invalid %%start declaration");
  428.   startval = symval;
  429. }
  430.  
  431.  
  432.  
  433. /* read in a %type declaration and record its information for get_type_name to access */
  434.  
  435. void parse_type_decl (void)
  436. {
  437.   register int k;
  438.   register char *name;
  439.  
  440.   extern char token_buffer[];
  441.  
  442.   if (lex() != TYPENAME)
  443.     fatal("ill-formed %%type declaration");
  444.  
  445.   k = strlen(token_buffer);
  446.   name = NEW2(k + 1, char);
  447.   strcpy(name, token_buffer);
  448.  
  449.  
  450.   for (;;)
  451.     {
  452.       register int t;
  453.  
  454.       if(ungetc(skip_white_space(), finput) == '%')
  455.     return;
  456.  
  457.       t = lex();
  458.  
  459.       switch (t)
  460.     {
  461.  
  462.     case COMMA:
  463.       break;
  464.  
  465.     case IDENTIFIER:
  466.       if (symval->type_name == NULL)
  467.         symval->type_name = name;
  468.       else
  469.         fatals("type redeclaration for %s", symval->tag);
  470.  
  471.       break;
  472.  
  473.     default:
  474.       fatal("invalid %%type declaration");
  475.     }
  476.     }
  477. }
  478.  
  479.  
  480.  
  481. /* read in a %left, %right or %nonassoc declaration and record its information.  */
  482. /* assoc is either LEFT_ASSOC, RIGHT_ASSOC or NON_ASSOC.  */
  483.  
  484. void parse_assoc_decl (int assoc)
  485. {
  486.   register int k;
  487.   register char *name = NULL;
  488.   register int prev = 0;    /* JF added = 0 to keep lint happy */
  489.  
  490.   extern char token_buffer[];
  491.  
  492.   lastprec++;  /* assign a new precedence level.  */
  493.  
  494.   for (;;)
  495.     {
  496.       register int t;
  497.  
  498.       if(ungetc(skip_white_space(), finput) == '%')
  499.     return;
  500.  
  501.       t = lex();
  502.  
  503.       switch (t)
  504.     {
  505.  
  506.     case TYPENAME:
  507.       k = strlen(token_buffer);
  508.       name = NEW2(k + 1, char);
  509.       strcpy(name, token_buffer);
  510.       break;
  511.  
  512.     case COMMA:
  513.       break;
  514.  
  515.     case IDENTIFIER:
  516.       symval->prec = lastprec;
  517.       symval->assoc = assoc;
  518.       if (symval->class == SNTERM)
  519.         fatals("symbol %s redefined", symval->tag);
  520.       symval->class = STOKEN;
  521.       if (name)
  522.         { /* record the type, if one is specified */
  523.           if (symval->type_name == NULL)
  524.         symval->type_name = name;
  525.           else
  526.         fatals("type redeclaration for %s", symval->tag);
  527.         }
  528.       break;
  529.  
  530.     case NUMBER:
  531.       if (prev == IDENTIFIER)
  532.             {
  533.           symval->user_token_number = numval;
  534.           translations = 1;
  535.             }
  536.           else      
  537.         fatal("invalid text in association declaration");
  538.       break;
  539.  
  540.     case SEMICOLON:
  541.       return;
  542.  
  543.     default:
  544.       fatal("malformatted association declaration");
  545.     }
  546.  
  547.       prev = t;
  548.  
  549.     }
  550. }
  551.  
  552.  
  553.  
  554. /* copy the union declaration into fattrs (and fdefines),
  555.    where it is made into the
  556.    definition of YYSTYPE, the type of elements of the parser value stack.  */
  557.  
  558. void parse_union_decl(void)
  559. {
  560.   register int c;
  561.   register int count;
  562.   register int in_comment;
  563.  
  564.   if (typed)
  565.     fatal("multiple %%union declarations");
  566.  
  567.   typed = 1;
  568.  
  569.   if (!nolinesflag)
  570.     fprintf(fattrs, "\n#line %d \"%s\"\n", lineno, infile);
  571.   else
  572.     fprintf(fattrs, "\n");
  573.  
  574.   fprintf(fattrs, "typedef union");
  575.   if (fdefines)
  576.     fprintf(fdefines, "typedef union");
  577.  
  578.   count = 0;
  579.   in_comment = 0;
  580.  
  581.   c = getc(finput);
  582.  
  583.   while (c != EOF)
  584.     {
  585.       putc(c, fattrs);
  586.       if (fdefines)
  587.     putc(c, fdefines);
  588.  
  589.       switch (c)
  590.     {
  591.     case '\n':
  592.       lineno++;
  593.       break;
  594.  
  595.     case '/':
  596.       c = getc(finput);
  597.       if (c != '*')
  598.         ungetc(c, finput);
  599.       else
  600.         {
  601.           putc('*', fattrs);
  602.           if (fdefines)
  603.         putc('*', fdefines);
  604.           c = getc(finput);
  605.           in_comment = 1;
  606.           while (in_comment)
  607.         {
  608.           if (c == EOF)
  609.             fatal("unterminated comment");
  610.  
  611.           putc(c, fattrs);
  612.           if (fdefines)
  613.             putc(c, fdefines);
  614.           if (c == '*')
  615.             {
  616.               c = getc(finput);
  617.               if (c == '/')
  618.             {
  619.               putc('/', fattrs);
  620.               if (fdefines)
  621.                 putc('/', fdefines);
  622.               in_comment = 0;
  623.             }
  624.             }
  625.           else
  626.             c = getc(finput);
  627.         }
  628.         }
  629.       break;
  630.  
  631.  
  632.     case '{':
  633.       count++;
  634.       break;
  635.  
  636.     case '}':
  637.       count--;
  638.       if (count == 0)
  639.         {
  640.           fprintf(fattrs, " YYSTYPE;\n\n");
  641.           if (fdefines)
  642.         fprintf(fdefines, " YYSTYPE;\n\n");
  643.           /* JF don't choke on trailing semi */
  644.           c=skip_white_space();
  645.           if(c!=';') ungetc(c,finput);
  646.           return;
  647.         }
  648.     }
  649.  
  650.       c = getc(finput);
  651.     }
  652. }
  653.  
  654. /* parse the declaration %expect N which says to expect N
  655.    shift-reduce conflicts.  */
  656.  
  657. void parse_expect_decl(void)
  658. {
  659.   register int c;
  660.   register int count;
  661.   char buffer[20];
  662.  
  663.   c = getc(finput);
  664.   while (c == ' ' || c == '\t')
  665.     c = getc(finput);
  666.  
  667.   count = 0;
  668.   while (c >= '0' && c <= '9')
  669.     {
  670.       if (count < 20)
  671.     buffer[count++] = c;
  672.       c = getc(finput);
  673.     }
  674.  
  675.   ungetc (c, finput);
  676.  
  677.   expected_conflicts = atoi (buffer);
  678. }
  679.  
  680. /* that's all of parsing the declaration section */
  681.  
  682.  
  683. /* Get the data type (alternative in the union) of the value for symbol n in rule rule.  */
  684.  
  685. static char *get_type_name(int n, symbol_list *rule)
  686. {
  687.   static char *msg = "invalid $ value";
  688.  
  689.   register int i;
  690.   register symbol_list *rp;
  691.  
  692.   if (n < 0)
  693.     fatal(msg);
  694.  
  695.   rp = rule;
  696.   i = 0;
  697.  
  698.   while (i < n)
  699.     {
  700.       rp = rp->next;
  701.       if (rp == NULL || rp->sym == NULL)
  702.     fatal(msg);
  703.       i++;
  704.     }
  705.  
  706.   return (rp->sym->type_name);
  707. }
  708.  
  709.  
  710. /* Assuming that a { has just been seen, copy everything up to the matching }
  711. into the actions file.
  712. stack_offset is the number of values in the current rule so far,
  713. which says where to find $0 with respect to the top of the stack.  */
  714.  
  715. static void copy_action(symbol_list *rule, int stack_offset)
  716. {
  717.   register int c;
  718.   register int n;
  719.   register int count;
  720.   register int match;
  721.   register int ended;
  722.   register char *type_name;
  723.   extern char token_buffer[];
  724.  
  725.   fprintf(faction, "\ncase %d:\n", nrules);
  726.   if (!nolinesflag)
  727.     fprintf(faction, "#line %d \"%s\"\n", lineno, infile);
  728.   putc('{', faction);
  729.  
  730.   count = 1;
  731.   c = getc(finput);
  732.  
  733.   while (count > 0)
  734.     {
  735.       while (c != '}')
  736.         {
  737.           switch (c)
  738.         {
  739.         case '\n':
  740.           putc(c, faction);
  741.           lineno++;
  742.           break;
  743.  
  744.         case '{':
  745.           putc(c, faction);
  746.           count++;
  747.           break;
  748.  
  749.         case '\'':
  750.         case '"':
  751.           match = c;
  752.           putc(c, faction);
  753.           c = getc(finput);
  754.  
  755.           while (c != match)
  756.         {
  757.           if (c == EOF || c == '\n')
  758.             fatal("unterminated string");
  759.  
  760.           putc(c, faction);
  761.  
  762.           if (c == '\\')
  763.             {
  764.               c = getc(finput);
  765.               if (c == EOF)
  766.             fatal("unterminated string");
  767.               putc(c, faction);
  768.               if (c == '\n')
  769.             lineno++;
  770.             }
  771.  
  772.           c = getc(finput);
  773.         }
  774.  
  775.           putc(c, faction);
  776.           break;
  777.  
  778.         case '/':
  779.           putc(c, faction);
  780.           c = getc(finput);
  781.           if (c != '*')
  782.         continue;
  783.  
  784.           putc(c, faction);
  785.           c = getc(finput);
  786.  
  787.           ended = 0;
  788.           while (!ended)
  789.         {
  790.           if (c == '*')
  791.             {
  792.               while (c == '*')
  793.                 {
  794.               putc(c, faction);
  795.               c = getc(finput);
  796.             }
  797.  
  798.               if (c == '/')
  799.             {
  800.               putc(c, faction);
  801.               ended = 1;
  802.             }
  803.             }
  804.           else if (c == '\n')
  805.             {
  806.               lineno++;
  807.               putc(c, faction);
  808.               c = getc(finput);
  809.             }
  810.           else if (c == EOF)
  811.             fatal("unterminated comment");
  812.           else
  813.             {
  814.               putc(c, faction);
  815.               c = getc(finput);
  816.             }
  817.         }
  818.  
  819.           break;
  820.  
  821.         case '$':
  822.           c = getc(finput);
  823.           type_name = NULL;
  824.  
  825.           if (c == '<')
  826.         {
  827.           register char *cp = token_buffer;
  828.  
  829.           while ((c = getc(finput)) != '>' && c > 0)
  830.             *cp++ = c;
  831.           *cp = 0;
  832.           type_name = token_buffer;
  833.  
  834.           c = getc(finput);
  835.         }
  836.           if (c == '$')
  837.         {
  838.           fprintf(faction, "yyval");
  839.           if (!type_name) type_name = get_type_name(0, rule);
  840.           if (type_name)
  841.             fprintf(faction, ".%s", type_name);
  842.           if(!type_name && typed)    /* JF */
  843.             fprintf(stderr,"%s:%d:  warning:  $$ of '%s' has no declared type.\n",infile,lineno,rule->sym->tag);
  844.         }
  845.           else if (isdigit(c) || c == '-')
  846.         {
  847.           ungetc (c, finput);
  848.           n = read_signed_integer(finput);
  849.           c = getc(finput);
  850.  
  851.           if (!type_name && n > 0)
  852.             type_name = get_type_name(n, rule);
  853.  
  854.           fprintf(faction, "yyvsp[%d]", n - stack_offset);
  855.           if (type_name)
  856.             fprintf(faction, ".%s", type_name);
  857.           if(!type_name && typed)    /* JF */
  858.             fprintf(stderr,"%s:%d:  warning:  $%d of '%s' has no declared type.\n",infile,lineno,n,rule->sym->tag);
  859.           continue;
  860.         }
  861.           else
  862.         fatals("$%c is invalid",c);    /* JF changed format */
  863.  
  864.           break;
  865.  
  866.         case '@':
  867.           c = getc(finput);
  868.           if (isdigit(c) || c == '-')
  869.         {
  870.           ungetc (c, finput);
  871.           n = read_signed_integer(finput);
  872.           c = getc(finput);
  873.         }
  874.           else
  875.         fatal("invalid @-construct");
  876.  
  877.           fprintf(faction, "yylsp[%d]", n - stack_offset);
  878.           yylsp_needed = 1;
  879.  
  880.           continue;
  881.  
  882.         case EOF:
  883.           fatal("unmatched '{'");
  884.  
  885.         default:
  886.           putc(c, faction);
  887.         }
  888.  
  889.           c = getc(finput);
  890.         }
  891.  
  892.       /* above loop exits when c is '}' */
  893.  
  894.       if (--count)
  895.         {
  896.       putc(c, faction);
  897.       c = getc(finput);
  898.     }
  899.     }
  900.  
  901.   fprintf(faction, ";\n    break;}");
  902. }
  903.  
  904.  
  905.  
  906. /* generate a dummy symbol, a nonterminal,
  907. whose name cannot conflict with the user's names. */
  908.  
  909. bucket *gensym(void)
  910. {
  911.   register bucket *sym;
  912.  
  913.   extern char token_buffer[];
  914.   sprintf (token_buffer, "@%d", ++gensym_count);
  915.   sym = getsym(token_buffer);
  916.   sym->class = SNTERM;
  917.   sym->value = nvars++;
  918.   return (sym);
  919. }
  920.  
  921.  
  922.  
  923. /* Parse the input grammar into a one symbol_list structure.
  924. Each rule is represented by a sequence of symbols: the left hand side
  925. followed by the contents of the right hand side, followed by a null pointer
  926. instead of a symbol to terminate the rule.
  927. The next symbol is the lhs of the following rule.
  928.  
  929. All actions are copied out to the appropriate file,
  930. labelled by the rule number they apply to.  */
  931.  
  932. void readgram(void)
  933. {
  934.   register int t;
  935.   register bucket *lhs;
  936.   register symbol_list *p;
  937.   register symbol_list *p1;
  938.   register bucket *bp;
  939.  
  940.   symbol_list *crule;    /* points to first symbol_list of current rule.  */
  941.             /* its symbol is the lhs of the rule.   */
  942.   symbol_list *crule1;  /* points to the symbol_list preceding crule.  */
  943.  
  944.   p1 = NULL;
  945.  
  946.   t = lex();
  947.  
  948.   while (t != TWO_PERCENTS && t != ENDFILE)
  949.     {
  950.       if (t == IDENTIFIER || t == BAR)
  951.     {
  952.       register int actionflag = 0;
  953.       int rulelength = 0;  /* number of symbols in rhs of this rule so far  */
  954.       int xactions = 0;    /* JF for error checking */
  955.       bucket *first_rhs = 0;
  956.  
  957.       if (t == IDENTIFIER)
  958.         {
  959.           lhs = symval;
  960.     
  961.           t = lex();
  962.           if (t != COLON)
  963.         fatal("ill-formed rule");
  964.         }
  965.  
  966.       if (nrules == 0)
  967.         {
  968.           if (t == BAR)
  969.         fatal("grammar starts with vertical bar");
  970.  
  971.           if (!start_flag)
  972.         startval = lhs;
  973.         }
  974.  
  975.       /* start a new rule and record its lhs.  */
  976.  
  977.       nrules++;
  978.       nitems++;
  979.  
  980.       record_rule_line ();
  981.  
  982.       p = NEW(symbol_list);
  983.       p->sym = lhs;
  984.  
  985.       crule1 = p1;
  986.       if (p1)
  987.         p1->next = p;
  988.       else
  989.         grammar = p;
  990.  
  991.       p1 = p;
  992.       crule = p;
  993.  
  994.       /* mark the rule's lhs as a nonterminal if not already so.  */
  995.  
  996.       if (lhs->class == SUNKNOWN)
  997.         {
  998.           lhs->class = SNTERM;
  999.           lhs->value = nvars;
  1000.           nvars++;
  1001.         }
  1002.       else if (lhs->class == STOKEN)
  1003.         fatals("rule given for %s, which is a token", lhs->tag);
  1004.  
  1005.       /* read the rhs of the rule.  */
  1006.  
  1007.       for (;;)
  1008.         {
  1009.           t = lex();
  1010.  
  1011.           if (! (t == IDENTIFIER || t == LEFT_CURLY)) break;
  1012.  
  1013.           /* if next token is an identifier, see if a colon follows it.
  1014.          If one does, exit this rule now.  */
  1015.           if (t == IDENTIFIER)
  1016.         {
  1017.           register bucket *ssave;
  1018.           register int t1;
  1019.  
  1020.           ssave = symval;
  1021.           t1 = lex();
  1022.           unlex(t1);
  1023.           symval = ssave;
  1024.           if (t1 == COLON) break;
  1025.  
  1026.           if(!first_rhs)    /* JF */
  1027.               first_rhs = symval;
  1028.           /* not followed by colon => process as part of this rule's rhs.  */
  1029.           if (actionflag)
  1030.             {
  1031.               register bucket *sdummy;
  1032.  
  1033.               /* if this symbol was preceded by an action, */
  1034.               /* make a dummy nonterminal to replace that action in this rule */
  1035.               /* and make another rule to associate the action to the dummy.  */
  1036.               /* Since the action was written out with this rule's number, */
  1037.               /* we must write give the new rule this number */
  1038.               /* by inserting the new rule before it.  */
  1039.  
  1040.               /* make a dummy nonterminal, a gensym.  */
  1041.               sdummy = gensym();
  1042.  
  1043.               /* make a new rule, whose body is empty, before the current one.  */
  1044.               /* so that the action just read can belong to it.  */
  1045.               nrules++;
  1046.               nitems++;
  1047.               record_rule_line ();
  1048.               p = NEW(symbol_list);
  1049.               if (crule1)
  1050.             crule1->next = p;
  1051.               else grammar = p;
  1052.               p->sym = sdummy;
  1053.               crule1 = NEW(symbol_list);
  1054.               p->next = crule1;
  1055.               crule1->next = crule;
  1056.               
  1057.               /* insert the dummy generated by that rule into this rule.  */
  1058.               nitems++;
  1059.               p = NEW(symbol_list);
  1060.               p->sym = sdummy;
  1061.               p1->next = p;
  1062.               p1 = p;
  1063.  
  1064.               actionflag = 0;
  1065.             }
  1066.           nitems++;
  1067.           p = NEW(symbol_list);
  1068.           p->sym = symval;
  1069.           p1->next = p;
  1070.           p1 = p;
  1071.         }
  1072.           else /* handle an action.  */
  1073.         {
  1074.           copy_action(crule, rulelength);
  1075.           actionflag = 1;
  1076.           xactions++;    /* JF */
  1077.         }
  1078.           rulelength++;
  1079.         }
  1080.  
  1081.       /* Put an empty link in the list to mark the end of this rule  */
  1082.       p = NEW(symbol_list);
  1083.       p1->next = p;
  1084.       p1 = p;
  1085.  
  1086.       /* Accept a precedence declaration */
  1087.       if (t == PREC)
  1088.         {
  1089.           t = lex();
  1090.           crule->ruleprec = symval;
  1091.           t = lex();
  1092.         }
  1093.  
  1094.       /* Now copy the action (if any) */
  1095.       if (t == LEFT_CURLY)
  1096.         {
  1097.           if (actionflag) fatal("two actions at end of one rule");
  1098.           copy_action(crule, rulelength);
  1099.           t = lex();
  1100.         }
  1101.       /* JF if we'd end up using default, get a warning */
  1102.       else if(!xactions && first_rhs && lhs->type_name!=first_rhs->type_name) {
  1103.         if(lhs->type_name == 0 || first_rhs->type_name == 0 ||
  1104.                       strcmp(lhs->type_name,first_rhs->type_name))
  1105.           fprintf(stderr,"%s:%d:  warning:  type clash ('%s' '%s') on default action\n",
  1106.               infile,
  1107.               lineno,
  1108.               lhs->type_name ? lhs->type_name : "",
  1109.               first_rhs->type_name ? first_rhs->type_name : "");
  1110.       }
  1111.       if (t == SEMICOLON)
  1112.         t = lex();
  1113.     }
  1114.       /* these things can appear as alternatives to rules.  */
  1115.       else if (t == TOKEN)
  1116.     {
  1117.       parse_token_decl(STOKEN, SNTERM);
  1118.       t = lex();
  1119.     }
  1120.       else if (t == NTERM)
  1121.     {
  1122.       parse_token_decl(SNTERM, STOKEN);
  1123.       t = lex();
  1124.     }
  1125.       else if (t == TYPE)
  1126.     {
  1127.       t = get_type();
  1128.     }
  1129.       else if (t == UNION)
  1130.     {
  1131.       parse_union_decl();
  1132.       t = lex();
  1133.     }
  1134.       else if (t == EXPECT)
  1135.     {
  1136.       parse_expect_decl();
  1137.       t = lex();
  1138.     }
  1139.       else if (t == START)
  1140.     {
  1141.       parse_start_decl();
  1142.       t = lex();
  1143.     }
  1144.       else
  1145.     fatal("invalid input");
  1146.     }
  1147.  
  1148.   if (nrules == 0)
  1149.     fatal("no input grammar");
  1150.  
  1151.   if (typed == 0)/* JF put out same default YYSTYPE as YACC does */
  1152.     {
  1153.       fprintf(fattrs, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
  1154.       if (fdefines)
  1155.     fprintf(fdefines, "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
  1156.     }
  1157.  
  1158.   /* Report any undefined symbols and consider them nonterminals.  */
  1159.  
  1160.   for (bp = firstsymbol; bp; bp = bp->next)
  1161.     if (bp->class == SUNKNOWN)
  1162.       {
  1163.     fprintf(stderr, "symbol %s used, not defined as token, and no rules for it\n",
  1164.             bp->tag);
  1165.     failure = 1;
  1166.     bp->class = SNTERM;
  1167.     bp->value = nvars++;
  1168.       }
  1169.  
  1170.   ntokens = nsyms - nvars;
  1171. }
  1172.  
  1173.  
  1174. void record_rule_line (void)
  1175. {
  1176.   /* Record each rule's source line number in rline table.  */
  1177.  
  1178.   if (nrules >= rline_allocated)
  1179.     {
  1180.       rline_allocated = nrules * 2;
  1181.       rline = (short *) realloc (rline,
  1182.                  rline_allocated * sizeof (short));
  1183.       if (rline == 0)
  1184.     {
  1185.       fprintf (stderr, "bison: memory exhausted\n");
  1186.       done (1);
  1187.     }
  1188.     }
  1189.   rline[nrules] = lineno;
  1190. }
  1191.  
  1192.  
  1193. /* read in a %type declaration and record its information for get_type_name to access */
  1194.  
  1195. int get_type(void)
  1196. {
  1197.   register int k;
  1198.   register int t;
  1199.   register char *name;
  1200.  
  1201.   extern char token_buffer[];
  1202.  
  1203.   t = lex();
  1204.  
  1205.   if (t != TYPENAME)
  1206.     fatal("ill-formed %%type declaration");
  1207.  
  1208.   k = strlen(token_buffer);
  1209.   name = NEW2(k + 1, char);
  1210.   strcpy(name, token_buffer);
  1211.  
  1212.   for (;;)
  1213.     {
  1214.       t = lex();
  1215.  
  1216.       switch (t)
  1217.     {
  1218.     case SEMICOLON:
  1219.       return (lex());
  1220.  
  1221.     case COMMA:
  1222.       break;
  1223.  
  1224.     case IDENTIFIER:
  1225.       if (symval->type_name == NULL)
  1226.         symval->type_name = name;
  1227.       else
  1228.         fatals("type redeclaration for %s", symval->tag);
  1229.  
  1230.       break;
  1231.  
  1232.     default:
  1233.       return (t);
  1234.     }
  1235.     }
  1236. }
  1237.  
  1238.  
  1239.  
  1240. /* assign symbol numbers, and write definition of token names into fdefines.
  1241. Set up vectors tags and sprec of names and precedences of symbols.  */
  1242.  
  1243. void packsymbols(void)
  1244. {
  1245.   register bucket *bp;
  1246.   register int tokno = 1;
  1247.   register int last_user_token_number;
  1248.  
  1249.   /* int lossage = 0; JF set but not used */
  1250.  
  1251.   tags = NEW2(nsyms + 1, char *);
  1252.   tags[0] = "$";
  1253.  
  1254.   sprec = NEW2(nsyms, short);
  1255.   sassoc = NEW2(nsyms, short);
  1256.  
  1257.   max_user_token_number = 256;
  1258.   last_user_token_number = 256;
  1259.  
  1260.   for (bp = firstsymbol; bp; bp = bp->next)
  1261.     {
  1262.       if (bp->class == SNTERM)
  1263.     {
  1264.       bp->value += ntokens;
  1265.     }
  1266.       else
  1267.     {
  1268.       if (translations && !(bp->user_token_number))
  1269.         bp->user_token_number = ++last_user_token_number;
  1270.       if (bp->user_token_number > max_user_token_number)
  1271.         max_user_token_number = bp->user_token_number;
  1272.       bp->value = tokno++;
  1273.     }
  1274.  
  1275.       tags[bp->value] = bp->tag;
  1276.       sprec[bp->value] = bp->prec;
  1277.       sassoc[bp->value] = bp->assoc;
  1278.  
  1279.     }
  1280.  
  1281.   if (translations)
  1282.     {
  1283.       register int i;
  1284.  
  1285.       token_translations = NEW2(max_user_token_number+1, short);
  1286.  
  1287.       /* initialize all entries for literal tokens to 2,
  1288.      the internal token number for $illegal., which represents all invalid inputs.  */
  1289.       for (i = 0; i <= max_user_token_number; i++)
  1290.         token_translations[i] = 2;      
  1291.     }
  1292.  
  1293.   for (bp = firstsymbol; bp; bp = bp->next)
  1294.     {
  1295.       if (bp->value >= ntokens) continue;
  1296.       if (translations)
  1297.     {
  1298.       if (token_translations[bp->user_token_number] != 2)
  1299.         {
  1300.             /* JF made this a call to fatals() */
  1301.           fatals( "tokens %s and %s both assigned number %d",
  1302.                   tags[token_translations[bp->user_token_number]],
  1303.                   bp->tag,
  1304.                   bp->user_token_number);
  1305.         }
  1306.       token_translations[bp->user_token_number] = bp->value;
  1307.     }
  1308.     }
  1309.  
  1310.   error_token_number = errtoken->value;
  1311.  
  1312.   output_token_defines(ftable);
  1313.  
  1314.   if (startval->class == SUNKNOWN)
  1315.     fatals("the start symbol %s is undefined", startval->tag);
  1316.   else if (startval->class == STOKEN)
  1317.     fatals("the start symbol %s is a token", startval->tag);
  1318.  
  1319.   start_symbol = startval->value;
  1320.  
  1321.   if (definesflag)
  1322.     {
  1323.       output_token_defines(fdefines);
  1324.  
  1325.       fprintf(fdefines, "extern YYSTYPE yylval;\n");
  1326.  
  1327.       fclose(fdefines);
  1328.       fdefines = NULL;
  1329.     }
  1330. }
  1331.       
  1332.  
  1333. void output_token_defines(FILE *file)
  1334. {
  1335.   bucket *bp;
  1336.  
  1337.   for (bp = firstsymbol; bp; bp = bp->next)
  1338.     {
  1339.       if (bp->value >= ntokens) continue;
  1340.  
  1341.       /* For named tokens, but not literal ones, define the name.  */
  1342.       /* The value is the user token number.  */
  1343.  
  1344.       if ('\'' != *tags[bp->value] && bp != errtoken)
  1345.     {
  1346.       register char *cp = tags[bp->value];
  1347.       register char c;
  1348.  
  1349.       /* Don't #define nonliteral tokens whose names contain periods.  */
  1350.  
  1351.       while ((c = *cp++) && c != '.');
  1352.       if (!c)
  1353.         {
  1354.               fprintf(file, "#define\t%s\t%d\n", tags[bp->value],
  1355.                 (translations ? bp->user_token_number : bp->value));
  1356.         }
  1357.     }
  1358.     }
  1359.  
  1360.   putc('\n', file);
  1361. }
  1362.  
  1363.  
  1364.  
  1365. /* convert the rules into the representation using rrhs, rlhs and ritems.  */
  1366.  
  1367. void packgram(void)
  1368. {
  1369.   register int itemno;
  1370.   register int ruleno;
  1371.   register symbol_list *p;
  1372. /*  register bucket *bp; JF unused */
  1373.  
  1374.   bucket *ruleprec;
  1375.  
  1376.   ritem = NEW2(nitems + 1, short);
  1377.   rlhs = NEW2(nrules, short) - 1;
  1378.   rrhs = NEW2(nrules, short) - 1;
  1379.   rprec = NEW2(nrules, short) - 1;
  1380.   rprecsym = NEW2(nrules, short) - 1;
  1381.   rassoc = NEW2(nrules, short) - 1;
  1382.  
  1383.   itemno = 0;
  1384.   ruleno = 1;
  1385.  
  1386.   p = grammar;
  1387.   while (p)
  1388.     {
  1389.       rlhs[ruleno] = p->sym->value;
  1390.       rrhs[ruleno] = itemno;
  1391.       ruleprec = p->ruleprec;
  1392.  
  1393.       p = p->next;
  1394.       while (p && p->sym)
  1395.     {
  1396.       ritem[itemno++] = p->sym->value;
  1397.       /* A rule gets by default the precedence and associativity
  1398.          of the last token in it.  */
  1399.           if (p->sym->class == STOKEN)
  1400.         {
  1401.           rprec[ruleno] = p->sym->prec;
  1402.           rassoc[ruleno] = p->sym->assoc;
  1403.         }
  1404.       if (p) p = p->next;
  1405.     }
  1406.  
  1407.       /* If this rule has a %prec,
  1408.      the specified symbol's precedence replaces the default.  */
  1409.       if (ruleprec)
  1410.     {
  1411.           rprec[ruleno] = ruleprec->prec;
  1412.           rassoc[ruleno] = ruleprec->assoc;
  1413.       rprecsym[ruleno] = ruleprec->value;
  1414.     }
  1415.  
  1416.       ritem[itemno++] = -ruleno;
  1417.       ruleno++;
  1418.  
  1419.       if (p) p = p->next;
  1420.     }
  1421.  
  1422.   ritem[itemno] = 0;
  1423. }
  1424.  
  1425. /* Read a signed integer from STREAM and return its value.  */
  1426.  
  1427. int read_signed_integer (FILE *stream)
  1428. {
  1429.   register int c = getc(stream);
  1430.   register int sign = 1;
  1431.   register int n;
  1432.  
  1433.   if (c == '-')
  1434.     {
  1435.       c = getc(stream);
  1436.       sign = -1;
  1437.     }
  1438.   n = 0;
  1439.   while (isdigit(c))
  1440.     {
  1441.       n = 10*n + (c - '0');
  1442.       c = getc(stream);
  1443.     }
  1444.  
  1445.   ungetc(c, stream);
  1446.  
  1447.   return n * sign;
  1448. }
  1449.