home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / gnu / flex-2.4.6-src.lha / src / amiga / flex-2.4.6 / parse.y < prev    next >
Text File  |  1993-12-09  |  16KB  |  818 lines

  1. /* parse.y - parser for flex input */
  2.  
  3. %token CHAR NUMBER SECTEND SCDECL XSCDECL WHITESPACE NAME PREVCCL EOF_OP
  4.  
  5. %{
  6. /*-
  7.  * Copyright (c) 1990 The Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * This code is derived from software contributed to Berkeley by
  11.  * Vern Paxson.
  12.  * 
  13.  * The United States Government has rights in this work pursuant
  14.  * to contract no. DE-AC03-76SF00098 between the United States
  15.  * Department of Energy and the University of California.
  16.  *
  17.  * Redistribution and use in source and binary forms are permitted provided
  18.  * that: (1) source distributions retain this entire copyright notice and
  19.  * comment, and (2) distributions including binaries display the following
  20.  * acknowledgement:  ``This product includes software developed by the
  21.  * University of California, Berkeley and its contributors'' in the
  22.  * documentation or other materials provided with the distribution and in
  23.  * all advertising materials mentioning features or use of this software.
  24.  * Neither the name of the University nor the names of its contributors may
  25.  * be used to endorse or promote products derived from this software without
  26.  * specific prior written permission.
  27.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  28.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  29.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  30.  */
  31.  
  32. /* $Header: /home/daffy/u0/vern/flex/RCS/parse.y,v 2.15 93/12/09 13:57:23 vern Exp $ */
  33.  
  34.  
  35. /* Some versions of bison are broken in that they use alloca() but don't
  36.  * declare it properly.  The following is the patented (just kidding!)
  37.  * #ifdef chud to fix the problem, courtesy of Francois Pinard.
  38.  */
  39. #ifdef YYBISON
  40. /* AIX requires this to be the first thing in the file.  */
  41. #ifdef __GNUC__
  42. #define alloca __builtin_alloca
  43. #else /* not __GNUC__ */
  44. #if HAVE_ALLOCA_H
  45. #include <alloca.h>
  46. #else /* not HAVE_ALLOCA_H */
  47. #ifdef _AIX
  48.  #pragma alloca
  49. #else /* not _AIX */
  50. char *alloca ();
  51. #endif /* not _AIX */
  52. #endif /* not HAVE_ALLOCA_H */
  53. #endif /* not __GNUC__ */
  54. #endif /* YYBISON */
  55.  
  56. /* Bletch, ^^^^ that was ugly! */
  57.  
  58.  
  59. #include "flexdef.h"
  60.  
  61. int pat, scnum, eps, headcnt, trailcnt, anyccl, lastchar, i, actvp, rulelen;
  62. int trlcontxt, xcluflg, cclsorted, varlength, variable_trail_rule;
  63. int *active_ss;
  64. Char clower();
  65. void build_eof_action();
  66. void yyerror();
  67.  
  68. static int madeany = false;  /* whether we've made the '.' character class */
  69. int previous_continued_action;    /* whether the previous rule's action was '|' */
  70.  
  71. /* On some over-ambitious machines, such as DEC Alpha's, the default
  72.  * token type is "long" instead of "int"; this leads to problems with
  73.  * declaring yylval in flexdef.h.  But so far, all the yacc's I've seen
  74.  * wrap their definitions of YYSTYPE with "#ifndef YYSTYPE"'s, so the
  75.  * following should ensure that the default token type is "int".
  76.  */
  77. #define YYSTYPE int
  78.  
  79. %}
  80.  
  81. %%
  82. goal        :  initlex sect1 sect1end sect2 initforrule
  83.             { /* add default rule */
  84.             int def_rule;
  85.  
  86.             pat = cclinit();
  87.             cclnegate( pat );
  88.  
  89.             def_rule = mkstate( -pat );
  90.  
  91.             /* Remember the number of the default rule so we
  92.              * don't generate "can't match" warnings for it.
  93.              */
  94.             default_rule = num_rules;
  95.  
  96.             finish_rule( def_rule, false, 0, 0 );
  97.  
  98.             for ( i = 1; i <= lastsc; ++i )
  99.                 scset[i] = mkbranch( scset[i], def_rule );
  100.  
  101.             if ( spprdflt )
  102.                 add_action(
  103.                 "YY_FATAL_ERROR( \"flex scanner jammed\" )" );
  104.             else
  105.                 add_action( "ECHO" );
  106.  
  107.             add_action( ";\n\tYY_BREAK\n" );
  108.             }
  109.         ;
  110.  
  111. initlex        :
  112.             { /* initialize for processing rules */
  113.  
  114.             /* Create default DFA start condition. */
  115.             scinstal( "INITIAL", false );
  116.  
  117.             /* Initially, the start condition scoping is
  118.              * "no start conditions active".
  119.              */
  120.             actvp = 0;
  121.             }
  122.         ;
  123.  
  124. sect1        :  sect1 startconddecl WHITESPACE namelist1 '\n'
  125.         |
  126.         |  error '\n'
  127.             { synerr( "unknown error processing section 1" ); }
  128.         ;
  129.  
  130. sect1end    :  SECTEND
  131.             {
  132.             /* We now know how many start conditions there
  133.              * are, so create the "activity" map indicating
  134.              * which conditions are active.
  135.              */
  136.             active_ss = allocate_integer_array( lastsc + 1 );
  137.  
  138.             for ( i = 1; i <= lastsc; ++i )
  139.                 active_ss[i] = 0;
  140.             }
  141.         ;
  142.  
  143. startconddecl    :  SCDECL
  144.             { xcluflg = false; }
  145.  
  146.         |  XSCDECL
  147.             { xcluflg = true; }
  148.         ;
  149.  
  150. namelist1    :  namelist1 WHITESPACE NAME
  151.             { scinstal( nmstr, xcluflg ); }
  152.  
  153.         |  NAME
  154.             { scinstal( nmstr, xcluflg ); }
  155.  
  156.         |  error
  157.             { synerr( "bad start condition list" ); }
  158.         ;
  159.  
  160. sect2        :  sect2 initforrule flexrule '\n'
  161.         |
  162.         ;
  163.  
  164. initforrule    :
  165.             {
  166.             /* Initialize for a parse of one rule. */
  167.             trlcontxt = variable_trail_rule = varlength = false;
  168.             trailcnt = headcnt = rulelen = 0;
  169.             current_state_type = STATE_NORMAL;
  170.             previous_continued_action = continued_action;
  171.             new_rule();
  172.             }
  173.         ;
  174.  
  175. flexrule    :  scon '^' rule
  176.             {
  177.             pat = $3;
  178.             finish_rule( pat, variable_trail_rule,
  179.                 headcnt, trailcnt );
  180.  
  181.             for ( i = 1; i <= actvp; ++i )
  182.                 scbol[actvsc[i]] =
  183.                     mkbranch( scbol[actvsc[i]], pat );
  184.  
  185.             if ( ! bol_needed )
  186.                 {
  187.                 bol_needed = true;
  188.  
  189.                 if ( performance_report > 1 )
  190.                     pinpoint_message( 
  191.             "'^' operator results in sub-optimal performance" );
  192.                 }
  193.             }
  194.  
  195.         |  scon rule
  196.             {
  197.             pat = $2;
  198.             finish_rule( pat, variable_trail_rule,
  199.                 headcnt, trailcnt );
  200.  
  201.             for ( i = 1; i <= actvp; ++i )
  202.                 scset[actvsc[i]] =
  203.                     mkbranch( scset[actvsc[i]], pat );
  204.             }
  205.  
  206.         |  '^' rule
  207.             {
  208.             pat = $2;
  209.             finish_rule( pat, variable_trail_rule,
  210.                 headcnt, trailcnt );
  211.  
  212.             /* Add to all non-exclusive start conditions,
  213.              * including the default (0) start condition.
  214.              */
  215.  
  216.             for ( i = 1; i <= lastsc; ++i )
  217.                 if ( ! scxclu[i] )
  218.                     scbol[i] = mkbranch( scbol[i], pat );
  219.  
  220.             if ( ! bol_needed )
  221.                 {
  222.                 bol_needed = true;
  223.  
  224.                 if ( performance_report > 1 )
  225.                     pinpoint_message(
  226.             "'^' operator results in sub-optimal performance" );
  227.                 }
  228.             }
  229.  
  230.         |  rule
  231.             {
  232.             pat = $1;
  233.             finish_rule( pat, variable_trail_rule,
  234.                 headcnt, trailcnt );
  235.  
  236.             for ( i = 1; i <= lastsc; ++i )
  237.                 if ( ! scxclu[i] )
  238.                     scset[i] = mkbranch( scset[i], pat );
  239.             }
  240.  
  241.         |  scon EOF_OP
  242.             { build_eof_action(); }
  243.  
  244.         |  EOF_OP
  245.             {
  246.             /* This EOF applies to all start conditions
  247.              * which don't already have EOF actions.
  248.              */
  249.             actvp = 0;
  250.  
  251.             for ( i = 1; i <= lastsc; ++i )
  252.                 if ( ! sceof[i] )
  253.                     actvsc[++actvp] = i;
  254.  
  255.             if ( actvp == 0 )
  256.                 warn(
  257.             "all start conditions already have <<EOF>> rules" );
  258.  
  259.             else
  260.                 build_eof_action();
  261.             }
  262.  
  263.         |  error
  264.             { synerr( "unrecognized rule" ); }
  265.         ;
  266.  
  267. scon        :  '<' namelist2 '>'
  268.  
  269.         |  '<' '*' '>'
  270.             {
  271.             actvp = 0;
  272.  
  273.             for ( i = 1; i <= lastsc; ++i )
  274.                 actvsc[++actvp] = i;
  275.             }
  276.         ;
  277.  
  278. namelist2    :  namelist2 ',' sconname
  279.  
  280.         |  { actvp = 0; } sconname
  281.  
  282.         |  error
  283.             { synerr( "bad start condition list" ); }
  284.         ;
  285.  
  286. sconname    :  NAME
  287.             {
  288.             if ( (scnum = sclookup( nmstr )) == 0 )
  289.                 format_pinpoint_message(
  290.                     "undeclared start condition %s",
  291.                     nmstr );
  292.             else
  293.                 {
  294.                 if ( ++actvp >= current_max_scs )
  295.                     /* Some bozo has included multiple
  296.                      * instances of start condition names.
  297.                      */
  298.                     pinpoint_message(
  299.                 "too many start conditions in <> construct!" );
  300.  
  301.                 else
  302.                     actvsc[actvp] = scnum;
  303.                 }
  304.             }
  305.         ;
  306.  
  307. rule        :  re2 re
  308.             {
  309.             if ( transchar[lastst[$2]] != SYM_EPSILON )
  310.                 /* Provide final transition \now/ so it
  311.                  * will be marked as a trailing context
  312.                  * state.
  313.                  */
  314.                 $2 = link_machines( $2,
  315.                         mkstate( SYM_EPSILON ) );
  316.  
  317.             mark_beginning_as_normal( $2 );
  318.             current_state_type = STATE_NORMAL;
  319.  
  320.             if ( previous_continued_action )
  321.                 {
  322.                 /* We need to treat this as variable trailing
  323.                  * context so that the backup does not happen
  324.                  * in the action but before the action switch
  325.                  * statement.  If the backup happens in the
  326.                  * action, then the rules "falling into" this
  327.                  * one's action will *also* do the backup,
  328.                  * erroneously.
  329.                  */
  330.                 if ( ! varlength || headcnt != 0 )
  331.                     warn(
  332.         "trailing context made variable due to preceding '|' action" );
  333.  
  334.                 /* Mark as variable. */
  335.                 varlength = true;
  336.                 headcnt = 0;
  337.                 }
  338.  
  339.             if ( lex_compat || (varlength && headcnt == 0) )
  340.                 { /* variable trailing context rule */
  341.                 /* Mark the first part of the rule as the
  342.                  * accept