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

        /// <summary>Emit the action table.
        /// </summary>
        /// <param name="out">            stream to produce output on.
        /// </param>
        /// <param name="act_tab">        the internal representation of the action table.
        /// </param>
        /// <param name="compact_reduces">do we use the most frequent reduce as default?
        ///
        /// </param>
        protected internal static void  do_action_table(System.IO.StreamWriter out_Renamed, parse_action_table act_tab, bool compact_reduces)
        {
            parse_action_row row;
            parse_action     act;
            int red;

            long start_time = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* collect values for the action table */
            short[][] action_table = new short[act_tab.num_states()][];
            /* do each state (row) of the action table */
            for (int i = 0; i < act_tab.num_states(); i++)
            {
                /* get the row */
                row = act_tab.under_state[i];

                /* determine the default for the row */
                if (compact_reduces)
                {
                    row.compute_default();
                }
                else
                {
                    row.default_reduce = -1;
                }

                /* make temporary table for the row. */
                short[] temp_table = new short[2 * CUP.parse_action_row.size()];
                int     nentries   = 0;

                /* do each column */
                for (int j = 0; j < CUP.parse_action_row.size(); j++)
                {
                    /* extract the action from the table */
                    act = row.under_term[j];

                    /* skip error entries these are all defaulted out */
                    if (act.kind() != parse_action.ERROR)
                    {
                        /* first put in the symbol index, then the actual entry */

                        /* shifts get positive entries of state number + 1 */
                        if (act.kind() == parse_action.SHIFT)
                        {
                            /* make entry */
                            temp_table[nentries++] = (short)j;
                            temp_table[nentries++] = (short)(((shift_action)act).shift_to().index() + 1);
                        }
                        else if (act.kind() == parse_action.REDUCE)
                        {
                            /* if its the default entry let it get defaulted out */
                            red = ((reduce_action)act).reduce_with().index();
                            if (red != row.default_reduce)
                            {
                                /* make entry */
                                temp_table[nentries++] = (short)j;
                                temp_table[nentries++] = (short)(-(red + 1));
                            }
                        }
                        else if (act.kind() == parse_action.NONASSOC)
                        {
                            /* do nothing, since we just want a syntax error */
                        }
                        else
                        {
                            throw new internal_error("Unrecognized action code " + act.kind() + " found in parse table");
                        }
                    }
                }

                /* now we know how big to make the row */
                action_table[i] = new short[nentries + 2];
                Array.Copy(temp_table, 0, action_table[i], 0, nentries);

                /* finish off the row with a default entry */
                action_table[i][nentries++] = -1;
                if (row.default_reduce != -1)
                {
                    action_table[i][nentries++] = (short)(-(row.default_reduce + 1));
                }
                else
                {
                    action_table[i][nentries++] = 0;
                }
            }

            /* finish off the init of the table */
            out_Renamed.WriteLine();
            out_Renamed.WriteLine("  /** Parse-action table. */");
            out_Renamed.Write("  private static string[] _strActionTable = ");
            do_table_as_string(out_Renamed, action_table);
            out_Renamed.WriteLine(";");
            out_Renamed.WriteLine("  protected static short[][] _action_table = CUP.runtime.lr_parser.unpackFromStrings(_strActionTable);");
            // out_Renamed.Write("    unpackFromStrings(");
            // do_table_as_string(out_Renamed, action_table);
            // out_Renamed.WriteLine(");");

            /* do the public accessor method */
            out_Renamed.WriteLine();
            out_Renamed.WriteLine("  /** Access to parse-action table. */");
            out_Renamed.WriteLine("  public override short[][] action_table() {return _action_table;}");

            action_table_time = (System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start_time;
        }
Beispiel #2
0
        /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
        /// <summary>Emit the action table. 
        /// </summary>
        /// <param name="out">            stream to produce output on.
        /// </param>
        /// <param name="act_tab">        the internal representation of the action table.
        /// </param>
        /// <param name="compact_reduces">do we use the most frequent reduce as default?
        /// 
        /// </param>
        protected internal static void do_action_table(System.IO.StreamWriter out_Renamed, parse_action_table act_tab, bool compact_reduces)
        {
            parse_action_row row;
            parse_action act;
            int red;

            long start_time = (System.DateTime.Now.Ticks - 621355968000000000) / 10000;

            /* collect values for the action table */
            short[][] action_table = new short[act_tab.num_states()][];
            /* do each state (row) of the action table */
             for (int i = 0; i < act_tab.num_states(); i++)
            {
                /* get the row */
                row = act_tab.under_state[i];

                /* determine the default for the row */
                if (compact_reduces)
                    row.compute_default();
                else
                    row.default_reduce = - 1;

                /* make temporary table for the row. */
                short[] temp_table = new short[2 * CUP.parse_action_row.size()];
                int nentries = 0;

                /* do each column */
                 for (int j = 0; j < CUP.parse_action_row.size(); j++)
                {
                    /* extract the action from the table */
                    act = row.under_term[j];

                    /* skip error entries these are all defaulted out */
                    if (act.kind() != parse_action.ERROR)
                    {
                        /* first put in the symbol index, then the actual entry */

                        /* shifts get positive entries of state number + 1 */
                        if (act.kind() == parse_action.SHIFT)
                        {
                            /* make entry */
                            temp_table[nentries++] = (short) j;
                            temp_table[nentries++] = (short) (((shift_action) act).shift_to().index() + 1);
                        }
                        else if (act.kind() == parse_action.REDUCE)
                        {
                            /* if its the default entry let it get defaulted out */
                            red = ((reduce_action) act).reduce_with().index();
                            if (red != row.default_reduce)
                            {
                                /* make entry */
                                temp_table[nentries++] = (short) j;
                                temp_table[nentries++] = (short) (- (red + 1));
                            }
                        }
                        else if (act.kind() == parse_action.NONASSOC)
                        {
                            /* do nothing, since we just want a syntax error */
                        }
                        else
                            throw new internal_error("Unrecognized action code " + act.kind() + " found in parse table");
                    }
                }

                /* now we know how big to make the row */
                action_table[i] = new short[nentries + 2];
                Array.Copy(temp_table, 0, action_table[i], 0, nentries);

                /* finish off the row with a default entry */
                action_table[i][nentries++] = - 1;
                if (row.default_reduce != - 1)
                    action_table[i][nentries++] = (short) (- (row.default_reduce + 1));
                else
                    action_table[i][nentries++] = 0;
            }

            /* finish off the init of the table */
            out_Renamed.WriteLine();
            out_Renamed.WriteLine("  /** Parse-action table. */");
            out_Renamed.Write("  private static string[] _strActionTable = ");
            do_table_as_string(out_Renamed, action_table);
            out_Renamed.WriteLine(";");
            out_Renamed.WriteLine("  protected static short[][] _action_table = CUP.runtime.lr_parser.unpackFromStrings(_strActionTable);");
            // out_Renamed.Write("    unpackFromStrings(");
            // do_table_as_string(out_Renamed, action_table);
            // out_Renamed.WriteLine(");");

            /* do the public accessor method */
            out_Renamed.WriteLine();
            out_Renamed.WriteLine("  /** Access to parse-action table. */");
            out_Renamed.WriteLine("  public override short[][] action_table() {return _action_table;}");

            action_table_time = (System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start_time;
        }