1 # Java skeleton
for Bison
-*- autoconf
-*-
3 #
Copyright (C
) 2007 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
], [b4_skeleton
])])
21 m4_ifval(m4_defn([b4_symbol_destructors
]),
22 [b4_fatal([%s
: %%destructor does not make sense in Java
], [b4_skeleton
])],
26 @output(b4_parser_file_name@
)
27 b4_copyright([Skeleton implementation
for Bison
LALR(1) parsers in Java
],
30 b4_percent_define_ifdef([package], [package b4_percent_define_get([package]);
31 ])[/* First part of user declarations. */
33 b4_percent_code_get([[imports
]])
35 * A Bison parser, automatically generated from <tt>@ofile@</tt>.
37 * @@author LALR (1) parser skeleton written by Paolo Bonzini.
39 ]b4_public_if([public ])b4_abstract_if([abstract ])[class ]b4_parser_class_name
[
43 /** True if verbose error messages are enabled. */
44 public boolean errorVerbose
= ]b4_flag_value([error_verbose
]);
48 * A class defining a pair of positions. Positions, defined by the
49 * <code>]b4_position_type[</code> class, denote a point in the input.
50 * Locations represent a part of the input through the beginning
51 * and ending positions. */
52 public class ]b4_location_type
[ {
53 /** The first, inclusive, position in the range. */
54 public ]b4_position_type
[ begin
;
56 /** The first position beyond the range. */
57 public ]b4_position_type
[ end
;
60 * Create a ]b4_location_type[ denoting an empty range located at
62 * @@param loc The position at which the range is anchored. */
63 public ]b4_location_type
[ (]b4_position_type
[ loc
) {
64 this.begin
= this.end
= loc
;
68 * Create a <code>]b4_location_type[</code> from the endpoints of the range.
69 * @@param begin The first position included in the range.
70 * @@param begin The first position beyond the range. */
71 public ]b4_location_type
[ (]b4_position_type
[ begin
, ]b4_position_type
[ end
) {
77 * Print a representation of the location. For this to be correct,
78 * <code>]b4_position_type[</code> should override the <code>equals</code>
80 public String
toString () {
81 if (begin
.equals (end
))
82 return begin
.toString ();
84 return begin
.toString () + "-" + end
.toString ();
90 [ /** Token returned by the scanner to signal the end of its input. */
91 public static final int EOF
= 0;]
93 b4_token_enums(b4_tokens
)
96 private ]b4_location_type
[ yylloc (YYStack rhs
, int n
)
99 return new ]b4_location_type
[ (rhs
.locationAt (1).begin
, rhs
.locationAt (n
).end
);
101 return new ]b4_location_type
[ (rhs
.locationAt (0).end
);
105 * Communication interface between the scanner and the Bison-generated
106 * parser <tt>]b4_parser_class_name[</tt>.
108 public interface Lexer
{
109 ]b4_locations_if([[/**
110 * Method to retrieve the beginning position of the last scanned token.
111 * @@return the position at which the last scanned token starts. */
112 ]b4_position_type
[ getStartPos ();
115 * Method to retrieve the ending position of the last scanned token.
116 * @@return the first position beyond the last scanned token. */
117 ]b4_position_type
[ getEndPos ();]])[
120 * Method to retrieve the semantic value of the last scanned token.
121 * @@return the semantic value of the last scanned token. */
122 ]b4_yystype
[ getLVal ();]
125 * Entry point for the scanner. Returns the token identifier corresponding
126 * to the next token and ]b4_pure_if([prepares to return], [stores])[
127 * the semantic value]b4_locations_if([ and beginning/ending positions])[
129 * @@return the token identifier corresponding to the next token. */
130 int yylex () ]b4_maybe_throws([b4_lex_throws
])[;
133 * Entry point for error reporting. Emits an error
134 * ]b4_locations_if([ referring to the given location])[in a user-defined
137 * ]b4_locations_if([loc], [[The location of the element to which the
138 * error message is related]])[
139 * @@param s The string for the error message. */
140 void yyerror (]b4_locations_if([b4_location_type
[ loc
, ]])[String s
);]
143 b4_lexer_if([[private class YYLexer
implements Lexer
{
144 ]b4_percent_code_get([[lexer
]])[
147 ]])[/** The object doing lexical analysis for us. */
148 private Lexer yylexer
;
154 * Instantiates the Bison-generated parser.
156 public ]b4_parser_class_name (b4_parse_param_decl([b4_lex_param_decl
])[) {
157 this.yylexer
= new YYLexer(]b4_lex_param_call
[);
158 ]b4_parse_param_cons
[
163 * Instantiates the Bison-generated parser.
164 * @@param yylex The scanner that will supply tokens to the parser.
166 b4_lexer_if([[protected]], [[public]]) b4_parser_class_name
[ (]b4_parse_param_decl([[Lexer yylexer
]])[) {
167 this.yylexer
= yylexer
;
168 ]b4_parse_param_cons
[
171 private java
.io
.PrintStream yyDebugStream
= System
.err
;
174 * Return the <tt>PrintStream</tt> on which the debugging output is
177 public final java
.io
.PrintStream
getDebugStream () { return yyDebugStream
; }
180 * Set the <tt>PrintStream</tt> on which the debug output is printed.
181 * @@param s The stream that is used for debugging output.
183 public final void setDebugStream(java
.io
.PrintStream s
) { yyDebugStream
= s
; }
185 private int yydebug
= 0;
188 * Answer the verbosity of the debugging output; 0 means that all kinds of
189 * output from the parser are suppressed.
191 public final int getDebugLevel() { return yydebug
; }
194 * Set the verbosity of the debugging output; 0 means that all kinds of
195 * output from the parser are suppressed.
196 * @@param level The verbosity level for debugging output.
198 public final void setDebugLevel(int level
) { yydebug
= level
; }
200 private final int yylex () ]b4_maybe_throws([b4_lex_throws
]) [{
201 return yylexer
.yylex ();
203 protected final void yyerror (]b4_locations_if([b4_location_type
[ loc
, ]])[String s
) {
204 yylexer
.yyerror (]b4_locations_if([loc
, ])[s
);
208 protected final void yyerror (String s
) {
209 yylexer
.yyerror ((Location
)null, s
);
211 protected final void yyerror (]b4_position_type
[ loc
, String s
) {
212 yylexer
.yyerror (new ]b4_location_type
[ (loc
), s
);
215 [protected final void yycdebug (String s
) {
217 yyDebugStream
.println (s
);
220 private final class YYStack
{
221 private int[] stateStack
= new int[16];
222 ]b4_locations_if([[private ]b4_location_type
[[] locStack
= new ]b4_location_type
[[16];]])[
223 private ]b4_yystype
[[] valueStack
= new ]b4_yystype
[[16];
225 public int size
= 16;
226 public int height
= -1;
228 public final void push (int state
, ]b4_yystype
[ value
]dnl
229 b4_locations_if([, ]b4_location_type
[ loc
])[) {
233 int[] newStateStack
= new int[size
* 2];
234 System
.arraycopy (stateStack
, 0, newStateStack
, 0, height
);
235 stateStack
= newStateStack
;
237 ]b4_location_type
[[] newLocStack
= new ]b4_location_type
[[size
* 2];
238 System
.arraycopy (locStack
, 0, newLocStack
, 0, height
);
239 locStack
= newLocStack
;]])
241 b4_yystype
[[] newValueStack
= new ]b4_yystype
[[size
* 2];
242 System
.arraycopy (valueStack
, 0, newValueStack
, 0, height
);
243 valueStack
= newValueStack
;
248 stateStack
[height
] = state
;
249 ]b4_locations_if([[locStack
[height
] = loc
;]])[
250 valueStack
[height
] = value
;
253 public final void pop () {
257 public final void pop (int num
) {
258 // Avoid memory leaks... garbage collection is a white lie!
260 java
.util
.Arrays
.fill (valueStack
, height
- num
+ 1, height
, null);
261 ]b4_locations_if([[java
.util
.Arrays
.fill (locStack
, height
- num
+ 1, height
, null);]])[
266 public final int stateAt (int i
) {
267 return stateStack
[height
- i
];
270 ]b4_locations_if([[public final ]b4_location_type
[ locationAt (int i
) {
271 return locStack
[height
- i
];
274 ]])[public final ]b4_yystype
[ valueAt (int i
) {
275 return valueStack
[height
- i
];
278 // Print the state stack on the debug stream.
279 public void print (java
.io
.PrintStream out
)
281 out
.print ("Stack now");
283 for (int i
= 0; i
< height
; i
++)
286 out
.print (stateStack
[i
]);
293 * Returned by a Bison action in order to stop the parsing process and
294 * return success (<tt>true</tt>). */
295 public static final int YYACCEPT
= 0;
298 * Returned by a Bison action in order to stop the parsing process and
299 * return failure (<tt>false</tt>). */
300 public static final int YYABORT
= 1;
303 * Returned by a Bison action in order to start error recovery without
304 * printing an error message. */
305 public static final int YYERROR
= 2;
308 * Returned by a Bison action in order to print an error message and start
310 public static final int YYFAIL
= 3;
312 private static final int YYNEWSTATE
= 4;
313 private static final int YYDEFAULT
= 5;
314 private static final int YYREDUCE
= 6;
315 private static final int YYERRLAB1
= 7;
316 private static final int YYRETURN
= 8;
318 private int yyerrstatus_
= 0;
321 * Return whether error recovery is being done. In this state, the parser
322 * reads token until it reaches a known state, and then restarts normal
324 public final boolean recovering ()
326 return yyerrstatus_
== 0;
329 private int yyaction (int yyn
, YYStack yystack
, int yylen
)
332 ]b4_locations_if([b4_location_type
[ yyloc
= yylloc (yystack
, yylen
);]])[
334 /* If YYLEN is nonzero, implement the default value of the action:
335 `$$ = $1'. Otherwise, use the top of the stack.
337 Otherwise, the following line sets YYVAL to garbage.
338 This behavior is undocumented and Bison
339 users should not rely upon it. */
341 yyval
= yystack
.valueAt (yylen
- 1);
343 yyval
= yystack
.valueAt (0);
345 yy_reduce_print (yyn
, yystack
);
353 yy_symbol_print ("-> $$ =", yyr1_
[yyn
], yyval
]b4_locations_if([, yyloc
])[);
358 /* Shift the result of the reduction. */
360 int yystate
= yypgoto_
[yyn
- yyntokens_
] + yystack
.stateAt (0);
361 if (0 <= yystate
&& yystate
<= yylast_
362 && yycheck_
[yystate
] == yystack
.stateAt (0))
363 yystate
= yytable_
[yystate
];
365 yystate
= yydefgoto_
[yyn
- yyntokens_
];
367 yystack
.push (yystate
, yyval
]b4_locations_if([, yyloc
])[);
371 /* Return YYSTR after stripping away unnecessary quotes and
372 backslashes, so that it's suitable for yyerror. The heuristic is
373 that double-quoting is unnecessary unless the string contains an
374 apostrophe, a comma, or backslash (other than backslash-backslash).
375 YYSTR is taken from yytname. */
376 private final String
yytnamerr_ (String yystr
)
378 if (yystr
.charAt (0) == '"')
380 StringBuffer yyr
= new StringBuffer ();
381 strip_quotes
: for (int i
= 1; i
< yystr
.length (); i
++)
382 switch (yystr
.charAt (i
))
389 if (yystr
.charAt(++i
) != '\\')
393 yyr
.append (yystr
.charAt (i
));
397 return yyr
.toString ();
400 else if (yystr
.equals ("$end"))
401 return "end of input";
406 /*--------------------------------.
407 | Print this symbol on YYOUTPUT. |
408 `--------------------------------*/
410 private void yy_symbol_print (String s
, int yytype
,
411 ]b4_yystype
[ yyvaluep
]dnl
412 b4_locations_if([, Object yylocationp
])[)
415 yycdebug (s
+ (yytype
< yyntokens_ ?
" token " : " nterm ")
416 + yytname_
[yytype
] + " ("]b4_locations_if([
417 + yylocationp
+ ": "])[
418 + (yyvaluep
== null ?
"(null)" : yyvaluep
) + ")");
422 * Parse input from the scanner that was specified at object construction
423 * time. Return whether the end of the input was reached successfully.
425 * @@return <tt>true</tt> if the parsing succeeds. Note that this does not
426 * imply that there were no syntax errors.
428 public boolean parse () ]b4_maybe_throws([b4_throws
])[
430 /// Lookahead and lookahead in internal form.
431 int yychar
= yyempty_
;
439 YYStack yystack
= new YYStack ();
441 /* Error handling. */
443 ]b4_locations_if([/// The location where the error started.
444 ]b4_location_type
[ yyerrloc
= null;
446 /// ]b4_location_type[ of the lookahead.
447 ]b4_location_type
[ yylloc
= new ]b4_location_type
[ (null, null);
450 ]b4_location_type
[ yyloc
;])
452 /// Semantic value of the lookahead.
453 b4_yystype
[ yylval
= null;
457 yycdebug ("Starting parse\n");
460 ]m4_ifdef([b4_initial_action
], [
461 m4_pushdef([b4_at_dollar
], [yylloc
])dnl
462 m4_pushdef([b4_dollar_dollar
], [yylval
])dnl
463 /* User initialization code. */
464 b4_user_initial_action
465 m4_popdef([b4_dollar_dollar
])dnl
466 m4_popdef([b4_at_dollar
])])dnl
468 [ /* Initialize the stack. */
469 yystack
.push (yystate
, yylval
]b4_locations_if([, yylloc
])[);
471 int label
= YYNEWSTATE
;
475 /* New state. Unlike in the C/C++ skeletons, the state is already
476 pushed when we come here. */
478 yycdebug ("Entering state " + yystate
+ "\n");
480 yystack
.print (yyDebugStream
);
483 if (yystate
== yyfinal_
)
486 /* Take a decision. First try without lookahead. */
487 yyn
= yypact_
[yystate
];
488 if (yyn
== yypact_ninf_
)
494 /* Read a lookahead token. */
495 if (yychar
== yyempty_
)
497 yycdebug ("Reading a token: ");
500 yylloc
= new ]b4_location_type
[(yylexer
.getStartPos (),
501 yylexer
.getEndPos ());]])
502 yylval
= yylexer
.getLVal ();[
505 /* Convert token to internal form. */
508 yychar
= yytoken
= EOF
;
509 yycdebug ("Now at end of input.\n");
513 yytoken
= yytranslate_ (yychar
);
514 yy_symbol_print ("Next token is", yytoken
,
515 yylval
]b4_locations_if([, yylloc
])[);
518 /* If the proper action on seeing token YYTOKEN is to reduce or to
519 detect an error, take that action. */
521 if (yyn
< 0 || yylast_
< yyn
|| yycheck_
[yyn
] != yytoken
)
524 /* <= 0 means reduce or error. */
525 else if ((yyn
= yytable_
[yyn
]) <= 0)
527 if (yyn
== 0 || yyn
== yytable_ninf_
)
538 /* Shift the lookahead token. */
539 yy_symbol_print ("Shifting", yytoken
,
540 yylval
]b4_locations_if([, yylloc
])[);
542 /* Discard the token being shifted. */
545 /* Count tokens shifted since error; after three, turn off error
547 if (yyerrstatus_
> 0)
551 yystack
.push (yystate
, yylval
]b4_locations_if([, yylloc
])[);
556 /*-----------------------------------------------------------.
557 | yydefault -- do the default action for the current state. |
558 `-----------------------------------------------------------*/
560 yyn
= yydefact_
[yystate
];
567 /*-----------------------------.
568 | yyreduce -- Do a reduction. |
569 `-----------------------------*/
572 label
= yyaction (yyn
, yystack
, yylen
);
573 yystate
= yystack
.stateAt (0);
576 /*------------------------------------.
577 | yyerrlab -- here on detecting error |
578 `------------------------------------*/
580 /* If not already recovering from an error, report this error. */
581 if (yyerrstatus_
== 0)
584 yyerror (]b4_locations_if([yylloc
, ])[yysyntax_error (yystate
, yytoken
));
587 ]b4_locations_if([yyerrloc
= yylloc
;])[
588 if (yyerrstatus_
== 3)
590 /* If just tried and failed to reuse lookahead token after an
591 error, discard it. */
595 /* Return failure if at end of input. */
603 /* Else will try to reuse lookahead token after shifting the error
608 /*---------------------------------------------------.
609 | errorlab -- error raised explicitly by YYERROR. |
610 `---------------------------------------------------*/
613 ]b4_locations_if([yyerrloc
= yystack
.locationAt (yylen
- 1);])[
614 /* Do not reclaim the symbols of the rule which action triggered
618 yystate
= yystack
.stateAt (0);
622 /*-------------------------------------------------------------.
623 | yyerrlab1 -- common code for both syntax error and YYERROR. |
624 `-------------------------------------------------------------*/
626 yyerrstatus_
= 3; /* Each real token shifted decrements this. */
630 yyn
= yypact_
[yystate
];
631 if (yyn
!= yypact_ninf_
)
634 if (0 <= yyn
&& yyn
<= yylast_
&& yycheck_
[yyn
] == yyterror_
)
642 /* Pop the current state because it cannot handle the error token. */
643 if (yystack
.height
== 1)
646 ]b4_locations_if([yyerrloc
= yystack
.locationAt (0);])[
648 yystate
= yystack
.stateAt (0);
650 yystack
.print (yyDebugStream
);
654 /* Muck with the stack to setup for yylloc. */
655 yystack
.push (0, null, yylloc
);
656 yystack
.push (0, null, yyerrloc
);
657 yyloc
= yylloc (yystack
, 2);
660 /* Shift the error token. */
661 yy_symbol_print ("Shifting", yystos_
[yyn
],
662 yylval
]b4_locations_if([, yyloc
])[);
665 yystack
.push (yyn
, yylval
]b4_locations_if([, yyloc
])[);
679 // Generate an error message.
680 private String
yysyntax_error (int yystate
, int tok
)
684 int yyn
= yypact_
[yystate
];
685 if (yypact_ninf_
< yyn
&& yyn
<= yylast_
)
689 /* Start YYX at -YYN if negative to avoid negative indexes in
691 int yyxbegin
= yyn
< 0 ?
-yyn
: 0;
693 /* Stay within bounds of both yycheck and yytname. */
694 int yychecklim
= yylast_
- yyn
+ 1;
695 int yyxend
= yychecklim
< yyntokens_ ? yychecklim
: yyntokens_
;
697 for (int x
= yyxbegin
; x
< yyxend
; ++x
)
698 if (yycheck_
[x
+ yyn
] == x
&& x
!= yyterror_
)
701 // FIXME: This method of building the message is not compatible
702 // with internationalization.
703 res
= new StringBuffer ("syntax error, unexpected ");
704 res
.append (yytnamerr_ (yytname_
[tok
]));
708 for (int x
= yyxbegin
; x
< yyxend
; ++x
)
709 if (yycheck_
[x
+ yyn
] == x
&& x
!= yyterror_
)
711 res
.append (count
++ == 0 ?
", expecting " : " or ");
712 res
.append (yytnamerr_ (yytname_
[x
]));
715 return res
.toString ();
719 return "syntax error";
723 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
725 private static final ]b4_int_type_for([b4_pact
])[ yypact_ninf_
= ]b4_pact_ninf
[;
726 private static final ]b4_int_type_for([b4_pact
])[ yypact_
[] =
731 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
732 doesn't specify something else to do. Zero means the default is an
734 private static final ]b4_int_type_for([b4_defact
])[ yydefact_
[] =
739 /* YYPGOTO[NTERM-NUM]. */
740 private static final ]b4_int_type_for([b4_pgoto
])[ yypgoto_
[] =
745 /* YYDEFGOTO[NTERM-NUM]. */
746 private static final ]b4_int_type_for([b4_defgoto
])[
752 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
753 positive, shift that token. If negative, reduce the rule which
754 number is the opposite. If zero, do what YYDEFACT says. */
755 private static final ]b4_int_type_for([b4_table
])[ yytable_ninf_
= ]b4_table_ninf
[;
756 private static final ]b4_int_type_for([b4_table
])[
763 private static final ]b4_int_type_for([b4_check
])[
769 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
770 symbol of state STATE-NUM. */
771 private static final ]b4_int_type_for([b4_stos
])[
777 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding
779 private static final ]b4_int_type_for([b4_toknum
])[
785 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
786 private static final ]b4_int_type_for([b4_r1
])[
792 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
793 private static final ]b4_int_type_for([b4_r2
])[
799 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
800 First, the terminals, then, starting at \a yyntokens_, nonterminals. */
801 private static final String yytname_
[] =
806 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
807 private static final ]b4_int_type_for([b4_rhs
])[ yyrhs_
[] =
812 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
814 private static final ]b4_int_type_for([b4_prhs
])[ yyprhs_
[] =
819 /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
820 private static final ]b4_int_type_for([b4_rline
])[ yyrline_
[] =
825 // Report on the debug stream that the rule yyrule is going to be reduced.
826 private void yy_reduce_print (int yyrule
, YYStack yystack
)
831 int yylno
= yyrline_
[yyrule
];
832 int yynrhs
= yyr2_
[yyrule
];
833 /* Print the symbols being reduced, and their result. */
834 yycdebug ("Reducing stack by rule " + (yyrule
- 1)
835 + " (line " + yylno
+ "), ");
837 /* The symbols being reduced. */
838 for (int yyi
= 0; yyi
< yynrhs
; yyi
++)
839 yy_symbol_print (" $" + (yyi
+ 1) + " =",
840 yyrhs_
[yyprhs_
[yyrule
] + yyi
],
841 ]b4_rhs_value(yynrhs
, yyi
+ 1)b4_locations_if([,
842 b4_rhs_location(yynrhs
, yyi
+ 1)])[);
845 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
846 private static final ]b4_int_type_for([b4_translate
])[ yytranslate_table_
[] =
851 private static final ]b4_int_type_for([b4_translate
])[ yytranslate_ (int t
)
853 if (t
>= 0 && t
<= yyuser_token_number_max_
)
854 return yytranslate_table_
[t
];
856 return yyundef_token_
;
859 private static final int yylast_
= ]b4_last
[;
860 private static final int yynnts_
= ]b4_nterms_number
[;
861 private static final int yyempty_
= -2;
862 private static final int yyfinal_
= ]b4_final_state_number
[;
863 private static final int yyterror_
= 1;
864 private static final int yyerrcode_
= 256;
865 private static final int yyntokens_
= ]b4_tokens_number
[;
867 private static final int yyuser_token_number_max_
= ]b4_user_token_number_max
[;
868 private static final int yyundef_token_
= ]b4_undef_token_number
[;
870 ]/* User implementation code. */
871 b4_percent_code_get
[]dnl