3 /** This abstract class serves as the base class for grammar symbols (i.e.,
4 * both terminals and non-terminals). Each symbol has a name string, and
5 * a string giving the type of object that the symbol will be represented by
6 * on the runtime parse stack. In addition, each symbol maintains a use count
7 * in order to detect symbols that are declared but never used, and an index
8 * number that indicates where it appears in parse tables (index numbers are
9 * unique within terminals or non terminals, but not across both).
11 * @see java_cup.terminal
12 * @see java_cup.non_terminal
13 * @version last updated: 7/3/96
14 * @author Frank Flannery
16 public abstract class symbol {
17 /*-----------------------------------------------------------*/
18 /*--- Constructor(s) ----------------------------------------*/
19 /*-----------------------------------------------------------*/
22 * @param nm the name of the symbol.
23 * @param tp a string with the type name.
25 public symbol(String nm, String tp)
28 if (nm == null) nm = "";
30 /* apply default if no type given */
31 if (tp == null) tp = "Object";
37 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
39 /** Constructor with default type.
40 * @param nm the name of the symbol.
42 public symbol(String nm)
47 /*-----------------------------------------------------------*/
48 /*--- (Access to) Instance Variables ------------------------*/
49 /*-----------------------------------------------------------*/
51 /** String for the human readable name of the symbol. */
52 protected String _name;
54 /** String for the human readable name of the symbol. */
55 public String name() {return _name;}
57 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
59 /** String for the type of object used for the symbol on the parse stack. */
60 protected String _stack_type;
62 /** String for the type of object used for the symbol on the parse stack. */
63 public String stack_type() {return _stack_type;}
65 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
67 /** Count of how many times the symbol appears in productions. */
68 protected int _use_count = 0;
70 /** Count of how many times the symbol appears in productions. */
71 public int use_count() {return _use_count;}
73 /** Increment the use count. */
74 public void note_use() {_use_count++;}
76 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
78 /** Index of this symbol (terminal or non terminal) in the parse tables.
79 * Note: indexes are unique among terminals and unique among non terminals,
80 * however, a terminal may have the same index as a non-terminal, etc.
84 /** Index of this symbol (terminal or non terminal) in the parse tables.
85 * Note: indexes are unique among terminals and unique among non terminals,
86 * however, a terminal may have the same index as a non-terminal, etc.
88 public int index() {return _index;}
90 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
92 /** Indicate if this is a non-terminal. Here in the base class we
93 * don't know, so this is abstract.
95 public abstract boolean is_non_term();
97 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
99 /** Convert to a string. */
100 public String toString()
105 /*-----------------------------------------------------------*/