/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Remove all embedded actions from a production by factoring them /// out into individual action production using new non terminals. /// if the original production was: <pre> /// A ::= B {action1} C {action2} D /// </pre> /// then it will be factored into: <pre> /// A ::= B NT$1 C NT$2 D /// NT$1 ::= {action1} /// NT$2 ::= {action2} /// </pre> /// where NT$1 and NT$2 are new system created non terminals. /// </summary> /* the declarations added to the parent production are also passed along, * as they should be perfectly valid in this code string, since it * was originally a code string in the parent, not on its own. * frank 6/20/96 */ protected internal virtual void remove_embedded_actions() { non_terminal new_nt; production new_prod; System.String declare_str; /* walk over the production and process each action */ for (int act_loc = 0; act_loc < rhs_length(); act_loc++) { if (rhs(act_loc).is_action()) { declare_str = declare_labels(_rhs, act_loc, ""); /* create a new non terminal for the action production */ new_nt = non_terminal.create_new(); new_nt.is_embedded_action = true; /* 24-Mar-1998, CSA */ /* create a new production with just the action */ new_prod = new action_production(this, new_nt, null, 0, declare_str + ((action_part)rhs(act_loc)).code_string()); /* replace the action with the generated non terminal */ _rhs[act_loc] = new symbol_part(new_nt); } } }
/*-----------------------------------------------------------*/ /*--- Constructor(s) ----------------------------------------*/ /*-----------------------------------------------------------*/ /// <summary>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. /// </summary> public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, string action_str) { InitBlock(); int i; action_part tail_action; System.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() is 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++; /* put us in the global collection of productions */ SupportClass.PutElement(_all, _index, this); /* put us in the production list of the lhs non terminal */ lhs_sym.add_production(this); }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Equality comparison. /// </summary> public virtual bool equals(symbol_part other) { return(other != null && base.equals(other) && the_symbol().Equals(other.the_symbol())); }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Remove all embedded actions from a production by factoring them /// out into individual action production using new non terminals. /// if the original production was: <pre> /// A ::= B {action1} C {action2} D /// </pre> /// then it will be factored into: <pre> /// A ::= B NT$1 C NT$2 D /// NT$1 ::= {action1} /// NT$2 ::= {action2} /// </pre> /// where NT$1 and NT$2 are new system created non terminals. /// </summary> /* the declarations added to the parent production are also passed along, as they should be perfectly valid in this code string, since it was originally a code string in the parent, not on its own. frank 6/20/96 */ protected internal virtual void remove_embedded_actions() { non_terminal new_nt; production new_prod; System.String declare_str; /* walk over the production and process each action */ for (int act_loc = 0; act_loc < rhs_length(); act_loc++) if (rhs(act_loc).is_action()) { declare_str = declare_labels(_rhs, act_loc, ""); /* create a new non terminal for the action production */ new_nt = non_terminal.create_new(); new_nt.is_embedded_action = true; /* 24-Mar-1998, CSA */ /* create a new production with just the action */ new_prod = new action_production(this, new_nt, null, 0, declare_str + ((action_part) rhs(act_loc)).code_string()); /* replace the action with the generated non terminal */ _rhs[act_loc] = new symbol_part(new_nt); } }
/*-----------------------------------------------------------*/ /*--- Constructor(s) ----------------------------------------*/ /*-----------------------------------------------------------*/ /// <summary>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. /// </summary> public production(non_terminal lhs_sym, production_part[] rhs_parts, int rhs_l, string action_str) { InitBlock(); int i; action_part tail_action; System.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() is 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++; /* put us in the global collection of productions */ SupportClass.PutElement(_all, _index, this); /* put us in the production list of the lhs non terminal */ lhs_sym.add_production(this); }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Equality comparison. /// </summary> public virtual bool equals(symbol_part other) { return other != null && base.equals(other) && the_symbol().Equals(other.the_symbol()); }