Source code: java_cup/symbol.java
1 package java_cup;
2
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).
10 *
11 * @see java_cup.terminal
12 * @see java_cup.non_terminal
13 * @version last updated: 7/3/96
14 * @author Frank Flannery
15 */
16 public abstract class symbol {
17 /*-----------------------------------------------------------*/
18 /*--- Constructor(s) ----------------------------------------*/
19 /*-----------------------------------------------------------*/
20
21 /** Full constructor.
22 * @param nm the name of the symbol.
23 * @param tp a string with the type name.
24 */
25 public symbol(String nm, String tp)
26 {
27 /* sanity check */
28 if (nm == null) nm = "";
29
30 /* apply default if no type given */
31 if (tp == null) tp = "Object";
32
33 _name = nm;
34 _stack_type = tp;
35 }
36
37 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
38
39 /** Constructor with default type.
40 * @param nm the name of the symbol.
41 */
42 public symbol(String nm)
43 {
44 this(nm, null);
45 }
46
47 /*-----------------------------------------------------------*/
48 /*--- (Access to) Instance Variables ------------------------*/
49 /*-----------------------------------------------------------*/
50
51 /** String for the human readable name of the symbol. */
52 protected String _name;
53
54 /** String for the human readable name of the symbol. */
55 public String name() {return _name;}
56
57 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
58
59 /** String for the type of object used for the symbol on the parse stack. */
60 protected String _stack_type;
61
62 /** String for the type of object used for the symbol on the parse stack. */
63 public String stack_type() {return _stack_type;}
64
65 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
66
67 /** Count of how many times the symbol appears in productions. */
68 protected int _use_count = 0;
69
70 /** Count of how many times the symbol appears in productions. */
71 public int use_count() {return _use_count;}
72
73 /** Increment the use count. */
74 public void note_use() {_use_count++;}
75
76 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
77
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.
81 */
82 protected int _index;
83
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.
87 */
88 public int index() {return _index;}
89
90 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
91
92 /** Indicate if this is a non-terminal. Here in the base class we
93 * don't know, so this is abstract.
94 */
95 public abstract boolean is_non_term();
96
97 /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
98
99 /** Convert to a string. */
100 public String toString()
101 {
102 return name();
103 }
104
105 /*-----------------------------------------------------------*/
106
107 }