CI: don't require Autoconf 2.71
[bison.git] / data / skeletons / lalr1.java
blob1bbecca1382c7d00ec34d7b69b702347519a898b
1 # Java skeleton for Bison -*- java -*-
3 # Copyright (C) 2007-2015, 2018-2021 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18 m4_include(b4_skeletonsdir/[java.m4])
20 b4_header_if([b4_complain([%header/%defines does not make sense in Java])])
22 m4_define([b4_symbol_no_destructor_assert],
23 [b4_symbol_if([$1], [has_destructor],
24 [b4_complain_at(m4_unquote(b4_symbol([$1], [destructor_loc])),
25 [%destructor does not make sense in Java])])])
26 b4_symbol_foreach([b4_symbol_no_destructor_assert])
28 ## --------------- ##
29 ## api.push-pull. ##
30 ## --------------- ##
32 b4_percent_define_default([[api.push-pull]], [[pull]])
33 b4_percent_define_check_values([[[[api.push-pull]],
34 [[pull]], [[push]], [[both]]]])
36 # Define m4 conditional macros that encode the value
37 # of the api.push-pull flag.
38 b4_define_flag_if([pull]) m4_define([b4_pull_flag], [[1]])
39 b4_define_flag_if([push]) m4_define([b4_push_flag], [[1]])
40 m4_case(b4_percent_define_get([[api.push-pull]]),
41 [pull], [m4_define([b4_push_flag], [[0]])],
42 [push], [m4_define([b4_pull_flag], [[0]])])
44 # Define a macro to be true when api.push-pull has the value "both".
45 m4_define([b4_both_if],[b4_push_if([b4_pull_if([$1],[$2])],[$2])])
47 # Handle BISON_USE_PUSH_FOR_PULL for the test suite. So that push parsing
48 # tests function as written, do not let BISON_USE_PUSH_FOR_PULL modify the
49 # behavior of Bison at all when push parsing is already requested.
50 b4_define_flag_if([use_push_for_pull])
51 b4_use_push_for_pull_if([
52 b4_push_if([m4_define([b4_use_push_for_pull_flag], [[0]])],
53 [m4_define([b4_push_flag], [[1]])])])
55 # Define a macro to encapsulate the parse state variables. This
56 # allows them to be defined either in parse() when doing pull parsing,
57 # or as class instance variable when doing push parsing.
58 m4_define([b4_define_state],
60 /* Lookahead token kind. */
61 int yychar = YYEMPTY_;
62 /* Lookahead symbol kind. */
63 SymbolKind yytoken = null;
65 /* State. */
66 int yyn = 0;
67 int yylen = 0;
68 int yystate = 0;
69 YYStack yystack = new YYStack ();
70 int label = YYNEWSTATE;
72 ]b4_locations_if([[
73 /* The location where the error started. */
74 ]b4_location_type[ yyerrloc = null;
76 /* Location. */
77 ]b4_location_type[ yylloc = new ]b4_location_type[ (null, null);]])[
79 /* Semantic value of the lookahead. */
80 ]b4_yystype[ yylval = null;
81 ]])
83 # parse.lac
84 b4_percent_define_default([[parse.lac]], [[none]])
85 b4_percent_define_check_values([[[[parse.lac]], [[full]], [[none]]]])
86 b4_define_flag_if([lac])
87 m4_define([b4_lac_flag],
88 [m4_if(b4_percent_define_get([[parse.lac]]),
89 [none], [[0]], [[1]])])
92 ## ------------- ##
93 ## Parser File. ##
94 ## ------------- ##
96 b4_output_begin([b4_parser_file_name])[
97 ]b4_copyright([Skeleton implementation for Bison LALR(1) parsers in Java],
98 [2007-2015, 2018-2021])[
99 ]b4_disclaimer[
100 ]b4_percent_define_ifdef([api.package], [package b4_percent_define_get([api.package]);[
101 ]])[
102 ]b4_user_pre_prologue[
103 ]b4_user_post_prologue[
104 import java.text.MessageFormat;
105 import java.util.ArrayList;
106 ]b4_percent_code_get([[imports]])[
108 * A Bison parser, automatically generated from <tt>]m4_bpatsubst(b4_file_name, [^"\(.*\)"$], [\1])[</tt>.
110 * @@author LALR (1) parser skeleton written by Paolo Bonzini.
112 ]b4_parser_class_declaration[
114 ]b4_identification[
116 ]b4_parse_error_bmatch(
117 [detailed\|verbose], [[
119 * True if verbose error messages are enabled.
121 private boolean yyErrorVerbose = true;
124 * Whether verbose error messages are enabled.
126 public final boolean getErrorVerbose() { return yyErrorVerbose; }
129 * Set the verbosity of error messages.
130 * @@param verbose True to request verbose error messages.
132 public final void setErrorVerbose(boolean verbose)
133 { yyErrorVerbose = verbose; }
134 ]])[
136 ]b4_locations_if([[
138 * A class defining a pair of positions. Positions, defined by the
139 * <code>]b4_position_type[</code> class, denote a point in the input.
140 * Locations represent a part of the input through the beginning
141 * and ending positions.
143 public static class ]b4_location_type[ {
145 * The first, inclusive, position in the range.
147 public ]b4_position_type[ begin;
150 * The first position beyond the range.
152 public ]b4_position_type[ end;
155 * Create a <code>]b4_location_type[</code> denoting an empty range located at
156 * a given point.
157 * @@param loc The position at which the range is anchored.
159 public ]b4_location_type[ (]b4_position_type[ loc) {
160 this.begin = this.end = loc;
164 * Create a <code>]b4_location_type[</code> from the endpoints of the range.
165 * @@param begin The first position included in the range.
166 * @@param end The first position beyond the range.
168 public ]b4_location_type[ (]b4_position_type[ begin, ]b4_position_type[ end) {
169 this.begin = begin;
170 this.end = end;
174 * Print a representation of the location. For this to be correct,
175 * <code>]b4_position_type[</code> should override the <code>equals</code>
176 * method.
178 public String toString() {
179 if (begin.equals (end))
180 return begin.toString();
181 else
182 return begin.toString() + "-" + end.toString();
186 private ]b4_location_type[ yylloc(YYStack rhs, int n)
188 if (0 < n)
189 return new ]b4_location_type[(rhs.locationAt(n-1).begin, rhs.locationAt(0).end);
190 else
191 return new ]b4_location_type[(rhs.locationAt(0).end);
192 }]])[
194 ]b4_declare_symbol_enum[
197 * Communication interface between the scanner and the Bison-generated
198 * parser <tt>]b4_parser_class[</tt>.
200 public interface Lexer {
201 ]b4_token_enums[
202 /** Deprecated, use ]b4_symbol(eof, id)[ instead. */
203 public static final int EOF = ]b4_symbol(eof, id)[;
204 ]b4_pull_if([b4_locations_if([[
206 * Method to retrieve the beginning position of the last scanned token.
207 * @@return the position at which the last scanned token starts.
209 ]b4_position_type[ getStartPos();
212 * Method to retrieve the ending position of the last scanned token.
213 * @@return the first position beyond the last scanned token.
215 ]b4_position_type[ getEndPos();]])[
218 * Method to retrieve the semantic value of the last scanned token.
219 * @@return the semantic value of the last scanned token.
221 ]b4_yystype[ getLVal();
224 * Entry point for the scanner. Returns the token identifier corresponding
225 * to the next token and prepares to return the semantic value
226 * ]b4_locations_if([and beginning/ending positions ])[of the token.
227 * @@return the token identifier corresponding to the next token.
229 int yylex()]b4_maybe_throws([b4_lex_throws])[;
230 ]])[
232 * Emit an error]b4_locations_if([ referring to the given location])[in a user-defined way.
234 *]b4_locations_if([[ @@param loc The location of the element to which the
235 * error message is related.]])[
236 * @@param msg The string for the error message.
238 void yyerror(]b4_locations_if([b4_location_type[ loc, ]])[String msg);
240 ]b4_parse_error_bmatch(
241 [custom], [[
243 * Build and emit a "syntax error" message in a user-defined way.
245 * @@param ctx The context of the error.
247 void reportSyntaxError(Context ctx);
248 ]])[
251 ]b4_lexer_if([[
252 private class YYLexer implements Lexer {
253 ]b4_percent_code_get([[lexer]])[
256 ]])[
258 * The object doing lexical analysis for us.
260 private Lexer yylexer;
262 ]b4_parse_param_vars[
264 ]b4_lexer_if([[
266 * Instantiates the Bison-generated parser.
268 public ]b4_parser_class[(]b4_parse_param_decl([b4_lex_param_decl])[)]b4_maybe_throws([b4_init_throws])[
270 ]b4_percent_code_get([[init]])[]b4_lac_if([[
271 this.yylacStack = new ArrayList<Integer>();
272 this.yylacEstablished = false;]])[
273 this.yylexer = new YYLexer(]b4_lex_param_call[);
274 ]b4_parse_param_cons[
276 ]])[
279 * Instantiates the Bison-generated parser.
280 * @@param yylexer The scanner that will supply tokens to the parser.
282 ]b4_lexer_if([[protected]], [[public]]) b4_parser_class[(]b4_parse_param_decl([[Lexer yylexer]])[)]b4_maybe_throws([b4_init_throws])[
284 ]b4_percent_code_get([[init]])[]b4_lac_if([[
285 this.yylacStack = new ArrayList<Integer>();
286 this.yylacEstablished = false;]])[
287 this.yylexer = yylexer;
288 ]b4_parse_param_cons[
291 ]b4_parse_trace_if([[
292 private java.io.PrintStream yyDebugStream = System.err;
295 * The <tt>PrintStream</tt> on which the debugging output is printed.
297 public final java.io.PrintStream getDebugStream() { return yyDebugStream; }
300 * Set the <tt>PrintStream</tt> on which the debug output is printed.
301 * @@param s The stream that is used for debugging output.
303 public final void setDebugStream(java.io.PrintStream s) { yyDebugStream = s; }
305 private int yydebug = 0;
308 * Answer the verbosity of the debugging output; 0 means that all kinds of
309 * output from the parser are suppressed.
311 public final int getDebugLevel() { return yydebug; }
314 * Set the verbosity of the debugging output; 0 means that all kinds of
315 * output from the parser are suppressed.
316 * @@param level The verbosity level for debugging output.
318 public final void setDebugLevel(int level) { yydebug = level; }
319 ]])[
321 private int yynerrs = 0;
324 * The number of syntax errors so far.
326 public final int getNumberOfErrors() { return yynerrs; }
329 * Print an error message via the lexer.
330 *]b4_locations_if([[ Use a <code>null</code> location.]])[
331 * @@param msg The error message.
333 public final void yyerror(String msg) {
334 yylexer.yyerror(]b4_locations_if([[(]b4_location_type[)null, ]])[msg);
336 ]b4_locations_if([[
338 * Print an error message via the lexer.
339 * @@param loc The location associated with the message.
340 * @@param msg The error message.
342 public final void yyerror(]b4_location_type[ loc, String msg) {
343 yylexer.yyerror(loc, msg);
347 * Print an error message via the lexer.
348 * @@param pos The position associated with the message.
349 * @@param msg The error message.
351 public final void yyerror(]b4_position_type[ pos, String msg) {
352 yylexer.yyerror(new ]b4_location_type[ (pos), msg);
353 }]])[
354 ]b4_parse_trace_if([[
355 protected final void yycdebugNnl(String s) {
356 if (0 < yydebug)
357 yyDebugStream.print(s);
360 protected final void yycdebug(String s) {
361 if (0 < yydebug)
362 yyDebugStream.println(s);
363 }]])[
365 private final class YYStack {
366 private int[] stateStack = new int[16];]b4_locations_if([[
367 private ]b4_location_type[[] locStack = new ]b4_location_type[[16];]])[
368 private ]b4_yystype[[] valueStack = new ]b4_yystype[[16];
370 public int size = 16;
371 public int height = -1;
373 public final void push(int state, ]b4_yystype[ value]b4_locations_if([, ]b4_location_type[ loc])[) {
374 height++;
375 if (size == height) {
376 int[] newStateStack = new int[size * 2];
377 System.arraycopy(stateStack, 0, newStateStack, 0, height);
378 stateStack = newStateStack;]b4_locations_if([[
379 ]b4_location_type[[] newLocStack = new ]b4_location_type[[size * 2];
380 System.arraycopy(locStack, 0, newLocStack, 0, height);
381 locStack = newLocStack;]])
383 b4_yystype[[] newValueStack = new ]b4_yystype[[size * 2];
384 System.arraycopy(valueStack, 0, newValueStack, 0, height);
385 valueStack = newValueStack;
387 size *= 2;
390 stateStack[height] = state;]b4_locations_if([[
391 locStack[height] = loc;]])[
392 valueStack[height] = value;
395 public final void pop() {
396 pop(1);
399 public final void pop(int num) {
400 // Avoid memory leaks... garbage collection is a white lie!
401 if (0 < num) {
402 java.util.Arrays.fill(valueStack, height - num + 1, height + 1, null);]b4_locations_if([[
403 java.util.Arrays.fill(locStack, height - num + 1, height + 1, null);]])[
405 height -= num;
408 public final int stateAt(int i) {
409 return stateStack[height - i];
411 ]b4_locations_if([[
413 public final ]b4_location_type[ locationAt(int i) {
414 return locStack[height - i];
416 ]])[
417 public final ]b4_yystype[ valueAt(int i) {
418 return valueStack[height - i];
421 // Print the state stack on the debug stream.
422 public void print(java.io.PrintStream out) {
423 out.print ("Stack now");
425 for (int i = 0; i <= height; i++) {
426 out.print(' ');
427 out.print(stateStack[i]);
429 out.println();
434 * Returned by a Bison action in order to stop the parsing process and
435 * return success (<tt>true</tt>).
437 public static final int YYACCEPT = 0;
440 * Returned by a Bison action in order to stop the parsing process and
441 * return failure (<tt>false</tt>).
443 public static final int YYABORT = 1;
445 ]b4_push_if([
447 * Returned by a Bison action in order to request a new token.
449 public static final int YYPUSH_MORE = 4;])[
452 * Returned by a Bison action in order to start error recovery without
453 * printing an error message.
455 public static final int YYERROR = 2;
458 * Internal return codes that are not supported for user semantic
459 * actions.
461 private static final int YYERRLAB = 3;
462 private static final int YYNEWSTATE = 4;
463 private static final int YYDEFAULT = 5;
464 private static final int YYREDUCE = 6;
465 private static final int YYERRLAB1 = 7;
466 private static final int YYRETURN = 8;
467 ]b4_push_if([[ private static final int YYGETTOKEN = 9; /* Signify that a new token is expected when doing push-parsing. */]])[
469 private int yyerrstatus_ = 0;
471 ]b4_push_if([b4_define_state])[
473 * Whether error recovery is being done. In this state, the parser
474 * reads token until it reaches a known state, and then restarts normal
475 * operation.
477 public final boolean recovering ()
479 return yyerrstatus_ == 0;
482 /** Compute post-reduction state.
483 * @@param yystate the current state
484 * @@param yysym the nonterminal to push on the stack
486 private int yyLRGotoState(int yystate, int yysym) {
487 int yyr = yypgoto_[yysym - YYNTOKENS_] + yystate;
488 if (0 <= yyr && yyr <= YYLAST_ && yycheck_[yyr] == yystate)
489 return yytable_[yyr];
490 else
491 return yydefgoto_[yysym - YYNTOKENS_];
494 private int yyaction(int yyn, YYStack yystack, int yylen)]b4_maybe_throws([b4_throws])[
496 /* If YYLEN is nonzero, implement the default value of the action:
497 '$$ = $1'. Otherwise, use the top of the stack.
499 Otherwise, the following line sets YYVAL to garbage.
500 This behavior is undocumented and Bison
501 users should not rely upon it. */
502 ]b4_yystype[ yyval = (0 < yylen) ? yystack.valueAt(yylen - 1) : yystack.valueAt(0);]b4_locations_if([[
503 ]b4_location_type[ yyloc = yylloc(yystack, yylen);]])[]b4_parse_trace_if([[
505 yyReducePrint(yyn, yystack);]])[
507 switch (yyn)
509 ]b4_user_actions[
510 default: break;
511 }]b4_parse_trace_if([[
513 yySymbolPrint("-> $$ =", SymbolKind.get(yyr1_[yyn]), yyval]b4_locations_if([, yyloc])[);]])[
515 yystack.pop(yylen);
516 yylen = 0;
517 /* Shift the result of the reduction. */
518 int yystate = yyLRGotoState(yystack.stateAt(0), yyr1_[yyn]);
519 yystack.push(yystate, yyval]b4_locations_if([, yyloc])[);
520 return YYNEWSTATE;
523 ]b4_parse_trace_if([[
524 /*--------------------------------.
525 | Print this symbol on YYOUTPUT. |
526 `--------------------------------*/
528 private void yySymbolPrint(String s, SymbolKind yykind,
529 ]b4_yystype[ yyvalue]b4_locations_if([, ]b4_location_type[ yylocation])[) {
530 if (0 < yydebug) {
531 yycdebug(s
532 + (yykind.getCode() < YYNTOKENS_ ? " token " : " nterm ")
533 + yykind.getName() + " ("]b4_locations_if([
534 + yylocation + ": "])[
535 + (yyvalue == null ? "(null)" : yyvalue.toString()) + ")");
537 }]])[
539 ]b4_push_if([],[[
541 * Parse input from the scanner that was specified at object construction
542 * time. Return whether the end of the input was reached successfully.
544 * @@return <tt>true</tt> if the parsing succeeds. Note that this does not
545 * imply that there were no syntax errors.
547 public boolean parse()]b4_maybe_throws([b4_list2([b4_lex_throws], [b4_throws])])[]])[
548 ]b4_push_if([
550 * Push Parse input from external lexer
552 * @@param yylextoken current token
553 * @@param yylexval current lval]b4_locations_if([[
554 * @@param yylexloc current position]])[
556 * @@return <tt>YYACCEPT, YYABORT, YYPUSH_MORE</tt>
558 public int push_parse(int yylextoken, b4_yystype yylexval[]b4_locations_if([, b4_location_type yylexloc]))b4_maybe_throws([b4_list2([b4_lex_throws], [b4_throws])])])[
559 {]b4_locations_if([[
560 /* @@$. */
561 ]b4_location_type[ yyloc;]])[
562 ]b4_push_if([],[[
563 ]b4_define_state[
564 ]b4_lac_if([[
565 // Discard the LAC context in case there still is one left from a
566 // previous invocation.
567 yylacDiscard("init");]])[
568 ]b4_parse_trace_if([[
569 yycdebug ("Starting parse");]])[
570 yyerrstatus_ = 0;
571 yynerrs = 0;
573 /* Initialize the stack. */
574 yystack.push (yystate, yylval]b4_locations_if([, yylloc])[);
575 ]m4_ifdef([b4_initial_action], [
576 b4_dollar_pushdef([yylval], [], [], [yylloc])dnl
577 b4_user_initial_action
578 b4_dollar_popdef[]dnl
580 ]])[
581 ]b4_push_if([[
582 if (!this.push_parse_initialized)
584 push_parse_initialize ();
585 ]m4_ifdef([b4_initial_action], [
586 b4_dollar_pushdef([yylval], [], [], [yylloc])dnl
587 b4_user_initial_action
588 b4_dollar_popdef[]dnl
589 ])[]b4_parse_trace_if([[
590 yycdebug ("Starting parse");]])[
591 yyerrstatus_ = 0;
592 } else
593 label = YYGETTOKEN;
595 boolean push_token_consumed = true;
596 ]])[
597 for (;;)
598 switch (label)
600 /* New state. Unlike in the C/C++ skeletons, the state is already
601 pushed when we come here. */
602 case YYNEWSTATE:]b4_parse_trace_if([[
603 yycdebug ("Entering state " + yystate);
604 if (0 < yydebug)
605 yystack.print (yyDebugStream);]])[
607 /* Accept? */
608 if (yystate == YYFINAL_)
609 ]b4_push_if([{label = YYACCEPT; break;}],
610 [return true;])[
612 /* Take a decision. First try without lookahead. */
613 yyn = yypact_[yystate];
614 if (yyPactValueIsDefault (yyn))
616 label = YYDEFAULT;
617 break;
619 ]b4_push_if([ /* Fall Through */
621 case YYGETTOKEN:])[
622 /* Read a lookahead token. */
623 if (yychar == YYEMPTY_)
625 ]b4_push_if([[
626 if (!push_token_consumed)
627 return YYPUSH_MORE;]b4_parse_trace_if([[
628 yycdebug ("Reading a token");]])[
629 yychar = yylextoken;
630 yylval = yylexval;]b4_locations_if([
631 yylloc = yylexloc;])[
632 push_token_consumed = false;]], [b4_parse_trace_if([[
633 yycdebug ("Reading a token");]])[
634 yychar = yylexer.yylex ();
635 yylval = yylexer.getLVal();]b4_locations_if([[
636 yylloc = new ]b4_location_type[(yylexer.getStartPos(),
637 yylexer.getEndPos());]])[
638 ]])[
641 /* Convert token to internal form. */
642 yytoken = yytranslate_ (yychar);]b4_parse_trace_if([[
643 yySymbolPrint("Next token is", yytoken,
644 yylval]b4_locations_if([, yylloc])[);]])[
646 if (yytoken == ]b4_symbol(error, kind)[)
648 // The scanner already issued an error message, process directly
649 // to error recovery. But do not keep the error token as
650 // lookahead, it is too special and may lead us to an endless
651 // loop in error recovery. */
652 yychar = Lexer.]b4_symbol(undef, id)[;
653 yytoken = ]b4_symbol(undef, kind)[;]b4_locations_if([[
654 yyerrloc = yylloc;]])[
655 label = YYERRLAB1;
657 else
659 /* If the proper action on seeing token YYTOKEN is to reduce or to
660 detect an error, take that action. */
661 yyn += yytoken.getCode();
662 if (yyn < 0 || YYLAST_ < yyn || yycheck_[yyn] != yytoken.getCode()) {]b4_lac_if([[
663 if (!yylacEstablish(yystack, yytoken)) {
664 label = YYERRLAB;
665 } else]])[
666 label = YYDEFAULT;
669 /* <= 0 means reduce or error. */
670 else if ((yyn = yytable_[yyn]) <= 0)
672 if (yyTableValueIsError(yyn)) {
673 label = YYERRLAB;
674 }]b4_lac_if([[ else if (!yylacEstablish(yystack, yytoken)) {
675 label = YYERRLAB;
676 }]])[ else {
677 yyn = -yyn;
678 label = YYREDUCE;
682 else
684 /* Shift the lookahead token. */]b4_parse_trace_if([[
685 yySymbolPrint("Shifting", yytoken,
686 yylval]b4_locations_if([, yylloc])[);
687 ]])[
688 /* Discard the token being shifted. */
689 yychar = YYEMPTY_;
691 /* Count tokens shifted since error; after three, turn off error
692 status. */
693 if (yyerrstatus_ > 0)
694 --yyerrstatus_;
696 yystate = yyn;
697 yystack.push(yystate, yylval]b4_locations_if([, yylloc])[);]b4_lac_if([[
698 yylacDiscard("shift");]])[
699 label = YYNEWSTATE;
702 break;
704 /*-----------------------------------------------------------.
705 | yydefault -- do the default action for the current state. |
706 `-----------------------------------------------------------*/
707 case YYDEFAULT:
708 yyn = yydefact_[yystate];
709 if (yyn == 0)
710 label = YYERRLAB;
711 else
712 label = YYREDUCE;
713 break;
715 /*-----------------------------.
716 | yyreduce -- Do a reduction. |
717 `-----------------------------*/
718 case YYREDUCE:
719 yylen = yyr2_[yyn];
720 label = yyaction(yyn, yystack, yylen);
721 yystate = yystack.stateAt(0);
722 break;
724 /*------------------------------------.
725 | yyerrlab -- here on detecting error |
726 `------------------------------------*/
727 case YYERRLAB:
728 /* If not already recovering from an error, report this error. */
729 if (yyerrstatus_ == 0)
731 ++yynerrs;
732 if (yychar == YYEMPTY_)
733 yytoken = null;
734 yyreportSyntaxError(new Context(this, yystack, yytoken]b4_locations_if([[, yylloc]])[));
736 ]b4_locations_if([[
737 yyerrloc = yylloc;]])[
738 if (yyerrstatus_ == 3)
740 /* If just tried and failed to reuse lookahead token after an
741 error, discard it. */
743 if (yychar <= Lexer.]b4_symbol(eof, id)[)
745 /* Return failure if at end of input. */
746 if (yychar == Lexer.]b4_symbol(eof, id)[)
747 ]b4_push_if([{label = YYABORT; break;}], [return false;])[
749 else
750 yychar = YYEMPTY_;
753 /* Else will try to reuse lookahead token after shifting the error
754 token. */
755 label = YYERRLAB1;
756 break;
758 /*-------------------------------------------------.
759 | errorlab -- error raised explicitly by YYERROR. |
760 `-------------------------------------------------*/
761 case YYERROR:]b4_locations_if([[
762 yyerrloc = yystack.locationAt (yylen - 1);]])[
763 /* Do not reclaim the symbols of the rule which action triggered
764 this YYERROR. */
765 yystack.pop (yylen);
766 yylen = 0;
767 yystate = yystack.stateAt(0);
768 label = YYERRLAB1;
769 break;
771 /*-------------------------------------------------------------.
772 | yyerrlab1 -- common code for both syntax error and YYERROR. |
773 `-------------------------------------------------------------*/
774 case YYERRLAB1:
775 yyerrstatus_ = 3; /* Each real token shifted decrements this. */
777 // Pop stack until we find a state that shifts the error token.
778 for (;;)
780 yyn = yypact_[yystate];
781 if (!yyPactValueIsDefault (yyn))
783 yyn += ]b4_symbol(error, kind)[.getCode();
784 if (0 <= yyn && yyn <= YYLAST_
785 && yycheck_[yyn] == ]b4_symbol(error, kind)[.getCode())
787 yyn = yytable_[yyn];
788 if (0 < yyn)
789 break;
793 /* Pop the current state because it cannot handle the
794 * error token. */
795 if (yystack.height == 0)
796 ]b4_push_if([{label = YYABORT; break;}],[return false;])[
798 ]b4_locations_if([[
799 yyerrloc = yystack.locationAt (0);]])[
800 yystack.pop ();
801 yystate = yystack.stateAt(0);]b4_parse_trace_if([[
802 if (0 < yydebug)
803 yystack.print (yyDebugStream);]])[
806 if (label == YYABORT)
807 /* Leave the switch. */
808 break;
810 ]b4_locations_if([[
811 /* Muck with the stack to setup for yylloc. */
812 yystack.push (0, null, yylloc);
813 yystack.push (0, null, yyerrloc);
814 yyloc = yylloc (yystack, 2);
815 yystack.pop (2);]])[
817 /* Shift the error token. */]b4_lac_if([[
818 yylacDiscard("error recovery");]])[]b4_parse_trace_if([[
819 yySymbolPrint("Shifting", SymbolKind.get(yystos_[yyn]),
820 yylval]b4_locations_if([, yyloc])[);]])[
822 yystate = yyn;
823 yystack.push (yyn, yylval]b4_locations_if([, yyloc])[);
824 label = YYNEWSTATE;
825 break;
827 /* Accept. */
828 case YYACCEPT:
829 ]b4_push_if([this.push_parse_initialized = false; return YYACCEPT;],
830 [return true;])[
832 /* Abort. */
833 case YYABORT:
834 ]b4_push_if([this.push_parse_initialized = false; return YYABORT;],
835 [return false;])[
838 ]b4_push_if([[
839 boolean push_parse_initialized = false;
842 * (Re-)Initialize the state of the push parser.
844 public void push_parse_initialize ()
846 /* Lookahead and lookahead in internal form. */
847 this.yychar = YYEMPTY_;
848 this.yytoken = null;
850 /* State. */
851 this.yyn = 0;
852 this.yylen = 0;
853 this.yystate = 0;
854 this.yystack = new YYStack();]b4_lac_if([[
855 this.yylacStack = new ArrayList<Integer>();
856 this.yylacEstablished = false;]])[
857 this.label = YYNEWSTATE;
859 /* Error handling. */
860 this.yynerrs = 0;]b4_locations_if([[
861 /* The location where the error started. */
862 this.yyerrloc = null;
863 this.yylloc = new ]b4_location_type[ (null, null);]])[
865 /* Semantic value of the lookahead. */
866 this.yylval = null;
868 yystack.push (this.yystate, this.yylval]b4_locations_if([, this.yylloc])[);
870 this.push_parse_initialized = true;
873 ]b4_locations_if([[
875 * Push parse given input from an external lexer.
877 * @@param yylextoken current token
878 * @@param yylexval current lval
879 * @@param yyylexpos current position
881 * @@return <tt>YYACCEPT, YYABORT, YYPUSH_MORE</tt>
883 public int push_parse(int yylextoken, ]b4_yystype[ yylexval, ]b4_position_type[ yylexpos)]b4_maybe_throws([b4_list2([b4_lex_throws], [b4_throws])])[ {
884 return push_parse(yylextoken, yylexval, new ]b4_location_type[(yylexpos));
886 ]])])[
888 ]b4_both_if([[
890 * Parse input from the scanner that was specified at object construction
891 * time. Return whether the end of the input was reached successfully.
892 * This version of parse() is defined only when api.push-push=both.
894 * @@return <tt>true</tt> if the parsing succeeds. Note that this does not
895 * imply that there were no syntax errors.
897 public boolean parse()]b4_maybe_throws([b4_list2([b4_lex_throws], [b4_throws])])[ {
898 if (yylexer == null)
899 throw new NullPointerException("Null Lexer");
900 int status;
901 do {
902 int token = yylexer.yylex();
903 ]b4_yystype[ lval = yylexer.getLVal();]b4_locations_if([[
904 ]b4_location_type[ yyloc = new ]b4_location_type[(yylexer.getStartPos(), yylexer.getEndPos());
905 status = push_parse(token, lval, yyloc);]], [[
906 status = push_parse(token, lval);]])[
907 } while (status == YYPUSH_MORE);
908 return status == YYACCEPT;
910 ]])[
913 * Information needed to get the list of expected tokens and to forge
914 * a syntax error diagnostic.
916 public static final class Context {
917 Context(]b4_parser_class[ parser, YYStack stack, SymbolKind token]b4_locations_if([[, ]b4_location_type[ loc]])[) {
918 yyparser = parser;
919 yystack = stack;
920 yytoken = token;]b4_locations_if([[
921 yylocation = loc;]])[
924 private ]b4_parser_class[ yyparser;
925 private YYStack yystack;
929 * The symbol kind of the lookahead token.
931 public final SymbolKind getToken() {
932 return yytoken;
935 private SymbolKind yytoken;]b4_locations_if([[
938 * The location of the lookahead.
940 public final ]b4_location_type[ getLocation() {
941 return yylocation;
944 private ]b4_location_type[ yylocation;]])[
945 static final int NTOKENS = ]b4_parser_class[.YYNTOKENS_;
948 * Put in YYARG at most YYARGN of the expected tokens given the
949 * current YYCTX, and return the number of tokens stored in YYARG. If
950 * YYARG is null, return the number of expected tokens (guaranteed to
951 * be less than YYNTOKENS).
953 int getExpectedTokens(SymbolKind yyarg[], int yyargn) {
954 return getExpectedTokens (yyarg, 0, yyargn);
957 int getExpectedTokens(SymbolKind yyarg[], int yyoffset, int yyargn) {
958 int yycount = yyoffset;]b4_lac_if([b4_parse_trace_if([[
959 // Execute LAC once. We don't care if it is successful, we
960 // only do it for the sake of debugging output.
961 if (!yyparser.yylacEstablished)
962 yyparser.yylacCheck(yystack, yytoken);
963 ]])[
964 for (int yyx = 0; yyx < YYNTOKENS_; ++yyx)
966 SymbolKind yysym = SymbolKind.get(yyx);
967 if (yysym != ]b4_symbol(error, kind)[
968 && yysym != ]b4_symbol(undef, kind)[
969 && yyparser.yylacCheck(yystack, yysym))
971 if (yyarg == null)
972 yycount += 1;
973 else if (yycount == yyargn)
974 return 0;
975 else
976 yyarg[yycount++] = yysym;
978 }]], [[
979 int yyn = yypact_[this.yystack.stateAt(0)];
980 if (!yyPactValueIsDefault(yyn))
982 /* Start YYX at -YYN if negative to avoid negative
983 indexes in YYCHECK. In other words, skip the first
984 -YYN actions for this state because they are default
985 actions. */
986 int yyxbegin = yyn < 0 ? -yyn : 0;
987 /* Stay within bounds of both yycheck and yytname. */
988 int yychecklim = YYLAST_ - yyn + 1;
989 int yyxend = yychecklim < NTOKENS ? yychecklim : NTOKENS;
990 for (int yyx = yyxbegin; yyx < yyxend; ++yyx)
991 if (yycheck_[yyx + yyn] == yyx && yyx != ]b4_symbol(error, kind)[.getCode()
992 && !yyTableValueIsError(yytable_[yyx + yyn]))
994 if (yyarg == null)
995 yycount += 1;
996 else if (yycount == yyargn)
997 return 0; // FIXME: this is incorrect.
998 else
999 yyarg[yycount++] = SymbolKind.get(yyx);
1001 }]])[
1002 if (yyarg != null && yycount == yyoffset && yyoffset < yyargn)
1003 yyarg[yycount] = null;
1004 return yycount - yyoffset;
1008 ]b4_lac_if([[
1009 /** Check the lookahead yytoken.
1010 * \returns true iff the token will be eventually shifted.
1012 boolean yylacCheck(YYStack yystack, SymbolKind yytoken)
1014 // Logically, the yylacStack's lifetime is confined to this function.
1015 // Clear it, to get rid of potential left-overs from previous call.
1016 yylacStack.clear();
1017 // Reduce until we encounter a shift and thereby accept the token.
1018 yycdebugNnl("LAC: checking lookahead " + yytoken.getName() + ":");
1019 int lacTop = 0;
1020 while (true)
1022 int topState = (yylacStack.isEmpty()
1023 ? yystack.stateAt(lacTop)
1024 : yylacStack.get(yylacStack.size() - 1));
1025 int yyrule = yypact_[topState];
1026 if (yyPactValueIsDefault(yyrule)
1027 || (yyrule += yytoken.getCode()) < 0 || YYLAST_ < yyrule
1028 || yycheck_[yyrule] != yytoken.getCode())
1030 // Use the default action.
1031 yyrule = yydefact_[+topState];
1032 if (yyrule == 0) {
1033 yycdebug(" Err");
1034 return false;
1037 else
1039 // Use the action from yytable.
1040 yyrule = yytable_[yyrule];
1041 if (yyTableValueIsError(yyrule)) {
1042 yycdebug(" Err");
1043 return false;
1045 if (0 < yyrule) {
1046 yycdebug(" S" + yyrule);
1047 return true;
1049 yyrule = -yyrule;
1051 // By now we know we have to simulate a reduce.
1052 yycdebugNnl(" R" + (yyrule - 1));
1053 // Pop the corresponding number of values from the stack.
1055 int yylen = yyr2_[yyrule];
1056 // First pop from the LAC stack as many tokens as possible.
1057 int lacSize = yylacStack.size();
1058 if (yylen < lacSize) {
1059 // yylacStack.setSize(lacSize - yylen);
1060 for (/* Nothing */; 0 < yylen; yylen -= 1) {
1061 yylacStack.remove(yylacStack.size() - 1);
1063 yylen = 0;
1064 } else if (lacSize != 0) {
1065 yylacStack.clear();
1066 yylen -= lacSize;
1068 // Only afterwards look at the main stack.
1069 // We simulate popping elements by incrementing lacTop.
1070 lacTop += yylen;
1072 // Keep topState in sync with the updated stack.
1073 topState = (yylacStack.isEmpty()
1074 ? yystack.stateAt(lacTop)
1075 : yylacStack.get(yylacStack.size() - 1));
1076 // Push the resulting state of the reduction.
1077 int state = yyLRGotoState(topState, yyr1_[yyrule]);
1078 yycdebugNnl(" G" + state);
1079 yylacStack.add(state);
1083 /** Establish the initial context if no initial context currently exists.
1084 * \returns true iff the token will be eventually shifted.
1086 boolean yylacEstablish(YYStack yystack, SymbolKind yytoken) {
1087 /* Establish the initial context for the current lookahead if no initial
1088 context is currently established.
1090 We define a context as a snapshot of the parser stacks. We define
1091 the initial context for a lookahead as the context in which the
1092 parser initially examines that lookahead in order to select a
1093 syntactic action. Thus, if the lookahead eventually proves
1094 syntactically unacceptable (possibly in a later context reached via a
1095 series of reductions), the initial context can be used to determine
1096 the exact set of tokens that would be syntactically acceptable in the
1097 lookahead's place. Moreover, it is the context after which any
1098 further semantic actions would be erroneous because they would be
1099 determined by a syntactically unacceptable token.
1101 yylacEstablish should be invoked when a reduction is about to be
1102 performed in an inconsistent state (which, for the purposes of LAC,
1103 includes consistent states that don't know they're consistent because
1104 their default reductions have been disabled).
1106 For parse.lac=full, the implementation of yylacEstablish is as
1107 follows. If no initial context is currently established for the
1108 current lookahead, then check if that lookahead can eventually be
1109 shifted if syntactic actions continue from the current context. */
1110 if (yylacEstablished) {
1111 return true;
1112 } else {
1113 yycdebug("LAC: initial context established for " + yytoken.getName());
1114 yylacEstablished = true;
1115 return yylacCheck(yystack, yytoken);
1119 /** Discard any previous initial lookahead context because of event.
1120 * \param event the event which caused the lookahead to be discarded.
1121 * Only used for debbuging output. */
1122 void yylacDiscard(String event) {
1123 /* Discard any previous initial lookahead context because of Event,
1124 which may be a lookahead change or an invalidation of the currently
1125 established initial context for the current lookahead.
1127 The most common example of a lookahead change is a shift. An example
1128 of both cases is syntax error recovery. That is, a syntax error
1129 occurs when the lookahead is syntactically erroneous for the
1130 currently established initial context, so error recovery manipulates
1131 the parser stacks to try to find a new initial context in which the
1132 current lookahead is syntactically acceptable. If it fails to find
1133 such a context, it discards the lookahead. */
1134 if (yylacEstablished) {
1135 yycdebug("LAC: initial context discarded due to " + event);
1136 yylacEstablished = false;
1140 /** The stack for LAC.
1141 * Logically, the yylacStack's lifetime is confined to the function
1142 * yylacCheck. We just store it as a member of this class to hold
1143 * on to the memory and to avoid frequent reallocations.
1145 ArrayList<Integer> yylacStack;
1146 /** Whether an initial LAC context was established. */
1147 boolean yylacEstablished;
1148 ]])[
1150 ]b4_parse_error_bmatch(
1151 [detailed\|verbose], [[
1152 private int yysyntaxErrorArguments(Context yyctx, SymbolKind[] yyarg, int yyargn) {
1153 /* There are many possibilities here to consider:
1154 - If this state is a consistent state with a default action,
1155 then the only way this function was invoked is if the
1156 default action is an error action. In that case, don't
1157 check for expected tokens because there are none.
1158 - The only way there can be no lookahead present (in tok) is
1159 if this state is a consistent state with a default action.
1160 Thus, detecting the absence of a lookahead is sufficient to
1161 determine that there is no unexpected or expected token to
1162 report. In that case, just report a simple "syntax error".
1163 - Don't assume there isn't a lookahead just because this
1164 state is a consistent state with a default action. There
1165 might have been a previous inconsistent state, consistent
1166 state with a non-default action, or user semantic action
1167 that manipulated yychar. (However, yychar is currently out
1168 of scope during semantic actions.)
1169 - Of course, the expected token list depends on states to
1170 have correct lookahead information, and it depends on the
1171 parser not to perform extra reductions after fetching a
1172 lookahead from the scanner and before detecting a syntax
1173 error. Thus, state merging (from LALR or IELR) and default
1174 reductions corrupt the expected token list. However, the
1175 list is correct for canonical LR with one exception: it
1176 will still contain any token that will not be accepted due
1177 to an error action in a later state.
1179 int yycount = 0;
1180 if (yyctx.getToken() != null)
1182 if (yyarg != null)
1183 yyarg[yycount] = yyctx.getToken();
1184 yycount += 1;
1185 yycount += yyctx.getExpectedTokens(yyarg, 1, yyargn);
1187 return yycount;
1189 ]])[
1192 * Build and emit a "syntax error" message in a user-defined way.
1194 * @@param ctx The context of the error.
1196 private void yyreportSyntaxError(Context yyctx) {]b4_parse_error_bmatch(
1197 [custom], [[
1198 yylexer.reportSyntaxError(yyctx);]],
1199 [detailed\|verbose], [[
1200 if (yyErrorVerbose) {
1201 final int argmax = 5;
1202 SymbolKind[] yyarg = new SymbolKind[argmax];
1203 int yycount = yysyntaxErrorArguments(yyctx, yyarg, argmax);
1204 String[] yystr = new String[yycount];
1205 for (int yyi = 0; yyi < yycount; ++yyi) {
1206 yystr[yyi] = yyarg[yyi].getName();
1208 String yyformat;
1209 switch (yycount) {
1210 default:
1211 case 0: yyformat = ]b4_trans(["syntax error"])[; break;
1212 case 1: yyformat = ]b4_trans(["syntax error, unexpected {0}"])[; break;
1213 case 2: yyformat = ]b4_trans(["syntax error, unexpected {0}, expecting {1}"])[; break;
1214 case 3: yyformat = ]b4_trans(["syntax error, unexpected {0}, expecting {1} or {2}"])[; break;
1215 case 4: yyformat = ]b4_trans(["syntax error, unexpected {0}, expecting {1} or {2} or {3}"])[; break;
1216 case 5: yyformat = ]b4_trans(["syntax error, unexpected {0}, expecting {1} or {2} or {3} or {4}"])[; break;
1218 yyerror(]b4_locations_if([[yyctx.yylocation, ]])[new MessageFormat(yyformat).format(yystr));
1219 } else {
1220 yyerror(]b4_locations_if([[yyctx.yylocation, ]])[]b4_trans(["syntax error"])[);
1221 }]],
1222 [simple], [[
1223 yyerror(]b4_locations_if([[yyctx.yylocation, ]])[]b4_trans(["syntax error"])[);]])[
1227 * Whether the given <code>yypact_</code> value indicates a defaulted state.
1228 * @@param yyvalue the value to check
1230 private static boolean yyPactValueIsDefault(int yyvalue) {
1231 return yyvalue == yypact_ninf_;
1235 * Whether the given <code>yytable_</code>
1236 * value indicates a syntax error.
1237 * @@param yyvalue the value to check
1239 private static boolean yyTableValueIsError(int yyvalue) {
1240 return yyvalue == yytable_ninf_;
1243 private static final ]b4_int_type_for([b4_pact])[ yypact_ninf_ = ]b4_pact_ninf[;
1244 private static final ]b4_int_type_for([b4_table])[ yytable_ninf_ = ]b4_table_ninf[;
1246 ]b4_parser_tables_define[
1248 ]b4_parse_trace_if([[
1249 ]b4_integral_parser_table_define([rline], [b4_rline],
1250 [[YYRLINE[YYN] -- Source line where rule number YYN was defined.]])[
1253 // Report on the debug stream that the rule yyrule is going to be reduced.
1254 private void yyReducePrint (int yyrule, YYStack yystack)
1256 if (yydebug == 0)
1257 return;
1259 int yylno = yyrline_[yyrule];
1260 int yynrhs = yyr2_[yyrule];
1261 /* Print the symbols being reduced, and their result. */
1262 yycdebug ("Reducing stack by rule " + (yyrule - 1)
1263 + " (line " + yylno + "):");
1265 /* The symbols being reduced. */
1266 for (int yyi = 0; yyi < yynrhs; yyi++)
1267 yySymbolPrint(" $" + (yyi + 1) + " =",
1268 SymbolKind.get(yystos_[yystack.stateAt(yynrhs - (yyi + 1))]),
1269 ]b4_rhs_data(yynrhs, yyi + 1)b4_locations_if([,
1270 b4_rhs_location(yynrhs, yyi + 1)])[);
1271 }]])[
1273 /* YYTRANSLATE_(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM
1274 as returned by yylex, with out-of-bounds checking. */
1275 private static final SymbolKind yytranslate_(int t)
1276 ]b4_api_token_raw_if(dnl
1277 [[ {
1278 return SymbolKind.get(t);
1281 [[ {
1282 // Last valid token kind.
1283 int code_max = ]b4_code_max[;
1284 if (t <= 0)
1285 return ]b4_symbol(eof, kind)[;
1286 else if (t <= code_max)
1287 return SymbolKind.get(yytranslate_table_[t]);
1288 else
1289 return ]b4_symbol(undef, kind)[;
1291 ]b4_integral_parser_table_define([translate_table], [b4_translate])[
1292 ]])[
1294 private static final int YYLAST_ = ]b4_last[;
1295 private static final int YYEMPTY_ = -2;
1296 private static final int YYFINAL_ = ]b4_final_state_number[;
1297 private static final int YYNTOKENS_ = ]b4_tokens_number[;
1299 ]b4_percent_code_get[
1301 ]b4_percent_code_get([[epilogue]])[]dnl
1302 b4_epilogue[]dnl
1303 b4_output_end