-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal.cs
192 lines (153 loc) · 5.74 KB
/
terminal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
namespace CUP
{
using System;
/// <summary>This class represents a terminal symbol in the grammar. Each terminal
/// has a textual name, an index, and a string which indicates the type of
/// object it will be implemented with at runtime (i.e. the class of object
/// that will be returned by the scanner and pushed on the parse stack to
/// represent it).
/// *
/// </summary>
/// <version> last updated: 7/3/96
/// </version>
/// <author> Frank Flannery
///
/// </author>
public class terminal:symbol
{
/*-----------------------------------------------------------*/
/*--- Constructor(s) ----------------------------------------*/
/*-----------------------------------------------------------*/
/// <summary>Full constructor.
/// </summary>
/// <param name="nm">the name of the terminal.
/// </param>
/// <param name="tp">the type of the terminal.
///
/// </param>
public terminal(string nm, string tp, int precedence_side, int precedence_num):base(nm, tp)
{
/* add to set of all terminals and check for duplicates */
System.Object conflict = SupportClass.PutElement(_all, nm, this);
if (conflict != null)
(new internal_error("Duplicate terminal (" + nm + ") created")).crash();
/* assign a unique index */
_index = next_index++;
/* set the precedence */
_precedence_num = precedence_num;
_precedence_side = precedence_side;
/* add to by_index set */
SupportClass.PutElement(_all_by_index, _index, this);
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Constructor for non-precedented terminal
/// </summary>
public terminal(string nm, string tp):this(nm, tp, assoc.no_prec, - 1)
{
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Constructor with default type.
/// </summary>
/// <param name="nm">the name of the terminal.
///
/// </param>
public terminal(string nm):this(nm, null)
{
}
/*-----------------------------------------------------------*/
/*------------------- Class Variables ---------------------*/
/*-----------------------------------------------------------*/
private int _precedence_num;
private int _precedence_side;
/*-----------------------------------------------------------*/
/*--- (Access to) Static (Class) Variables ------------------*/
/*-----------------------------------------------------------*/
/// <summary>Table of all terminals. Elements are stored using name strings as
/// the key
/// </summary>
protected internal static System.Collections.Hashtable _all = new System.Collections.Hashtable();
/// <summary>Access to all terminals.
/// </summary>
public static System.Collections.IEnumerator all()
{
return _all.Values.GetEnumerator();
}
/// <summary>Lookup a terminal by name string.
/// </summary>
public static terminal find(string with_name)
{
if (with_name == null)
return null;
else
return (terminal) _all[with_name];
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Table of all terminals indexed by their index number.
/// </summary>
protected internal static System.Collections.Hashtable _all_by_index = new System.Collections.Hashtable();
/// <summary>Lookup a terminal by index.
/// </summary>
public static terminal find(int indx)
{
System.Int32 the_indx = indx;
return (terminal) _all_by_index[the_indx];
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Total number of terminals.
/// </summary>
public static int number()
{
return _all.Count;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Static counter to assign unique index.
/// </summary>
protected internal static int next_index = 0;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Special terminal for end of input.
/// </summary>
//UPGRADE_NOTE: Final was removed from the declaration of 'EOF '. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1003"'
public static terminal EOF = new terminal("EOF");
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>special terminal used for error recovery
/// </summary>
//UPGRADE_NOTE: Final was removed from the declaration of 'error '. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1003"'
public static terminal error = new terminal("error");
/*-----------------------------------------------------------*/
/*--- General Methods ---------------------------------------*/
/*-----------------------------------------------------------*/
/// <summary>Report this symbol as not being a non-terminal.
/// </summary>
public override bool is_non_term()
{
return false;
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>Convert to a string.
/// </summary>
public override string ToString()
{
//UPGRADE_TODO: The equivalent in .NET for method 'java.Object.toString' may return a different value. 'ms-help://MS.VSCC/commoner/redir/redirect.htm?keyword="jlca1043"'
return base.ToString() + "[" + index() + "]";
}
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
/// <summary>get the precedence of a terminal
/// </summary>
public virtual int precedence_num()
{
return _precedence_num;
}
public virtual int precedence_side()
{
return _precedence_side;
}
/// <summary>set the precedence of a terminal
/// </summary>
public virtual void set_precedence(int p, int new_prec)
{
_precedence_side = p;
_precedence_num = new_prec;
}
/*-----------------------------------------------------------*/
}
}