Beispiel #1
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <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);
        }
Beispiel #2
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <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;
        }
Beispiel #3
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <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()));
        }
Beispiel #4
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <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());
        }
Beispiel #5
0
 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 /// <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());
 }
Beispiel #6
0
 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 /// <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());
 }
Beispiel #7
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/

        /// <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;
        }