3 # character in column 1 determines outcome...
6 # t is copied as //t if -t is set
7 # other lines are interpreted to call jay procedures
9 .// created by jay 0.7 (c) 1998 Axel.Schreiner@informatik.uni-osnabrueck.de
11 prolog ## %{ ... %} prior to the first %%
14 . /** error output stream.
15 . It should be changeable.
17 . public System.IO.TextWriter ErrorOutput = System.Console.Out;
19 . /** simplified error message.
20 . @see <a href="#yyerror(java.lang.String, java.lang.String[])">yyerror</a>
22 . public void yyerror (string message) {
23 . yyerror(message, null);
27 . public int eof_token;
29 . /** (syntax) error message.
30 . Can be overwritten to control message format.
31 . @param message text to be displayed.
32 . @param expected vector of acceptable tokens, if available.
34 . public void yyerror (string message, string[] expected) {
35 . if ((yacc_verbose_flag > 0) && (expected != null) && (expected.Length > 0)) {
36 . ErrorOutput.Write (message+", expecting");
37 . for (int n = 0; n < expected.Length; ++ n)
38 . ErrorOutput.Write (" "+expected[n]);
39 . ErrorOutput.WriteLine ();
41 . ErrorOutput.WriteLine (message);
44 . /** debugging support, requires the package jay.yydebug.
45 . Set to null to suppress debugging messages.
47 t internal yydebug.yyDebug debug;
49 debug ## tables for debugging support
51 . /** index-checked interface to yyNames[].
52 . @param token single character or %token value.
53 . @return token name or [illegal] or [unknown].
55 t public static string yyname (int token) {
56 t if ((token < 0) || (token > yyNames.Length)) return "[illegal]";
58 t if ((name = yyNames[token]) != null) return name;
62 . int yyExpectingState;
63 . /** computes list of expected tokens on error by tracing the tables.
64 . @param state for which to compute the list.
65 . @return list of token names.
67 . protected int [] yyExpectingTokens (int state){
68 . int token, n, len = 0;
69 . bool[] ok = new bool[yyNames.Length];
70 . if ((n = yySindex[state]) != 0)
71 . for (token = n < 0 ? -n : 0;
72 . (token < yyNames.Length) && (n+token < yyTable.Length); ++ token)
73 . if (yyCheck[n+token] == token && !ok[token] && yyNames[token] != null) {
77 . if ((n = yyRindex[state]) != 0)
78 . for (token = n < 0 ? -n : 0;
79 . (token < yyNames.Length) && (n+token < yyTable.Length); ++ token)
80 . if (yyCheck[n+token] == token && !ok[token] && yyNames[token] != null) {
84 . int [] result = new int [len];
85 . for (n = token = 0; n < len; ++ token)
86 . if (ok[token]) result[n++] = token;
89 . protected string[] yyExpecting (int state) {
90 . int [] tokens = yyExpectingTokens (state);
91 . string [] result = new string[tokens.Length];
92 . for (int n = 0; n < tokens.Length; n++)
93 . result[n++] = yyNames[tokens [n]];
97 . /** the generated parser, with debugging messages.
98 . Maintains a state and a value stack, currently with fixed maximum size.
99 . @param yyLex scanner.
100 . @param yydebug debug message writer implementing yyDebug, or null.
101 . @return result of the last reduction, if any.
102 . @throws yyException on irrecoverable parse error.
104 . internal Object yyparse (yyParser.yyInput yyLex, Object yyd)
106 t this.debug = (yydebug.yyDebug)yyd;
107 . return yyparse(yyLex);
110 . /** initial size and increment of the state/value stack [default 256].
111 . This is not final so that it can be overwritten outside of invocations
114 . protected int yyMax;
116 . /** executed at the beginning of a reduce action.
117 . Used as $$ = yyDefault($1), prior to the user-specified action, if any.
118 . Can be overwritten to provide deep copy, etc.
119 . @param first value for $1, or null.
122 . protected Object yyDefault (Object first) {
126 . static int[] global_yyStates;
127 . static object[] global_yyVals;
128 . protected bool use_global_stacks;
129 . object[] yyVals; // value stack
130 . object yyVal; // value stack ptr
131 . int yyToken; // current input
134 . /** the generated parser.
135 . Maintains a state and a value stack, currently with fixed maximum size.
136 . @param yyLex scanner.
137 . @return result of the last reduction, if any.
138 . @throws yyException on irrecoverable parse error.
140 . internal Object yyparse (yyParser.yyInput yyLex)
142 . if (yyMax <= 0) yyMax = 256; // initial size
143 . int yyState = 0; // state stack ptr
144 . int [] yyStates; // state stack
147 . int yyErrorFlag = 0; // #tks to shift
148 . if (use_global_stacks && global_yyStates != null) {
149 . yyVals = global_yyVals;
150 . yyStates = global_yyStates;
152 . yyVals = new object [yyMax];
153 . yyStates = new int [yyMax];
154 . if (use_global_stacks) {
155 . global_yyVals = yyVals;
156 . global_yyStates = yyStates;
160 local ## %{ ... %} after the first %%
162 . /*yyLoop:*/ for (yyTop = 0;; ++ yyTop) {
163 . if (yyTop >= yyStates.Length) { // dynamically increase
164 . global::System.Array.Resize (ref yyStates, yyStates.Length+yyMax);
165 . global::System.Array.Resize (ref yyVals, yyVals.Length+yyMax);
167 . yyStates[yyTop] = yyState;
168 . yyVals[yyTop] = yyVal;
169 t if (debug != null) debug.push(yyState, yyVal);
171 . /*yyDiscarded:*/ while (true) { // discarding a token does not change stack
173 . if ((yyN = yyDefRed[yyState]) == 0) { // else [default] reduce (yyN)
175 . yyToken = yyLex.advance() ? yyLex.token() : 0;
178 t debug.lex(yyState, yyToken, yyname(yyToken), yyLex.value());
180 . if ((yyN = yySindex[yyState]) != 0 && ((yyN += yyToken) >= 0)
181 . && (yyN < yyTable.Length) && (yyCheck[yyN] == yyToken)) {
183 t debug.shift(yyState, yyTable[yyN], yyErrorFlag-1);
184 . yyState = yyTable[yyN]; // shift to yyN
185 . yyVal = yyLex.value();
187 . if (yyErrorFlag > 0) -- yyErrorFlag;
188 . goto continue_yyLoop;
190 . if ((yyN = yyRindex[yyState]) != 0 && (yyN += yyToken) >= 0
191 . && yyN < yyTable.Length && yyCheck[yyN] == yyToken)
192 . yyN = yyTable[yyN]; // reduce (yyN)
194 . switch (yyErrorFlag) {
197 . yyExpectingState = yyState;
198 . // yyerror(String.Format ("syntax error, got token `{0}'", yyname (yyToken)), yyExpecting(yyState));
199 t if (debug != null) debug.error("syntax error");
200 . if (yyToken == 0 /*eof*/ || yyToken == eof_token) throw new yyParser.yyUnexpectedEof ();
205 . if ((yyN = yySindex[yyStates[yyTop]]) != 0
206 . && (yyN += Token.yyErrorCode) >= 0 && yyN < yyTable.Length
207 . && yyCheck[yyN] == Token.yyErrorCode) {
209 t debug.shift(yyStates[yyTop], yyTable[yyN], 3);
210 . yyState = yyTable[yyN];
211 . yyVal = yyLex.value();
212 . goto continue_yyLoop;
214 t if (debug != null) debug.pop(yyStates[yyTop]);
215 . } while (-- yyTop >= 0);
216 t if (debug != null) debug.reject();
217 . throw new yyParser.yyException("irrecoverable syntax error");
220 . if (yyToken == 0) {
221 t if (debug != null) debug.reject();
222 . throw new yyParser.yyException("irrecoverable syntax error at end-of-file");
225 t debug.discard(yyState, yyToken, yyname(yyToken),
228 . goto continue_yyDiscarded; // leave stack alone
231 . int yyV = yyTop + 1-yyLen[yyN];
233 t debug.reduce(yyState, yyStates[yyV-1], yyN, YYRules.getRule (yyN), yyLen[yyN]);
234 . yyVal = yyV > yyTop ? null : yyVals[yyV]; // yyVal = yyDefault(yyV > yyTop ? null : yyVals[yyV]);
237 actions ## code from the actions within the grammar
240 . yyTop -= yyLen[yyN];
241 . yyState = yyStates[yyTop];
242 . int yyM = yyLhs[yyN];
243 . if (yyState == 0 && yyM == 0) {
244 t if (debug != null) debug.shift(0, yyFinal);
247 . yyToken = yyLex.advance() ? yyLex.token() : 0;
250 t debug.lex(yyState, yyToken,yyname(yyToken), yyLex.value());
252 . if (yyToken == 0) {
253 t if (debug != null) debug.accept(yyVal);
256 . goto continue_yyLoop;
258 . if (((yyN = yyGindex[yyM]) != 0) && ((yyN += yyState) >= 0)
259 . && (yyN < yyTable.Length) && (yyCheck[yyN] == yyState))
260 . yyState = yyTable[yyN];
262 . yyState = yyDgoto[yyM];
263 t if (debug != null) debug.shift(yyStates[yyTop], yyState);
264 . goto continue_yyLoop;
265 . continue_yyDiscarded: ; // implements the named-loop continue: 'continue yyDiscarded'
267 . continue_yyLoop: ; // implements the named-loop continue: 'continue yyLoop'
271 tables ## tables for rules, default reduction, and action calls
273 epilog ## text following second %%
276 . internal interface yyDebug {
277 . void push (int state, Object value);
278 . void lex (int state, int token, string name, Object value);
279 . void shift (int from, int to, int errorFlag);
280 . void pop (int state);
281 . void discard (int state, int token, string name, Object value);
282 . void reduce (int from, int to, int rule, string text, int len);
283 . void shift (int from, int to);
284 . void accept (Object value);
285 . void error (string message);
289 . class yyDebugSimple : yyDebug {
290 . void println (string s){
291 . Console.Error.WriteLine (s);
294 . public void push (int state, Object value) {
295 . println ("push\tstate "+state+"\tvalue "+value);
298 . public void lex (int state, int token, string name, Object value) {
299 . println("lex\tstate "+state+"\treading "+name+"\tvalue "+value);
302 . public void shift (int from, int to, int errorFlag) {
303 . switch (errorFlag) {
304 . default: // normally
305 . println("shift\tfrom state "+from+" to "+to);
307 . case 0: case 1: case 2: // in error recovery
308 . println("shift\tfrom state "+from+" to "+to
309 . +"\t"+errorFlag+" left to recover");
311 . case 3: // normally
312 . println("shift\tfrom state "+from+" to "+to+"\ton error");
317 . public void pop (int state) {
318 . println("pop\tstate "+state+"\ton error");
321 . public void discard (int state, int token, string name, Object value) {
322 . println("discard\tstate "+state+"\ttoken "+name+"\tvalue "+value);
325 . public void reduce (int from, int to, int rule, string text, int len) {
326 . println("reduce\tstate "+from+"\tuncover "+to
327 . +"\trule ("+rule+") "+text);
330 . public void shift (int from, int to) {
331 . println("goto\tfrom state "+from+" to "+to);
334 . public void accept (Object value) {
335 . println("accept\tvalue "+value);
338 . public void error (string message) {
339 . println("error\t"+message);
342 . public void reject () {
350 tokens public const int
352 . namespace yyParser {
354 . /** thrown for irrecoverable syntax errors and stack overflow.
356 . internal class yyException : System.Exception {
357 . public yyException (string message) : base (message) {
360 . internal class yyUnexpectedEof : yyException {
361 . public yyUnexpectedEof (string message) : base (message) {
363 . public yyUnexpectedEof () : base ("") {
367 . /** must be implemented by a scanner object to supply input to the parser.
369 . internal interface yyInput {
370 . /** move on to next token.
371 . @return false if positioned beyond tokens.
372 . @throws IOException on input error.
374 . bool advance (); // throws java.io.IOException;
375 . /** classifies current token.
376 . Should not be called if advance() returned false.
377 . @return current %token or single character.
380 . /** associated with current token.
381 . Should not be called if advance() returned false.
382 . @return value for token().
387 .} // close outermost namespace, that MUST HAVE BEEN opened in the prolog