~ubuntu-branches/ubuntu/raring/weka/raring

« back to all changes in this revision

Viewing changes to weka/core/parser/java_cup/parse_action_table.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner, Soeren Sonnenburg, Torsten Werner
  • Date: 2008-08-10 21:27:05 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080810212705-tr8etpnkdx2ziktp
Tags: 3.5.8-1
[ Soeren Sonnenburg ]
* Bump Standards Version to 3.8.0.
* Remove references to non-free Java in debian/copyright.

[ Torsten Werner ]
* new upstream release
* Switch to openjdk-6.
* Move package to main.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * STANDARD ML OF NEW JERSEY COPYRIGHT NOTICE, LICENSE AND DISCLAIMER.
 
3
 * 
 
4
 * Copyright (c) 1989-1998 by Lucent Technologies
 
5
 * 
 
6
 * Permission to use, copy, modify, and distribute this software and its
 
7
 * documentation for any purpose and without fee is hereby granted, provided
 
8
 * that the above copyright notice appear in all copies and that both the
 
9
 * copyright notice and this permission notice and warranty disclaimer appear
 
10
 * in supporting documentation, and that the name of Lucent Technologies, Bell
 
11
 * Labs or any Lucent entity not be used in advertising or publicity pertaining
 
12
 * to distribution of the software without specific, written prior permission.
 
13
 *
 
14
 * Lucent disclaims all warranties with regard to this software, including all
 
15
 * implied warranties of merchantability and fitness. In no event shall Lucent
 
16
 * be liable for any special, indirect or consequential damages or any damages
 
17
 * whatsoever resulting from loss of use, data or profits, whether in an action
 
18
 * of contract, negligence or other tortious action, arising out of or in
 
19
 * connection with the use or performance of this software. 
 
20
 *
 
21
 * Taken from this URL:
 
22
 * http://www.smlnj.org/license.html
 
23
 * 
 
24
 * This license is compatible with the GNU GPL (see section "Standard ML of New
 
25
 * Jersey Copyright License"):
 
26
 * http://www.gnu.org/licenses/license-list.html#StandardMLofNJ
 
27
 */
 
28
 
 
29
/*
 
30
 * Copyright 1996-1999 by Scott Hudson, Frank Flannery, C. Scott Ananian
 
31
 */
 
32
 
 
33
package weka.core.parser.java_cup;
 
34
 
 
35
import java.util.Enumeration;
 
36
 
 
37
/** This class represents the complete "action" table of the parser. 
 
38
 *  It has one row for each state in the parse machine, and a column for
 
39
 *  each terminal symbol.  Each entry in the table represents a shift,
 
40
 *  reduce, or an error.  
 
41
 *
 
42
 * @see     weka.core.parser.java_cup.parse_action
 
43
 * @see     weka.core.parser.java_cup.parse_action_row
 
44
 * @version last updated: 11/25/95
 
45
 * @author  Scott Hudson
 
46
 */
 
47
public class parse_action_table {
 
48
 
 
49
  /*-----------------------------------------------------------*/
 
50
  /*--- Constructor(s) ----------------------------------------*/
 
51
  /*-----------------------------------------------------------*/
 
52
 
 
53
  /** Simple constructor.  All terminals, non-terminals, and productions must 
 
54
   *  already have been entered, and the viable prefix recognizer should
 
55
   *  have been constructed before this is called.
 
56
   */
 
57
  public parse_action_table()
 
58
    {
 
59
      /* determine how many states we are working with */
 
60
      _num_states = lalr_state.number();
 
61
 
 
62
      /* allocate the array and fill it in with empty rows */
 
63
      under_state = new parse_action_row[_num_states];
 
64
      for (int i=0; i<_num_states; i++)
 
65
        under_state[i] = new parse_action_row();
 
66
    }
 
67
 
 
68
  /*-----------------------------------------------------------*/
 
69
  /*--- (Access to) Instance Variables ------------------------*/
 
70
  /*-----------------------------------------------------------*/
 
71
 
 
72
  /** How many rows/states are in the machine/table. */
 
73
  protected int _num_states;
 
74
 
 
75
  /** How many rows/states are in the machine/table. */
 
76
  public int num_states() {return _num_states;}
 
77
 
 
78
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
 
79
 
 
80
  /** Actual array of rows, one per state. */
 
81
  public  parse_action_row[] under_state;
 
82
 
 
83
  /*-----------------------------------------------------------*/
 
84
  /*--- General Methods ---------------------------------------*/
 
85
  /*-----------------------------------------------------------*/
 
86
 
 
87
  /** Check the table to ensure that all productions have been reduced. 
 
88
   *  Issue a warning message (to System.err) for each production that
 
89
   *  is never reduced.
 
90
   */
 
91
  public void check_reductions()
 
92
    throws internal_error
 
93
    {
 
94
      parse_action act;
 
95
      production   prod;
 
96
 
 
97
      /* tabulate reductions -- look at every table entry */
 
98
      for (int row = 0; row < num_states(); row++)
 
99
        {
 
100
          for (int col = 0; col < parse_action_row.size(); col++)
 
101
            {
 
102
              /* look at the action entry to see if its a reduce */
 
103
              act = under_state[row].under_term[col];
 
104
              if (act != null && act.kind() == parse_action.REDUCE)
 
105
                {
 
106
                  /* tell production that we used it */
 
107
                  ((reduce_action)act).reduce_with().note_reduction_use();
 
108
                }
 
109
            }
 
110
        }
 
111
 
 
112
      /* now go across every production and make sure we hit it */
 
113
      for (Enumeration p = production.all(); p.hasMoreElements(); )
 
114
        {
 
115
          prod = (production)p.nextElement();
 
116
 
 
117
          /* if we didn't hit it give a warning */
 
118
          if (prod.num_reductions() == 0)
 
119
            {
 
120
              /* count it *
 
121
              emit.not_reduced++;
 
122
 
 
123
              /* give a warning if they haven't been turned off */
 
124
              if (!emit.nowarn)
 
125
                {
 
126
 
 
127
                  ErrorManager.getManager().emit_warning("*** Production \"" + 
 
128
                                  prod.to_simple_string() + "\" never reduced");
 
129
                }
 
130
            }
 
131
        }
 
132
    }
 
133
 
 
134
  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*
 
135
 
 
136
  /** Convert to a string. */
 
137
  public String toString()
 
138
    {
 
139
      String result;
 
140
      int cnt;
 
141
 
 
142
      result = "-------- ACTION_TABLE --------\n";
 
143
      for (int row = 0; row < num_states(); row++)
 
144
        {
 
145
          result += "From state #" + row + "\n";
 
146
          cnt = 0;
 
147
          for (int col = 0; col < parse_action_row.size(); col++)
 
148
            {
 
149
              /* if the action is not an error print it */ 
 
150
              if (under_state[row].under_term[col].kind() != parse_action.ERROR)
 
151
                {
 
152
                  result += " [term " + col + ":" + under_state[row].under_term[col] + "]";
 
153
 
 
154
                  /* end the line after the 2nd one */
 
155
                  cnt++;
 
156
                  if (cnt == 2)
 
157
                    {
 
158
                      result += "\n";
 
159
                      cnt = 0;
 
160
                    }
 
161
                }
 
162
            }
 
163
          /* finish the line if we haven't just done that */
 
164
          if (cnt != 0) result += "\n";
 
165
        }
 
166
      result += "------------------------------";
 
167
 
 
168
      return result;
 
169
    }
 
170
 
 
171
  /*-----------------------------------------------------------*/
 
172
 
 
173
}
 
174