in util/src/main/java/java_cup/emit.java [549:646]
protected static void do_action_table(
PrintWriter out,
parse_action_table act_tab,
boolean compact_reduces)
throws internal_error
{
parse_action_row row;
parse_action act;
int red;
long start_time = System.currentTimeMillis();
/* 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*row.size()];
int nentries = 0;
/* do each column */
for (int j = 0; j < 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);
}
/* reduce actions get negated entries of production# + 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 */
}
/* shouldn't be anything else */
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];
System.arraycopy(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.println();
out.println(" /** Parse-action table. */");
out.println(" protected static final short[][] _action_table = ");
out.print (" unpackFromStrings(");
do_table_as_string(out, action_table);
out.println(");");
/* do the public accessor method */
out.println();
out.println(" /** Access to parse-action table. */");
out.println(" public short[][] action_table() {return _action_table;}");
action_table_time = System.currentTimeMillis() - start_time;
}