/*-----------------------------------------------------------*/ /*--- Constructor(s) ----------------------------------------*/ /*-----------------------------------------------------------*/ /// <summary>Full constructor. /// </summary> /// <param name="sym">the symbol that this part is made up of. /// </param> /// <param name="lab">an optional label string for the part. /// /// </param> public symbol_part(symbol sym, string lab) : base(lab) { if (sym == null) throw new internal_error("Attempt to construct a symbol_part with a null symbol"); _the_symbol = sym; }
/*-----------------------------------------------------------*/ /*--- General Methods ---------------------------------------*/ /*-----------------------------------------------------------*/ /// <summary>Add a transition out of this state to another. /// </summary> /// <param name="on_sym">the symbol the transition is under. /// </param> /// <param name="to_st"> the state the transition goes to. /// /// </param> public virtual void add_transition(symbol on_sym, lalr_state to_st) { lalr_transition trans; /* create a new transition object and put it in our list */ trans = new lalr_transition(on_sym, to_st, _transitions); _transitions = trans; }
/*-----------------------------------------------------------*/ /*--- Constructor(s) ----------------------------------------*/ /*-----------------------------------------------------------*/ /// <summary>Full constructor. /// </summary> /// <param name="sym">the symbol that this part is made up of. /// </param> /// <param name="lab">an optional label string for the part. /// /// </param> public symbol_part(symbol sym, string lab) : base(lab) { if (sym == null) { throw new internal_error("Attempt to construct a symbol_part with a null symbol"); } _the_symbol = sym; }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Add a single symbol to the set. /// </summary> /// <param name="sym">the symbol we are adding. /// </param> /// <returns>true if this changes the set. /// /// </returns> public virtual bool add(symbol sym) { System.Object previous; not_null(sym); /* put the object in */ previous = SupportClass.PutElement(_all, sym.name_Renamed_Method(), sym); /* if we had a previous, this is no change */ return previous == null; }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Add a single symbol to the set. /// </summary> /// <param name="sym">the symbol we are adding. /// </param> /// <returns>true if this changes the set. /// /// </returns> public virtual bool add(symbol sym) { System.Object previous; not_null(sym); /* put the object in */ previous = SupportClass.PutElement(_all, sym.name_Renamed_Method(), sym); /* if we had a previous, this is no change */ return(previous == null); }
/*-----------------------------------------------------------*/ /*--- Constructor(s) ----------------------------------------*/ /*-----------------------------------------------------------*/ /// <summary>Full constructor. /// </summary> /// <param name="on_sym"> symbol we are transitioning on. /// </param> /// <param name="to_st"> state we transition to. /// </param> /// <param name="nxt"> next transition in linked list. /// /// </param> public lalr_transition(symbol on_sym, lalr_state to_st, lalr_transition nxt) { /* sanity checks */ if (on_sym == null) throw new internal_error("Attempt to create transition on null symbol"); if (to_st == null) throw new internal_error("Attempt to create transition to null state"); /* initialize */ _on_symbol = on_sym; _to_state = to_st; _next = nxt; }
/*-----------------------------------------------------------*/ /*--- Constructor(s) ----------------------------------------*/ /*-----------------------------------------------------------*/ /// <summary>Full constructor. /// </summary> /// <param name="on_sym"> symbol we are transitioning on. /// </param> /// <param name="to_st"> state we transition to. /// </param> /// <param name="nxt"> next transition in linked list. /// /// </param> public lalr_transition(symbol on_sym, lalr_state to_st, lalr_transition nxt) { /* sanity checks */ if (on_sym == null) { throw new internal_error("Attempt to create transition on null symbol"); } if (to_st == null) { throw new internal_error("Attempt to create transition to null state"); } /* initialize */ _on_symbol = on_sym; _to_state = to_st; _next = nxt; }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Determine if the set contains a particular symbol. /// </summary> /// <param name="sym">the symbol we are looking for. /// /// </param> public virtual bool contains(symbol sym) { return(_all.ContainsKey(sym.name_Renamed_Method())); }
/*-----------------------------------------------------------*/ /*--- Constructor(s) ----------------------------------------*/ /*-----------------------------------------------------------*/ /// <summary>Full constructor. /// </summary> /// <param name="prod">production this item uses. /// </param> /// <param name="pos"> position of the "dot" within the item. /// /// </param> public lr_item_core(production prod, int pos) { // symbol after_dot = null; production_part part; if (prod == null) throw new internal_error("Attempt to create an lr_item_core with a null production"); _the_production = prod; if (pos < 0 || pos > _the_production.rhs_length()) throw new internal_error("Attempt to create an lr_item_core with a bad dot position"); _dot_pos = pos; /* compute and cache hash code now */ _core_hash_cache = 13 * _the_production.GetHashCode() + pos; /* cache the symbol after the dot */ if (_dot_pos < _the_production.rhs_length()) { part = _the_production.rhs(_dot_pos); if (!part.is_action()) _symbol_after_dot = ((symbol_part) part).the_symbol(); } }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Constructor with no label. /// </summary> /// <param name="sym">the symbol that this part is made up of. /// /// </param> public symbol_part(symbol sym) : this(sym, null) { }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Remove a single symbol if it is in the set. /// </summary> /// <param name="sym">the symbol we are removing. /// /// </param> public virtual void remove(symbol sym) { not_null(sym); _all.Remove(sym.name_Renamed_Method()); }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Emit code for the non-public class holding the actual action code. /// </summary> /// <param name="out"> stream to produce output on. /// </param> /// <param name="start_prod">the start production of the grammar. /// /// </param> protected internal static void emit_action_code(System.IO.StreamWriter out_Renamed, production start_prod) { production prod; long start_time = (System.DateTime.Now.Ticks - 621355968000000000) / 10000; /* class header */ out_Renamed.WriteLine(); out_Renamed.WriteLine("/** Cup generated class to encapsulate user supplied action code.*/"); out_Renamed.WriteLine("public class " + pre("actions") + " {"); /* user supplied code */ if (action_code != null) { out_Renamed.WriteLine(); out_Renamed.WriteLine(action_code); } /* field for parser object */ out_Renamed.WriteLine(" private " + parser_class_name + " parser;"); /* constructor */ out_Renamed.WriteLine(); out_Renamed.WriteLine(" /** Constructor */"); out_Renamed.WriteLine(" public " + pre("actions") + "(" + parser_class_name + " parser) {"); out_Renamed.WriteLine(" this.parser = parser;"); out_Renamed.WriteLine(" }"); /* action method head */ out_Renamed.WriteLine(); out_Renamed.WriteLine(" /** Method with the actual generated action code. */"); out_Renamed.WriteLine(" public CUP.runtime.Symbol " + pre("do_action") + "("); out_Renamed.WriteLine(" int " + pre("act_num,")); out_Renamed.WriteLine(" CUP.runtime.lr_parser " + pre("parser,")); out_Renamed.WriteLine(" CUP.runtime.SymbolStack " + pre("stack,")); out_Renamed.WriteLine(" int " + pre("top)")); out_Renamed.WriteLine(" {"); /* declaration of result symbol */ /* New declaration!! now return Symbol * 6/13/96 frankf */ out_Renamed.WriteLine(" /* Symbol object for return from actions */"); out_Renamed.WriteLine(" CUP.runtime.Symbol " + pre("result") + ";"); out_Renamed.WriteLine(); /* switch top */ out_Renamed.WriteLine(" /* select the action based on the action number */"); out_Renamed.WriteLine(" switch (" + pre("act_num") + ")"); out_Renamed.WriteLine(" {"); /* emit action code for each production as a separate case */ for (System.Collections.IEnumerator p = production.all(); p.MoveNext();) { prod = (production)p.Current; /* case label */ out_Renamed.WriteLine(" /*. . . . . . . . . . . . . . . . . . . .*/"); out_Renamed.WriteLine(" case " + prod.index() + ": // " + prod.to_simple_string()); /* give them their own block to work in */ out_Renamed.WriteLine(" {"); /* create the result symbol */ /*make the variable RESULT which will point to the new Symbol (see below) * and be changed by action code * 6/13/96 frankf */ string strSymType = prod.lhs().the_symbol().stack_type().Trim(); string strPrimitives = "int;float;double;short;char;byte;decimal;sbyte;bool;ushort;uint;long;ulong"; if (strPrimitives.IndexOf(strSymType) > -1) { string init = ""; switch (strSymType) { case "double": init = " = 0.0"; break; case "int": case "uint": case "short": case "ushort": case "byte": case "sbyte": init = " = 0"; break; case "float": init = " = 0.0f"; break; case "decimal": init = " = decimal.Zero"; break; case "char": init = " = '\\0'"; break; case "long": case "ulong": init = " = 0L"; break; } if (strSymType == "double") { init = " = 0.0"; } out_Renamed.WriteLine(" " + strSymType + " RESULT " + init + ";"); } else { out_Renamed.WriteLine(" " + strSymType + " RESULT = null;"); } /* Add code to propagate RESULT assignments that occur in * action code embedded in a production (ie, non-rightmost * action code). 24-Mar-1998 CSA */ for (int i = 0; i < prod.rhs_length(); i++) { // only interested in non-terminal symbols. if (!(prod.rhs(i) is symbol_part)) { continue; } symbol s = ((symbol_part)prod.rhs(i)).the_symbol(); if (!(s is non_terminal)) { continue; } // skip this non-terminal unless it corresponds to // an embedded action production. if (((non_terminal)s).is_embedded_action == false) { continue; } // OK, it fits. Make a conditional assignment to RESULT. int index = prod.rhs_length() - i - 1; // last rhs is on top. out_Renamed.WriteLine(" " + "// propagate RESULT from " + s.name_Renamed_Method()); out_Renamed.WriteLine(" " + "if ( " + "((CUP.runtime.Symbol) " + emit.pre("stack") + ".Peek(" + emit.pre("top") + "-" + index + ")).Value != null )"); out_Renamed.WriteLine(" " + "RESULT = " + "(" + prod.lhs().the_symbol().stack_type() + ") " + "((CUP.runtime.Symbol) " + emit.pre("stack") + ".Peek(" + emit.pre("top") + "-" + index + ")).Value;"); } /* if there is an action string, emit it */ if (prod.action() != null && prod.action().code_string() != null && !prod.action().Equals("")) { out_Renamed.WriteLine(prod.action().code_string()); } /* here we have the left and right values being propagated. * must make this a command line option. * frankf 6/18/96 */ /* Create the code that assigns the left and right values of * the new Symbol that the production is reducing to */ if (emit.lr_values()) { int loffset; System.String leftstring, rightstring; int roffset = 0; rightstring = "((CUP.runtime.Symbol)" + emit.pre("stack") + ".Peek(" + emit.pre("top") + "-" + roffset + ")).right"; if (prod.rhs_length() == 0) { leftstring = rightstring; } else { loffset = prod.rhs_length() - 1; leftstring = "((CUP.runtime.Symbol)" + emit.pre("stack") + ".Peek(" + emit.pre("top") + "-" + loffset + ")).left"; } out_Renamed.WriteLine(" " + pre("result") + " = new CUP.runtime.Symbol(" + prod.lhs().the_symbol().index() + "/*" + prod.lhs().the_symbol().name_Renamed_Method() + "*/" + ", " + leftstring + ", " + rightstring + ", RESULT);"); } else { out_Renamed.WriteLine(" " + pre("result") + " = new CUP.runtime.Symbol(" + prod.lhs().the_symbol().index() + "/*" + prod.lhs().the_symbol().name_Renamed_Method() + "*/" + ", RESULT);"); } /* end of their block */ out_Renamed.WriteLine(" }"); /* if this was the start production, do action for accept */ if (prod == start_prod) { out_Renamed.WriteLine(" /* ACCEPT */"); out_Renamed.WriteLine(" " + pre("parser") + ".done_parsing();"); } /* code to return lhs symbol */ out_Renamed.WriteLine(" return " + pre("result") + ";"); out_Renamed.WriteLine(); } /* end of switch */ out_Renamed.WriteLine(" /* . . . . . .*/"); out_Renamed.WriteLine(" default:"); out_Renamed.WriteLine(" throw new Exception("); out_Renamed.WriteLine(" \"Invalid action number found in " + "internal parse table\");"); out_Renamed.WriteLine(); out_Renamed.WriteLine(" }"); /* end of method */ out_Renamed.WriteLine(" }"); /* end of class */ out_Renamed.WriteLine("}"); out_Renamed.WriteLine(); action_code_time = (System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start_time; }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Determine if the set contains a particular symbol. /// </summary> /// <param name="sym">the symbol we are looking for. /// /// </param> public virtual bool contains(symbol sym) { return _all.ContainsKey(sym.name_Renamed_Method()); }
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/ /// <summary>Constructor with null next. /// </summary> /// <param name="on_sym"> symbol we are transitioning on. /// </param> /// <param name="to_st"> state we transition to. /// /// </param> public lalr_transition(symbol on_sym, lalr_state to_st) : this(on_sym, to_st, null) { }