1 # Java skeleton
for Bison
-*- autoconf
-*-
3 #
Copyright (C
) 2007, 2008, 2009 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
<http
://www.gnu.org/licenses/>.
18 m4_include(b4_pkgdatadir
/[java
.m4
])
20 b4_defines_if([b4_fatal([%s
: %%defines does not make sense in Java
],
23 # We don
't depend on %debug in Java, but pacify warnings about non-used flags.
24 b4_parse_trace_if([0], [0])
26 m4_define([b4_symbol_no_destructor_assert],
27 [b4_symbol_if([$1], [has_destructor],
28 [b4_fatal([%s: %s: %%destructor does not make sense in Java],
30 [b4_symbol_action_location([$1], [destructor])])])])
31 b4_symbol_foreach([b4_symbol_no_destructor_assert])
34 @output(b4_parser_file_name@)@
35 b4_copyright([Skeleton implementation for Bison LALR(1) parsers in Java],
38 b4_percent_define_ifdef([package], [package b4_percent_define_get([package]);
39 ])[/* First part of user declarations. */
42 b4_percent_code_get([[imports]])
44 * A Bison parser, automatically generated from <tt>]m4_bpatsubst(b4_file_name, [^"\(.*\)"$], [\1])[</tt>.
46 * @@author LALR (1) parser skeleton written by Paolo Bonzini.
48 ]b4_percent_define_get3([annotations], [], [ ])dnl
49 b4_public_if([public ])dnl
50 b4_abstract_if([abstract ])dnl
51 b4_final_if([final ])dnl
52 b4_strictfp_if([strictfp ])dnl
53 [class ]b4_parser_class_name[]dnl
54 b4_percent_define_get3([extends], [ extends ])dnl
55 b4_percent_define_get3([implements], [ implements ])[
58 ]b4_error_verbose_if([[
59 /** True if verbose error messages are enabled. */
60 private boolean yyErrorVerbose = true;
63 * Return whether verbose error messages are enabled.
65 public final boolean getErrorVerbose() { return yyErrorVerbose; }
68 * Set the verbosity of error messages.
69 * @@param verbose True to request verbose error messages.
71 public final void setErrorVerbose(boolean verbose)
72 { yyErrorVerbose = verbose; }
77 * A class defining a pair of positions. Positions, defined by the
78 * <code>]b4_position_type[</code> class, denote a point in the input.
79 * Locations represent a part of the input through the beginning
80 * and ending positions. */
81 public class ]b4_location_type[ {
82 /** The first, inclusive, position in the range. */
83 public ]b4_position_type[ begin;
85 /** The first position beyond the range. */
86 public ]b4_position_type[ end;
89 * Create a <code>]b4_location_type[</code> denoting an empty range located at
91 * @@param loc The position at which the range is anchored. */
92 public ]b4_location_type[ (]b4_position_type[ loc) {
93 this.begin = this.end = loc;
97 * Create a <code>]b4_location_type[</code> from the endpoints of the range.
98 * @@param begin The first position included in the range.
99 * @@param end The first position beyond the range. */
100 public ]b4_location_type[ (]b4_position_type[ begin, ]b4_position_type[ end) {
106 * Print a representation of the location. For this to be correct,
107 * <code>]b4_position_type[</code> should override the <code>equals</code>
109 public String toString () {
110 if (begin.equals (end))
111 return begin.toString ();
113 return begin.toString () + "-" + end.toString ();
120 private ]b4_location_type[ yylloc (YYStack rhs, int n)
123 return new ]b4_location_type[ (rhs.locationAt (1).begin, rhs.locationAt (n).end);
125 return new ]b4_location_type[ (rhs.locationAt (0).end);
129 * Communication interface between the scanner and the Bison-generated
130 * parser <tt>]b4_parser_class_name[</tt>.
132 public interface Lexer {
133 /** Token returned by the scanner to signal the end of its input. */
134 public static final int EOF = 0;
136 ]b4_token_enums(b4_tokens)[
138 ]b4_locations_if([[/**
139 * Method to retrieve the beginning position of the last scanned token.
140 * @@return the position at which the last scanned token starts. */
141 ]b4_position_type[ getStartPos ();
144 * Method to retrieve the ending position of the last scanned token.
145 * @@return the first position beyond the last scanned token. */
146 ]b4_position_type[ getEndPos ();]])[
149 * Method to retrieve the semantic value of the last scanned token.
150 * @@return the semantic value of the last scanned token. */
151 ]b4_yystype[ getLVal ();
154 * Entry point for the scanner. Returns the token identifier corresponding
155 * to the next token and prepares to return the semantic value
156 * ]b4_locations_if([and beginning/ending positions ])[of the token.
157 * @@return the token identifier corresponding to the next token. */
158 int yylex () ]b4_maybe_throws([b4_lex_throws])[;
161 * Entry point for error reporting. Emits an error
162 * ]b4_locations_if([referring to the given location ])[in a user-defined way.
164 * ]b4_locations_if([[@@param loc The location of the element to which the
165 * error message is related]])[
166 * @@param msg The string for the error message. */
167 void yyerror (]b4_locations_if([b4_location_type[ loc, ]])[String msg);]
170 b4_lexer_if([[private class YYLexer implements Lexer {
171 ]b4_percent_code_get([[lexer]])[
174 ]])[/** The object doing lexical analysis for us. */
175 private Lexer yylexer;
181 * Instantiates the Bison-generated parser.
183 public ]b4_parser_class_name (b4_parse_param_decl([b4_lex_param_decl])[) ]b4_maybe_throws([b4_init_throws])[
185 ]b4_percent_code_get([[init]])[
186 this.yylexer = new YYLexer(]b4_lex_param_call[);
187 ]b4_parse_param_cons[
192 * Instantiates the Bison-generated parser.
193 * @@param yylexer The scanner that will supply tokens to the parser.
195 b4_lexer_if([[protected]], [[public]]) b4_parser_class_name[ (]b4_parse_param_decl([[Lexer yylexer]])[) ]b4_maybe_throws([b4_init_throws])[
197 ]b4_percent_code_get([[init]])[
198 this.yylexer = yylexer;
199 ]b4_parse_param_cons[
202 private java.io.PrintStream yyDebugStream = System.err;
205 * Return the <tt>PrintStream</tt> on which the debugging output is
208 public final java.io.PrintStream getDebugStream () { return yyDebugStream; }
211 * Set the <tt>PrintStream</tt> on which the debug output is printed.
212 * @@param s The stream that is used for debugging output.
214 public final void setDebugStream(java.io.PrintStream s) { yyDebugStream = s; }
216 private int yydebug = 0;
219 * Answer the verbosity of the debugging output; 0 means that all kinds of
220 * output from the parser are suppressed.
222 public final int getDebugLevel() { return yydebug; }
225 * Set the verbosity of the debugging output; 0 means that all kinds of
226 * output from the parser are suppressed.
227 * @@param level The verbosity level for debugging output.
229 public final void setDebugLevel(int level) { yydebug = level; }
232 * Print an error message via the lexer.
233 *]b4_locations_if([[ Use a <code>null</code> location.]])[
234 * @@param msg The error message.
236 public final void yyerror (String msg)
238 yylexer.yyerror (]b4_locations_if([[(]b4_location_type[)null, ]])[msg);
242 * Print an error message via the lexer.
243 * @@param loc The location associated with the message.
244 * @@param msg The error message.
246 public final void yyerror (]b4_location_type[ loc, String msg)
248 yylexer.yyerror (loc, msg);
252 * Print an error message via the lexer.
253 * @@param pos The position associated with the message.
254 * @@param msg The error message.
256 public final void yyerror (]b4_position_type[ pos, String msg)
258 yylexer.yyerror (new ]b4_location_type[ (pos), msg);
261 [protected final void yycdebug (String s) {
263 yyDebugStream.println (s);
266 private final class YYStack {
267 private int[] stateStack = new int[16];
268 ]b4_locations_if([[private ]b4_location_type[[] locStack = new ]b4_location_type[[16];]])[
269 private ]b4_yystype[[] valueStack = new ]b4_yystype[[16];
271 public int size = 16;
272 public int height = -1;
274 public final void push (int state, ]b4_yystype[ value]dnl
275 b4_locations_if([, ]b4_location_type[ loc])[) {
279 int[] newStateStack = new int[size * 2];
280 System.arraycopy (stateStack, 0, newStateStack, 0, height);
281 stateStack = newStateStack;
283 ]b4_location_type[[] newLocStack = new ]b4_location_type[[size * 2];
284 System.arraycopy (locStack, 0, newLocStack, 0, height);
285 locStack = newLocStack;]])
287 b4_yystype[[] newValueStack = new ]b4_yystype[[size * 2];
288 System.arraycopy (valueStack, 0, newValueStack, 0, height);
289 valueStack = newValueStack;
294 stateStack[height] = state;
295 ]b4_locations_if([[locStack[height] = loc;]])[
296 valueStack[height] = value;
299 public final void pop () {
303 public final void pop (int num) {
304 // Avoid memory leaks... garbage collection is a white lie!
306 java.util.Arrays.fill (valueStack, height - num + 1, height, null);
307 ]b4_locations_if([[java.util.Arrays.fill (locStack, height - num + 1, height, null);]])[
312 public final int stateAt (int i) {
313 return stateStack[height - i];
316 ]b4_locations_if([[public final ]b4_location_type[ locationAt (int i) {
317 return locStack[height - i];
320 ]])[public final ]b4_yystype[ valueAt (int i) {
321 return valueStack[height - i];
324 // Print the state stack on the debug stream.
325 public void print (java.io.PrintStream out)
327 out.print ("Stack now");
329 for (int i = 0; i < height; i++)
332 out.print (stateStack[i]);
339 * Returned by a Bison action in order to stop the parsing process and
340 * return success (<tt>true</tt>). */
341 public static final int YYACCEPT = 0;
344 * Returned by a Bison action in order to stop the parsing process and
345 * return failure (<tt>false</tt>). */
346 public static final int YYABORT = 1;
349 * Returned by a Bison action in order to start error recovery without
350 * printing an error message. */
351 public static final int YYERROR = 2;
354 * Returned by a Bison action in order to print an error message and start
355 * error recovery. Formally deprecated in Bison 2.4.2's NEWS entry
, where
356 * a plan to phase it out is discussed
. */
357 public static final int YYFAIL
= 3;
359 private static final int YYNEWSTATE
= 4;
360 private static final int YYDEFAULT
= 5;
361 private static final int YYREDUCE
= 6;
362 private static final int YYERRLAB1
= 7;
363 private static final int YYRETURN
= 8;
365 private int yyerrstatus_
= 0;
368 * Return whether error recovery is being done. In this state, the parser
369 * reads token until it reaches a known state, and then restarts normal
371 public final boolean recovering ()
373 return yyerrstatus_
== 0;
376 private int yyaction (int yyn
, YYStack yystack
, int yylen
) ]b4_maybe_throws([b4_throws
])[
379 ]b4_locations_if([b4_location_type
[ yyloc
= yylloc (yystack
, yylen
);]])[
381 /* If YYLEN is nonzero, implement the default value of the action:
382 `$$ = $1'. Otherwise, use the top of the stack.
384 Otherwise, the following line sets YYVAL to garbage.
385 This behavior is undocumented and Bison
386 users should not rely upon it. */
388 yyval
= yystack
.valueAt (yylen
- 1);
390 yyval
= yystack
.valueAt (0);
392 yy_reduce_print (yyn
, yystack
);
400 yy_symbol_print ("-> $$ =", yyr1_
[yyn
], yyval
]b4_locations_if([, yyloc
])[);
405 /* Shift the result of the reduction. */
407 int yystate
= yypgoto_
[yyn
- yyntokens_
] + yystack
.stateAt (0);
408 if (0 <= yystate
&& yystate
<= yylast_
409 && yycheck_
[yystate
] == yystack
.stateAt (0))
410 yystate
= yytable_
[yystate
];
412 yystate
= yydefgoto_
[yyn
- yyntokens_
];
414 yystack
.push (yystate
, yyval
]b4_locations_if([, yyloc
])[);
418 ]b4_error_verbose_if([[
419 /* Return YYSTR after stripping away unnecessary quotes and
420 backslashes, so that it's suitable for yyerror. The heuristic is
421 that double-quoting is unnecessary unless the string contains an
422 apostrophe, a comma, or backslash (other than backslash-backslash).
423 YYSTR is taken from yytname. */
424 private final String
yytnamerr_ (String yystr
)
426 if (yystr
.charAt (0) == '"')
428 StringBuffer yyr
= new StringBuffer ();
429 strip_quotes
: for (int i
= 1; i
< yystr
.length (); i
++)
430 switch (yystr
.charAt (i
))
437 if (yystr
.charAt(++i
) != '\\')
441 yyr
.append (yystr
.charAt (i
));
445 return yyr
.toString ();
448 else if (yystr
.equals ("$end"))
449 return "end of input";
455 /*--------------------------------.
456 | Print this symbol on YYOUTPUT. |
457 `--------------------------------*/
459 private void yy_symbol_print (String s
, int yytype
,
460 ]b4_yystype
[ yyvaluep
]dnl
461 b4_locations_if([, Object yylocationp
])[)
464 yycdebug (s
+ (yytype
< yyntokens_ ?
" token " : " nterm ")
465 + yytname_
[yytype
] + " ("]b4_locations_if([
466 + yylocationp
+ ": "])[
467 + (yyvaluep
== null ?
"(null)" : yyvaluep
.toString ()) + ")");
471 * Parse input from the scanner that was specified at object construction
472 * time. Return whether the end of the input was reached successfully.
474 * @@return <tt>true</tt> if the parsing succeeds. Note that this does not
475 * imply that there were no syntax errors.
477 public boolean parse () ]b4_maybe_throws([b4_list2([b4_lex_throws
], [b4_throws
])])[
479 /// Lookahead and lookahead in internal form.
480 int yychar
= yyempty_
;
488 YYStack yystack
= new YYStack ();
490 /* Error handling. */
492 ]b4_locations_if([/// The location where the error started.
493 ]b4_location_type
[ yyerrloc
= null;
495 /// ]b4_location_type[ of the lookahead.
496 ]b4_location_type
[ yylloc
= new ]b4_location_type
[ (null, null);
499 ]b4_location_type
[ yyloc
;])
501 /// Semantic value of the lookahead.
502 b4_yystype
[ yylval
= null;
506 yycdebug ("Starting parse\n");
509 ]m4_ifdef([b4_initial_action
], [
510 m4_pushdef([b4_at_dollar
], [yylloc
])dnl
511 m4_pushdef([b4_dollar_dollar
], [yylval
])dnl
512 /* User initialization code. */
513 b4_user_initial_action
514 m4_popdef([b4_dollar_dollar
])dnl
515 m4_popdef([b4_at_dollar
])])dnl
517 [ /* Initialize the stack. */
518 yystack
.push (yystate
, yylval
]b4_locations_if([, yylloc
])[);
520 int label
= YYNEWSTATE
;
524 /* New state. Unlike in the C/C++ skeletons, the state is already
525 pushed when we come here. */
527 yycdebug ("Entering state " + yystate
+ "\n");
529 yystack
.print (yyDebugStream
);
532 if (yystate
== yyfinal_
)
535 /* Take a decision. First try without lookahead. */
536 yyn
= yypact_
[yystate
];
537 if (yy_pact_value_is_default_ (yyn
))
543 /* Read a lookahead token. */
544 if (yychar
== yyempty_
)
546 yycdebug ("Reading a token: ");
547 yychar
= yylexer
.yylex ();]
549 yylloc
= new ]b4_location_type
[(yylexer
.getStartPos (),
550 yylexer
.getEndPos ());]])
551 yylval
= yylexer
.getLVal ();[
554 /* Convert token to internal form. */
555 if (yychar
<= Lexer
.EOF
)
557 yychar
= yytoken
= Lexer
.EOF
;
558 yycdebug ("Now at end of input.\n");
562 yytoken
= yytranslate_ (yychar
);
563 yy_symbol_print ("Next token is", yytoken
,
564 yylval
]b4_locations_if([, yylloc
])[);
567 /* If the proper action on seeing token YYTOKEN is to reduce or to
568 detect an error, take that action. */
570 if (yyn
< 0 || yylast_
< yyn
|| yycheck_
[yyn
] != yytoken
)
573 /* <= 0 means reduce or error. */
574 else if ((yyn
= yytable_
[yyn
]) <= 0)
576 if (yy_table_value_is_error_ (yyn
))
587 /* Shift the lookahead token. */
588 yy_symbol_print ("Shifting", yytoken
,
589 yylval
]b4_locations_if([, yylloc
])[);
591 /* Discard the token being shifted. */
594 /* Count tokens shifted since error; after three, turn off error
596 if (yyerrstatus_
> 0)
600 yystack
.push (yystate
, yylval
]b4_locations_if([, yylloc
])[);
605 /*-----------------------------------------------------------.
606 | yydefault -- do the default action for the current state. |
607 `-----------------------------------------------------------*/
609 yyn
= yydefact_
[yystate
];
616 /*-----------------------------.
617 | yyreduce -- Do a reduction. |
618 `-----------------------------*/
621 label
= yyaction (yyn
, yystack
, yylen
);
622 yystate
= yystack
.stateAt (0);
625 /*------------------------------------.
626 | yyerrlab -- here on detecting error |
627 `------------------------------------*/
629 /* If not already recovering from an error, report this error. */
630 if (yyerrstatus_
== 0)
633 yyerror (]b4_locations_if([yylloc
, ])[yysyntax_error (yystate
, yytoken
));
636 ]b4_locations_if([yyerrloc
= yylloc
;])[
637 if (yyerrstatus_
== 3)
639 /* If just tried and failed to reuse lookahead token after an
640 error, discard it. */
642 if (yychar
<= Lexer
.EOF
)
644 /* Return failure if at end of input. */
645 if (yychar
== Lexer
.EOF
)
652 /* Else will try to reuse lookahead token after shifting the error
657 /*---------------------------------------------------.
658 | errorlab -- error raised explicitly by YYERROR. |
659 `---------------------------------------------------*/
662 ]b4_locations_if([yyerrloc
= yystack
.locationAt (yylen
- 1);])[
663 /* Do not reclaim the symbols of the rule which action triggered
667 yystate
= yystack
.stateAt (0);
671 /*-------------------------------------------------------------.
672 | yyerrlab1 -- common code for both syntax error and YYERROR. |
673 `-------------------------------------------------------------*/
675 yyerrstatus_
= 3; /* Each real token shifted decrements this. */
679 yyn
= yypact_
[yystate
];
680 if (!yy_pact_value_is_default_ (yyn
))
683 if (0 <= yyn
&& yyn
<= yylast_
&& yycheck_
[yyn
] == yyterror_
)
691 /* Pop the current state because it cannot handle the error token. */
692 if (yystack
.height
== 1)
695 ]b4_locations_if([yyerrloc
= yystack
.locationAt (0);])[
697 yystate
= yystack
.stateAt (0);
699 yystack
.print (yyDebugStream
);
703 /* Muck with the stack to setup for yylloc. */
704 yystack
.push (0, null, yylloc
);
705 yystack
.push (0, null, yyerrloc
);
706 yyloc
= yylloc (yystack
, 2);
709 /* Shift the error token. */
710 yy_symbol_print ("Shifting", yystos_
[yyn
],
711 yylval
]b4_locations_if([, yyloc
])[);
714 yystack
.push (yyn
, yylval
]b4_locations_if([, yyloc
])[);
728 // Generate an error message.
729 private String
yysyntax_error (int yystate
, int tok
)
730 {]b4_error_verbose_if([[
733 int yyn
= yypact_
[yystate
];
734 if (yypact_ninf_
< yyn
&& yyn
<= yylast_
)
738 /* Start YYX at -YYN if negative to avoid negative indexes in
739 YYCHECK. In other words, skip the first -YYN actions for this
740 state because they are default actions. */
741 int yyxbegin
= yyn
< 0 ?
-yyn
: 0;
743 /* Stay within bounds of both yycheck and yytname. */
744 int yychecklim
= yylast_
- yyn
+ 1;
745 int yyxend
= yychecklim
< yyntokens_ ? yychecklim
: yyntokens_
;
747 for (int x
= yyxbegin
; x
< yyxend
; ++x
)
748 if (yycheck_
[x
+ yyn
] == x
&& x
!= yyterror_
749 && !yy_table_value_is_error_ (yytable_
[x
+ yyn
]))
752 // FIXME: This method of building the message is not compatible
753 // with internationalization.
754 res
= new StringBuffer ("syntax error, unexpected ");
755 res
.append (yytnamerr_ (yytname_
[tok
]));
759 for (int x
= yyxbegin
; x
< yyxend
; ++x
)
760 if (yycheck_
[x
+ yyn
] == x
&& x
!= yyterror_
761 && !yy_table_value_is_error_ (yytable_
[x
+ yyn
]))
763 res
.append (count
++ == 0 ?
", expecting " : " or ");
764 res
.append (yytnamerr_ (yytname_
[x
]));
767 return res
.toString ();
771 return "syntax error";
775 * Whether the given <code>yypact_</code> value indicates a defaulted state.
776 * @@param yyvalue the value to check
778 private static boolean yy_pact_value_is_default_ (int yyvalue
)
780 return yyvalue
== yypact_ninf_
;
784 * Whether the given <code>yytable_</code> value indicates a syntax error.
785 * @@param yyvalue the value to check
787 private static boolean yy_table_value_is_error_ (int yyvalue
)
789 return yyvalue
== yytable_ninf_
;
792 private static final ]b4_int_type_for([b4_pact
])[ yypact_ninf_
= ]b4_pact_ninf
[;
793 private static final ]b4_int_type_for([b4_table
])[ yytable_ninf_
= ]b4_table_ninf
[;
795 ]b4_parser_tables_define
[
796 ]b4_integral_parser_table_define([token_number
], [b4_toknum
],
797 [TOKEN_NUMBER_
[YYLEX
-NUM
] -- Internal symbol number corresponding
800 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
801 First, the terminals, then, starting at \a yyntokens_, nonterminals. */
802 ]b4_typed_parser_table_define([String
], [tname
], [b4_tname
])[
804 ]b4_integral_parser_table_define([rline
], [b4_rline
],
805 [YYRLINE
[YYN
] -- Source line where rule number YYN was defined
.])[
807 // Report on the debug stream that the rule yyrule is going to be reduced.
808 private void yy_reduce_print (int yyrule
, YYStack yystack
)
813 int yylno
= yyrline_
[yyrule
];
814 int yynrhs
= yyr2_
[yyrule
];
815 /* Print the symbols being reduced, and their result. */
816 yycdebug ("Reducing stack by rule " + (yyrule
- 1)
817 + " (line " + yylno
+ "), ");
819 /* The symbols being reduced. */
820 for (int yyi
= 0; yyi
< yynrhs
; yyi
++)
821 yy_symbol_print (" $" + (yyi
+ 1) + " =",
822 yystos_
[yystack
.stateAt(yyi
+ 1 - yynrhs
)],
823 ]b4_rhs_value(yynrhs
, yyi
+ 1)b4_locations_if([,
824 b4_rhs_location(yynrhs
, yyi
+ 1)])[);
827 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
828 ]b4_integral_parser_table_define([translate_table
], [b4_translate
])[
830 private static final ]b4_int_type_for([b4_translate
])[ yytranslate_ (int t
)
832 if (t
>= 0 && t
<= yyuser_token_number_max_
)
833 return yytranslate_table_
[t
];
835 return yyundef_token_
;
838 private static final int yylast_
= ]b4_last
[;
839 private static final int yynnts_
= ]b4_nterms_number
[;
840 private static final int yyempty_
= -2;
841 private static final int yyfinal_
= ]b4_final_state_number
[;
842 private static final int yyterror_
= 1;
843 private static final int yyerrcode_
= 256;
844 private static final int yyntokens_
= ]b4_tokens_number
[;
846 private static final int yyuser_token_number_max_
= ]b4_user_token_number_max
[;
847 private static final int yyundef_token_
= ]b4_undef_token_number
[;
849 ]/* User implementation code. */
850 b4_percent_code_get
[]dnl