/*
* STANDARD ML OF NEW JERSEY COPYRIGHT NOTICE, LICENSE AND DISCLAIMER.
*
* Copyright (c) 1989-1998 by Lucent Technologies
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted, provided
* that the above copyright notice appear in all copies and that both the
* copyright notice and this permission notice and warranty disclaimer appear
* in supporting documentation, and that the name of Lucent Technologies, Bell
* Labs or any Lucent entity not be used in advertising or publicity pertaining
* to distribution of the software without specific, written prior permission.
*
* Lucent disclaims all warranties with regard to this software, including all
* implied warranties of merchantability and fitness. In no event shall Lucent
* be liable for any special, indirect or consequential damages or any damages
* whatsoever resulting from loss of use, data or profits, whether in an action
* of contract, negligence or other tortious action, arising out of or in
* connection with the use or performance of this software.
*
* Taken from this URL:
* http://www.smlnj.org/license.html
*
* This license is compatible with the GNU GPL (see section "Standard ML of New
* Jersey Copyright License"):
* http://www.gnu.org/licenses/license-list.html#StandardMLofNJ
*/
/*
* Copyright 1996-1999 by Scott Hudson, Frank Flannery, C. Scott Ananian
*/
package weka.core.parser.java_cup;
import java.util.Enumeration;
/** This class represents the complete "action" table of the parser.
* It has one row for each state in the parse machine, and a column for
* each terminal symbol. Each entry in the table represents a shift,
* reduce, or an error.
*
* @see weka.core.parser.java_cup.parse_action
* @see weka.core.parser.java_cup.parse_action_row
* @version last updated: 11/25/95
* @author Scott Hudson
*/
public class parse_action_table {
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
/** Simple constructor. All terminals, non-terminals, and productions must
* already have been entered, and the viable prefix recognizer should
* have been constructed before this is called.
*/
public parse_action_table()
{
/* determine how many states we are working with */
_num_states = lalr_state.number();
/* allocate the array and fill it in with empty rows */
under_state = new parse_action_row[_num_states];
for (int i=0; i<_num_states; i++)
under_state[i] = new parse_action_row();
}
/*-----------------------------------------------------------*/
/*--- (Access to) Instance Variables ------------------------*/
/*-----------------------------------------------------------*/
/** How many rows/states are in the machine/table. */
protected int _num_states;
/** How many rows/states are in the machine/table. */
public int num_states() {return _num_states;}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/** Actual array of rows, one per state. */
public parse_action_row[] under_state;
/*-----------------------------------------------------------*/
/*--- General Methods ---------------------------------------*/
/*-----------------------------------------------------------*/
/** Check the table to ensure that all productions have been reduced.
* Issue a warning message (to System.err) for each production that
* is never reduced.
*/
public void check_reductions()
throws internal_error
{
parse_action act;
production prod;
/* tabulate reductions -- look at every table entry */
for (int row = 0; row < num_states(); row++)
{
for (int col = 0; col < parse_action_row.size(); col++)
{
/* look at the action entry to see if its a reduce */
act = under_state[row].under_term[col];
if (act != null && act.kind() == parse_action.REDUCE)
{
/* tell production that we used it */
((reduce_action)act).reduce_with().note_reduction_use();
}
}
}
/* now go across every production and make sure we hit it */
for (Enumeration p = production.all(); p.hasMoreElements(); )
{
prod = (production)p.nextElement();
/* if we didn't hit it give a warning */
if (prod.num_reductions() == 0)
{
/* count it *
emit.not_reduced++;
/* give a warning if they haven't been turned off */
if (!emit.nowarn)
{
ErrorManager.getManager().emit_warning("*** Production \"" +
prod.to_simple_string() + "\" never reduced");
}
}
}
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*
/** Convert to a string. */
public String toString()
{
String result;
int cnt;
result = "-------- ACTION_TABLE --------\n";
for (int row = 0; row < num_states(); row++)
{
result += "From state #" + row + "\n";
cnt = 0;
for (int col = 0; col < parse_action_row.size(); col++)
{
/* if the action is not an error print it */
if (under_state[row].under_term[col].kind() != parse_action.ERROR)
{
result += " [term " + col + ":" + under_state[row].under_term[col] + "]";
/* end the line after the 2nd one */
cnt++;
if (cnt == 2)
{
result += "\n";
cnt = 0;
}
}
}
/* finish the line if we haven't just done that */
if (cnt != 0) result += "\n";
}
result += "------------------------------";
return result;
}
/*-----------------------------------------------------------*/
}