Пример #1
0
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

  /** Helper routine to strip a trailing action off rhs and return it
   * @param rhs_parts array of RHS parts.
   * @param len       how many of those are valid.
   * @return          the removed action part.
   */
  protected action_part strip_trailing_action(
    production_part[] rhs_parts,
    int             len)
    {
      action_part result;

      /* bail out early if we have nothing to do */
      if (rhs_parts == null || len == 0) return null;

      /* see if we have a trailing action */
      if (rhs_parts[len-1].is_action())
	{
	  /* snip it out and return it */
	  result = (action_part)rhs_parts[len-1];
	  rhs_parts[len-1] = null;
	  return result;
	}
      else
	return null;
    }
Пример #2
0
  /*-----------------------------------------------------------*/
  /*--- Constructor(s) ----------------------------------------*/
  /*-----------------------------------------------------------*/

  /** Full constructor.  This constructor accepts a LHS non terminal,
   *  an array of RHS parts (including terminals, non terminals, and 
   *  actions), and a string for a final reduce action.   It does several
   *  manipulations in the process of  creating a production object.
   *  After some validity checking it translates labels that appear in
   *  actions into code for accessing objects on the runtime parse stack.
   *  It them merges adjacent actions if they appear and moves any trailing
   *  action into the final reduce actions string.  Next it removes any
   *  embedded actions by factoring them out with new action productions.  
   *  Finally it assigns a unique index to the production.<p>
   *
   *  Factoring out of actions is accomplished by creating new "hidden"
   *  non terminals.  For example if the production was originally: <pre>
   *    A ::= B {action} C D
   *  </pre>
   *  then it is factored into two productions:<pre>
   *    A ::= B X C D
   *    X ::= {action}
   *  </pre> 
   *  (where X is a unique new non terminal).  This has the effect of placing
   *  all actions at the end where they can be handled as part of a reduce by
   *  the parser.
   */
  public production(
    non_terminal    lhs_sym, 
    production_part[] rhs_parts, 
    int             rhs_l,
    string          action_str)
    {
      int         i;
      action_part tail_action;
      string declare_str;
      int rightlen = rhs_l;

      /* remember the length */
      if (rhs_l >= 0)
	_rhs_length = rhs_l;
      else if (rhs_parts != null)
	_rhs_length = rhs_parts.Length;
      else
	_rhs_length = 0;
	
      /* make sure we have a valid left-hand-side */
      if (lhs_sym == null) 
	throw new internal_error(
	  "Attempt to construct a production with a null LHS");

      /* I'm not translating labels anymore, I'm adding code to declare
	 labels as valid variables.  This way, the users code string is
	 untouched 
	 6/96 frankf */

      /* check if the last part of the right hand side is an action.  If
         it is, it won't be on the stack, so we don't want to count it 
	 in the rightlen.  Then when we search down the stack for a
         Symbol, we don't try to search past action */

      if (rhs_l > 0) {
	if (rhs_parts[rhs_l - 1].is_action()) {
	  rightlen = rhs_l - 1;
	} else {
	  rightlen = rhs_l;
	}
      }

      /* get the generated declaration code for the necessary labels. */
      declare_str = declare_labels(
		    rhs_parts, rightlen, action_str);

      if (action_str == null) 
	action_str = declare_str;
      else 
	action_str = declare_str + action_str;	 	  

      /* count use of lhs */
      lhs_sym.note_use();

      /* create the part for left-hand-side */
      _lhs = new symbol_part(lhs_sym);

      /* merge adjacent actions (if any) */
      _rhs_length = merge_adjacent_actions(rhs_parts, _rhs_length);

      /* strip off any trailing action */
      tail_action = strip_trailing_action(rhs_parts, _rhs_length);
      if (tail_action != null) _rhs_length--;

      /* Why does this run through the right hand side happen
	 over and over?  here a quick combination of two 
	 prior runs plus one I wanted of my own
	 frankf 6/25/96 */
      /* allocate and copy over the right-hand-side */
      /* count use of each rhs symbol */
      _rhs = new production_part[_rhs_length];
      for (i=0; i<_rhs_length; i++) {
	_rhs[i] = rhs_parts[i];
	if (!_rhs[i].is_action()) {
	  ((symbol_part)_rhs[i]).the_symbol().note_use();
	  if ((((symbol_part)_rhs[i]).the_symbol()).GetType()==typeof(terminal)) {
	    _rhs_prec = 
	      ((terminal)((symbol_part)_rhs[i]).the_symbol()).precedence_num();
	    _rhs_assoc = 
	      ((terminal)((symbol_part)_rhs[i]).the_symbol()).precedence_side();
	  }
	}
      }

      /*now action string is really declaration string, so put it in front!
	6/14/96 frankf */
      if (action_str == null) action_str = "";
      if (tail_action != null && tail_action.code_string() != null)
	action_str = action_str + "\t\t" +  tail_action.code_string();

      /* stash the action */
      _action = new action_part(action_str);

      /* rewrite production to remove any embedded actions */
      remove_embedded_actions();

      /* assign an index */
      _index = next_index++;
//      System.Console.WriteLine(_index+":len:"+rhs_l);

      /* put us in the global collection of productions */
      _all.Add(_index,this);

      /* put us in the production list of the lhs non terminal */
      lhs_sym.add_production(this);
    }
Пример #3
0
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

  /** Declare label names as valid variables within the action string
   * @param rhs          array of RHS parts.
   * @param rhs_len      how much of rhs to consider valid.
   * @param final_action the final action string of the production. 
   * @param lhs_type     the object type associated with the LHS symbol.
   */ 
  protected string declare_labels(
    production_part[]  rhs, 
    int              rhs_len, 
    string           final_action)
    {
      string declaration = "";

      symbol_part part;
      int         pos;

      /* walk down the parts and extract the labels */
      for (pos = 0; pos < rhs_len; pos++)
	{
	  if (!rhs[pos].is_action())
	    {
	      part = (symbol_part)rhs[pos];

	      /* if it has a label, make declaration! */
	      if (part.label() != null)
		{
		  declaration = declaration + 
		    make_declaration(part.label(), part.the_symbol().stack_type(), 
				     rhs_len-pos-1);
		}
	    }
	}
      return declaration;
    }
Пример #4
0
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

  /** Helper routine to merge adjacent actions in a Set of RHS parts 
   * @param rhs_parts array of RHS parts.
   * @param len       amount of that array that is valid.
   * @return          remaining valid length.
   */
  protected int merge_adjacent_actions(production_part[] rhs_parts, int len)
    {
      int from_loc, to_loc, merge_cnt;

      /* bail out early if we have no work to do */
      if (rhs_parts == null || len == 0) return 0;

      merge_cnt = 0;
      to_loc = -1;
      for (from_loc=0; from_loc<len; from_loc++)
	{
	  /* do we go in the current position or one further */
	  if (to_loc < 0 || !rhs_parts[to_loc].is_action() 
			 || !rhs_parts[from_loc].is_action())
	    {
	      /* next one */
	      to_loc++;

	      /* clear the way for it */
	      if (to_loc != from_loc) rhs_parts[to_loc] = null;
	    }

	  /* if this is not trivial? */
	  if (to_loc != from_loc)
	    {
	      /* do we merge or copy? */
	      if (rhs_parts[to_loc] != null && rhs_parts[to_loc].is_action() && 
		  rhs_parts[from_loc].is_action())
	      {
	        /* merge */
	        rhs_parts[to_loc] = new action_part(
		  ((action_part)rhs_parts[to_loc]).code_string() +
		  ((action_part)rhs_parts[from_loc]).code_string());
	        merge_cnt++;
	      }
	    else
	      {
	        /* copy */
	        rhs_parts[to_loc] = rhs_parts[from_loc];
	      }
	    }
	}

      /* return the used length */
      return len - merge_cnt;
    }
Пример #5
0
 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/	
    
 /* Constructor w/ no action string and contextual precedence
    defined */
 public production(
   non_terminal    lhs_sym, 
   production_part[] rhs_parts, 
   int             rhs_l,
   int             prec_num,
   int             prec_side):this(lhs_sym,rhs_parts,rhs_l,null)
   {
     /* Set the precedence */
     set_precedence_num(prec_num);
     set_precedence_side(prec_side);
   }
Пример #6
0
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

  /** Constructor with no action string. */
  public production(
    non_terminal    lhs_sym, 
    production_part[] rhs_parts, 
    int             rhs_l) : this(lhs_sym,rhs_parts,rhs_l,null) { }
Пример #7
0
        /** add a new right hand side part */
    	protected void add_rhs_part(production_part part)// throws java.lang.Exception
        {
        	if (rhs_pos >= MAX_RHS)
            	throw new System.Exception("Internal Error: Productions limited to " + 
                	MAX_RHS + " symbols and actions"); 

        	rhs_parts[rhs_pos] = part;
        	rhs_pos++;
        }
Пример #8
0
        /** helper routine to clone a new production part adding a given label */
    	protected production_part add_lab(production_part part, string lab)
            //  throws internal_error
        {
            /* if there is no label, or this is an action, just return the original */
        	if (lab == null || part.is_action()) return part;

            /* otherwise build a new one with the given label attached */
        	return new symbol_part(((symbol_part)part).the_symbol(),lab);
        }
Пример #9
0
        /*-----------------------------------------------------------*/
        /*--- Constructor(s) ----------------------------------------*/
        /*-----------------------------------------------------------*/

        /** Full constructor.  This constructor accepts a LHS non terminal,
         *  an array of RHS parts (including terminals, non terminals, and
         *  actions), and a string for a final reduce action.   It does several
         *  manipulations in the process of  creating a production object.
         *  After some validity checking it translates labels that appear in
         *  actions into code for accessing objects on the runtime parse stack.
         *  It them merges adjacent actions if they appear and moves any trailing
         *  action into the final reduce actions string.  Next it removes any
         *  embedded actions by factoring them out with new action productions.
         *  Finally it assigns a unique index to the production.<p>
         *
         *  Factoring out of actions is accomplished by creating new "hidden"
         *  non terminals.  For example if the production was originally: <pre>
         *    A ::= B {action} C D
         *  </pre>
         *  then it is factored into two productions:<pre>
         *    A ::= B X C D
         *    X ::= {action}
         *  </pre>
         *  (where X is a unique new non terminal).  This has the effect of placing
         *  all actions at the end where they can be handled as part of a reduce by
         *  the parser.
         */
        public production(
            non_terminal lhs_sym,
            production_part[] rhs_parts,
            int rhs_l,
            string action_str)
        {
            int         i;
            action_part tail_action;
            string      declare_str;
            int         rightlen = rhs_l;

            /* remember the length */
            if (rhs_l >= 0)
            {
                _rhs_length = rhs_l;
            }
            else if (rhs_parts != null)
            {
                _rhs_length = rhs_parts.Length;
            }
            else
            {
                _rhs_length = 0;
            }

            /* make sure we have a valid left-hand-side */
            if (lhs_sym == null)
            {
                throw new internal_error(
                          "Attempt to construct a production with a null LHS");
            }

            /* I'm not translating labels anymore, I'm adding code to declare
             * labels as valid variables.  This way, the users code string is
             * untouched
             * 6/96 frankf */

            /* check if the last part of the right hand side is an action.  If
             * it is, it won't be on the stack, so we don't want to count it
             * in the rightlen.  Then when we search down the stack for a
             * Symbol, we don't try to search past action */

            if (rhs_l > 0)
            {
                if (rhs_parts[rhs_l - 1].is_action())
                {
                    rightlen = rhs_l - 1;
                }
                else
                {
                    rightlen = rhs_l;
                }
            }

            /* get the generated declaration code for the necessary labels. */
            declare_str = declare_labels(
                rhs_parts, rightlen, action_str);

            if (action_str == null)
            {
                action_str = declare_str;
            }
            else
            {
                action_str = declare_str + action_str;
            }

            /* count use of lhs */
            lhs_sym.note_use();

            /* create the part for left-hand-side */
            _lhs = new symbol_part(lhs_sym);

            /* merge adjacent actions (if any) */
            _rhs_length = merge_adjacent_actions(rhs_parts, _rhs_length);

            /* strip off any trailing action */
            tail_action = strip_trailing_action(rhs_parts, _rhs_length);
            if (tail_action != null)
            {
                _rhs_length--;
            }

            /* Why does this run through the right hand side happen
             * over and over?  here a quick combination of two
             * prior runs plus one I wanted of my own
             * frankf 6/25/96 */
            /* allocate and copy over the right-hand-side */
            /* count use of each rhs symbol */
            _rhs = new production_part[_rhs_length];
            for (i = 0; i < _rhs_length; i++)
            {
                _rhs[i] = rhs_parts[i];
                if (!_rhs[i].is_action())
                {
                    ((symbol_part)_rhs[i]).the_symbol().note_use();
                    if ((((symbol_part)_rhs[i]).the_symbol()).GetType() == typeof(terminal))
                    {
                        _rhs_prec =
                            ((terminal)((symbol_part)_rhs[i]).the_symbol()).precedence_num();
                        _rhs_assoc =
                            ((terminal)((symbol_part)_rhs[i]).the_symbol()).precedence_side();
                    }
                }
            }

            /*now action string is really declaration string, so put it in front!
             * 6/14/96 frankf */
            if (action_str == null)
            {
                action_str = "";
            }
            if (tail_action != null && tail_action.code_string() != null)
            {
                action_str = action_str + "\t\t" + tail_action.code_string();
            }

            /* stash the action */
            _action = new action_part(action_str);

            /* rewrite production to remove any embedded actions */
            remove_embedded_actions();

            /* assign an index */
            _index = next_index++;
//      System.Console.WriteLine(_index+":len:"+rhs_l);

            /* put us in the global collection of productions */
            _all.Add(_index, this);

            /* put us in the production list of the lhs non terminal */
            lhs_sym.add_production(this);
        }