Develop and Download Open Source Software

Browse CVS Repository

Contents of /netruby/netruby/skeleton.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1.1.1 - (show annotations) (download) (vendor branch)
Mon Apr 8 13:30:00 2002 UTC (22 years ago) by arton
Branch: MAIN, vendor
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
initial version 0.8

1 # jay skeleton
2
3 # character in column 1 determines outcome...
4 # # is a comment
5 # . is copied
6 # t is copied as //t if -t is set
7 # other lines are interpreted to call jay procedures
8
9 .// created by jay 0.7 (c) 1998 Axel.Schreiner@informatik.uni-osnabrueck.de
10 .
11 prolog ## %{ ... %} prior to the first %%
12
13 .
14 . /** simplified error message.
15 . @see <a href="#yyerror(java.lang.String, java.lang.String[])">yyerror</a>
16 . */
17 . public void yyerror (string message) {
18 . yyerror(message, null);
19 . }
20 .
21 . /** (syntax) error message.
22 . Can be overwritten to control message format.
23 . @param message text to be displayed.
24 . @param expected vector of acceptable tokens, if available.
25 . */
26 . public void yyerror (string message, string[] expected) {
27 . if ((expected != null) && (expected.Length > 0)) {
28 . StringBuilder sb = new StringBuilder();
29 . sb.AppendFormat("{0}, expecting", message);
30 . for (int n = 0; n < expected.Length; ++ n)
31 . sb.AppendFormat(" {0}", expected[n]);
32 . thread.CompileError(sb.ToString());
33 . } else
34 . thread.CompileError (message);
35 . }
36 .
37 . /** debugging support, requires the package jay.yydebug.
38 . Set to null to suppress debugging messages.
39 . */
40 t protected yydebug.yyDebug yydebug;
41 .
42 debug ## tables for debugging support
43 .
44 . /** index-checked interface to yyName[].
45 . @param token single character or %token value.
46 . @return token name or [illegal] or [unknown].
47 . */
48 t public static string yyname (int token) {
49 t if ((token < 0) || (token > yyName.Length)) return "[illegal]";
50 t string name;
51 t if ((name = yyName[token]) != null) return name;
52 t return "[unknown]";
53 t }
54 .
55 . /** computes list of expected tokens on error by tracing the tables.
56 . @param state for which to compute the list.
57 . @return list of token names.
58 . */
59 . protected string[] yyExpecting (int state) {
60 . int token, n, len = 0;
61 . bool[] ok = new bool[yyName.Length];
62 .
63 . if ((n = yySindex[state]) != 0)
64 . for (token = n < 0 ? -n : 0;
65 . (token < yyName.Length) && (n+token < yyTable.Length); ++ token)
66 . if (yyCheck[n+token] == token && !ok[token] && yyName[token] != null) {
67 . ++ len;
68 . ok[token] = true;
69 . }
70 . if ((n = yyRindex[state]) != 0)
71 . for (token = n < 0 ? -n : 0;
72 . (token < yyName.Length) && (n+token < yyTable.Length); ++ token)
73 . if (yyCheck[n+token] == token && !ok[token] && yyName[token] != null) {
74 . ++ len;
75 . ok[token] = true;
76 . }
77 .
78 . string [] result = new string[len];
79 . for (n = token = 0; n < len; ++ token)
80 . if (ok[token]) result[n++] = yyName[token];
81 . return result;
82 . }
83 .
84 . /** the generated parser, with debugging messages.
85 . Maintains a state and a value stack, currently with fixed maximum size.
86 . @param yyLex scanner.
87 . @param yydebug debug message writer implementing yyDebug, or null.
88 . @return result of the last reduction, if any.
89 . @throws yyException on irrecoverable parse error.
90 . */
91 . public Object yyparse (yyParser.yyInput yyLex, Object yydebug)
92 . {
93 t this.yydebug = (yydebug.yyDebug)yydebug;
94 . return yyparse(yyLex);
95 . }
96 .
97 . /** initial size and increment of the state/value stack [default 256].
98 . This is not final so that it can be overwritten outside of invocations
99 . of yyparse().
100 . */
101 . protected int yyMax;
102 .
103 . /** executed at the beginning of a reduce action.
104 . Used as $$ = yyDefault($1), prior to the user-specified action, if any.
105 . Can be overwritten to provide deep copy, etc.
106 . @param first value for $1, or null.
107 . @return first.
108 . */
109 . protected Object yyDefault (Object first) {
110 . return first;
111 . }
112 .
113 . /** the generated parser.
114 . Maintains a state and a value stack, currently with fixed maximum size.
115 . @param yyLex scanner.
116 . @return result of the last reduction, if any.
117 . @throws yyException on irrecoverable parse error.
118 . */
119 . public Object yyparse (yyParser.yyInput yyLex)
120 . {
121 . if (yyMax <= 0) yyMax = 256; // initial size
122 . int yyState = 0; // state stack ptr
123 . int [] yyStates = new int[yyMax]; // state stack
124 . Object yyVal = null; // value stack ptr
125 . Object [] yyVals = new Object[yyMax]; // value stack
126 . int yyToken = -1; // current input
127 . int yyErrorFlag = 0; // #tks to shift
128 .
129 local ## %{ ... %} after the first %%
130
131 . int yyTop = 0;
132 . goto skip;
133 . yyLoop:
134 . yyTop++;
135 . skip:
136 . for (;; ++ yyTop) {
137 . if (yyTop >= yyStates.Length) { // dynamically increase
138 . int[] i = new int[yyStates.Length+yyMax];
139 . System.Array.Copy(yyStates, i, 0);
140 . yyStates = i;
141 . Object[] o = new Object[yyVals.Length+yyMax];
142 . System.Array.Copy(yyVals, o, 0);
143 . yyVals = o;
144 . }
145 . yyStates[yyTop] = yyState;
146 . yyVals[yyTop] = yyVal;
147 t if (yydebug != null) yydebug.push(yyState, yyVal);
148 .
149 . yyDiscarded: for (;;) { // discarding a token does not change stack
150 . int yyN;
151 . if ((yyN = yyDefRed[yyState]) == 0) { // else [default] reduce (yyN)
152 . if (yyToken < 0) {
153 . yyToken = yyLex.advance() ? yyLex.token() : 0;
154 t if (yydebug != null)
155 t yydebug.lex(yyState, yyToken, yyname(yyToken), yyLex.value());
156 . }
157 . if ((yyN = yySindex[yyState]) != 0 && ((yyN += yyToken) >= 0)
158 . && (yyN < yyTable.Length) && (yyCheck[yyN] == yyToken)) {
159 t if (yydebug != null)
160 t yydebug.shift(yyState, yyTable[yyN], yyErrorFlag-1);
161 . yyState = yyTable[yyN]; // shift to yyN
162 . yyVal = yyLex.value();
163 . yyToken = -1;
164 . if (yyErrorFlag > 0) -- yyErrorFlag;
165 . goto yyLoop;
166 . }
167 . if ((yyN = yyRindex[yyState]) != 0 && (yyN += yyToken) >= 0
168 . && yyN < yyTable.Length && yyCheck[yyN] == yyToken)
169 . yyN = yyTable[yyN]; // reduce (yyN)
170 . else
171 . switch (yyErrorFlag) {
172 .
173 . case 0:
174 . yyerror("syntax error", yyExpecting(yyState));
175 t if (yydebug != null) yydebug.error("syntax error");
176 . goto case 1;
177 . case 1: case 2:
178 . yyErrorFlag = 3;
179 . do {
180 . if ((yyN = yySindex[yyStates[yyTop]]) != 0
181 . && (yyN += Token.yyErrorCode) >= 0 && yyN < yyTable.Length
182 . && yyCheck[yyN] == Token.yyErrorCode) {
183 t if (yydebug != null)
184 t yydebug.shift(yyStates[yyTop], yyTable[yyN], 3);
185 . yyState = yyTable[yyN];
186 . yyVal = yyLex.value();
187 . goto yyLoop;
188 . }
189 t if (yydebug != null) yydebug.pop(yyStates[yyTop]);
190 . } while (-- yyTop >= 0);
191 t if (yydebug != null) yydebug.reject();
192 . throw new yyParser.yyException("irrecoverable syntax error");
193 .
194 . case 3:
195 . if (yyToken == 0) {
196 t if (yydebug != null) yydebug.reject();
197 . throw new yyParser.yyException("irrecoverable syntax error at end-of-file");
198 . }
199 t if (yydebug != null)
200 t yydebug.discard(yyState, yyToken, yyname(yyToken),
201 t yyLex.value());
202 . yyToken = -1;
203 . goto yyDiscarded; // leave stack alone
204 . }
205 . }
206 . int yyV = yyTop + 1-yyLen[yyN];
207 t if (yydebug != null)
208 t yydebug.reduce(yyState, yyStates[yyV-1], yyN, yyRule[yyN], yyLen[yyN]);
209 . yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
210 . switch (yyN) {
211
212 actions ## code from the actions within the grammar
213
214 . }
215 . yyTop -= yyLen[yyN];
216 . yyState = yyStates[yyTop];
217 . int yyM = yyLhs[yyN];
218 . if (yyState == 0 && yyM == 0) {
219 t if (yydebug != null) yydebug.shift(0, yyFinal);
220 . yyState = yyFinal;
221 . if (yyToken < 0) {
222 . yyToken = yyLex.advance() ? yyLex.token() : 0;
223 t if (yydebug != null)
224 t yydebug.lex(yyState, yyToken,yyname(yyToken), yyLex.value());
225 . }
226 . if (yyToken == 0) {
227 t if (yydebug != null) yydebug.accept(yyVal);
228 . return yyVal;
229 . }
230 . goto yyLoop;
231 . }
232 . if (((yyN = yyGindex[yyM]) != 0) && ((yyN += yyState) >= 0)
233 . && (yyN < yyTable.Length) && (yyCheck[yyN] == yyState))
234 . yyState = yyTable[yyN];
235 . else
236 . yyState = yyDgoto[yyM];
237 t if (yydebug != null) yydebug.shift(yyStates[yyTop], yyState);
238 . goto yyLoop;
239 . }
240 . }
241 . }
242 .
243 tables ## tables for rules, default reduction, and action calls
244 .
245 epilog ## text following second %%
246 .namespace yydebug {
247 . using System;
248 . public interface yyDebug {
249 . void push (int state, Object value);
250 . void lex (int state, int token, string name, Object value);
251 . void shift (int from, int to, int errorFlag);
252 . void pop (int state);
253 . void discard (int state, int token, string name, Object value);
254 . void reduce (int from, int to, int rule, string text, int len);
255 . void shift (int from, int to);
256 . void accept (Object value);
257 . void error (string message);
258 . void reject ();
259 . }
260 .
261 . class yyDebugSimple : yyDebug {
262 . void println (string s){
263 . Console.WriteLine (s);
264 . }
265 .
266 . public void push (int state, Object value) {
267 . println ("push\tstate "+state+"\tvalue "+value);
268 . }
269 .
270 . public void lex (int state, int token, string name, Object value) {
271 . println("lex\tstate "+state+"\treading "+name+"\tvalue "+value);
272 . }
273 .
274 . public void shift (int from, int to, int errorFlag) {
275 . switch (errorFlag) {
276 . default: // normally
277 . println("shift\tfrom state "+from+" to "+to);
278 . break;
279 . case 0: case 1: case 2: // in error recovery
280 . println("shift\tfrom state "+from+" to "+to
281 . +"\t"+errorFlag+" left to recover");
282 . break;
283 . case 3: // normally
284 . println("shift\tfrom state "+from+" to "+to+"\ton error");
285 . break;
286 . }
287 . }
288 .
289 . public void pop (int state) {
290 . println("pop\tstate "+state+"\ton error");
291 . }
292 .
293 . public void discard (int state, int token, string name, Object value) {
294 . println("discard\tstate "+state+"\ttoken "+name+"\tvalue "+value);
295 . }
296 .
297 . public void reduce (int from, int to, int rule, string text, int len) {
298 . println("reduce\tstate "+from+"\tuncover "+to
299 . +"\trule ("+rule+") "+text);
300 . }
301 .
302 . public void shift (int from, int to) {
303 . println("goto\tfrom state "+from+" to "+to);
304 . }
305 .
306 . public void accept (Object value) {
307 . println("accept\tvalue "+value);
308 . }
309 .
310 . public void error (string message) {
311 . println("error\t"+message);
312 . }
313 .
314 . public void reject () {
315 . println("reject");
316 . }
317 .
318 . }
319 .}
320 .// %token constants
321 . class Token {
322 tokens public const int
323 . }
324 . namespace yyParser {
325 . using System;
326 . /** thrown for irrecoverable syntax errors and stack overflow.
327 . */
328 . public class yyException : System.Exception {
329 . public yyException (string message) : base (message) {
330 . }
331 . }
332 .
333 . /** must be implemented by a scanner object to supply input to the parser.
334 . */
335 . public interface yyInput {
336 . /** move on to next token.
337 . @return false if positioned beyond tokens.
338 . @throws IOException on input error.
339 . */
340 . bool advance (); // throws java.io.IOException;
341 . /** classifies current token.
342 . Should not be called if advance() returned false.
343 . @return current %token or single character.
344 . */
345 . int token ();
346 . /** associated with current token.
347 . Should not be called if advance() returned false.
348 . @return value for token().
349 . */
350 . Object value ();
351 . }
352 . }
353 .} // close outermost namespace, that MUST HAVE BEEN opened in the prolog

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26