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
2 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
, write to the Free Software
17 # Foundation
, Inc
., 51 Franklin Street
, Fifth Floor
, Boston
, MA
20 m4_include(b4_pkgdatadir
/[java
.m4
])
22 b4_defines_if([b4_fatal([%s
: %%defines does not make sense in Java
], [b4_skeleton
])])
23 m4_ifval(m4_defn([b4_symbol_destructors
]),
24 [b4_fatal([%s
: %%destructor does not make sense in Java
], [b4_skeleton
])],
28 @output(b4_parser_file_name@
)
29 b4_copyright([Skeleton implementation
for Bison
LALR(1) parsers in Java
],
32 b4_percent_define_ifdef([package], [package b4_percent_define_get([package]);
33 ])[/* First part of user declarations. */
35 b4_percent_code_get([[imports
]])
37 * A Bison parser, automatically generated from <tt>@ofile@</tt>.
39 * @@author LALR (1) parser skeleton written by Paolo Bonzini.
41 ]b4_public_if([public ])b4_abstract_if([abstract ])[class ]b4_parser_class_name
[
45 /** True if verbose error messages are enabled. */
46 public boolean errorVerbose
= ]b4_flag_value([error_verbose
]);
50 * A class defining a pair of positions. Positions, defined by the
51 * <code>]b4_position_type[</code> class, denote a point in the input.
52 * Locations represent a part of the input through the beginning
53 * and ending positions. */
54 public class ]b4_location_type
[ {
55 /** The first, inclusive, position in the range. */
56 public ]b4_position_type
[ begin
;
58 /** The first position beyond the range. */
59 public ]b4_position_type
[ end
;
62 * Create a ]b4_location_type[ denoting an empty range located at
64 * @@param loc The position at which the range is anchored. */
65 public ]b4_location_type
[ (]b4_position_type
[ loc
) {
66 this.begin
= this.end
= loc
;
70 * Create a <code>]b4_location_type[</code> from the endpoints of the range.
71 * @@param begin The first position included in the range.
72 * @@param begin The first position beyond the range. */
73 public ]b4_location_type
[ (]b4_position_type
[ begin
, ]b4_position_type
[ end
) {
79 * Print a representation of the location. For this to be correct,
80 * <code>]b4_position_type[</code> should override the <code>equals</code>
82 public String
toString () {
83 if (begin
.equals (end
))
84 return begin
.toString ();
86 return begin
.toString () + "-" + end
.toString ();
92 [ /** Token returned by the scanner to signal the end of its input. */
93 public static final int EOF
= 0;]
95 b4_token_enums(b4_tokens
)
98 private ]b4_location_type
[ yylloc (Stack rhs
, int n
)
101 return new ]b4_location_type
[ (rhs
.locationAt (1).begin
, rhs
.locationAt (n
).end
);
103 return new ]b4_location_type
[ (rhs
.locationAt (0).end
);
107 * Communication interface between the scanner and the Bison-generated
108 * parser <tt>]b4_parser_class_name[</tt>.
110 public interface Lexer
{
111 ]b4_locations_if([[/**
112 * Method to retrieve the beginning position of the last scanned token.
113 * @@return the position at which the last scanned token starts. */
114 ]b4_position_type
[ getStartPos ();
117 * Method to retrieve the ending position of the last scanned token.
118 * @@return the first position beyond the last scanned token. */
119 ]b4_position_type
[ getEndPos ();]])[
122 * Method to retrieve the semantic value of the last scanned token.
123 * @@return the semantic value of the last scanned token. */
124 ]b4_union_name
[ getLVal ();]], [[
127 * A place where the scanner can store the beginning position of the
128 * last scanned token. */
129 ]b4_locations_if([b4_position_type
[ yystartpos
;]])[
132 * A place where the scanner can store the ending position of the last
133 * scanned token, i.e. the first position beyond the last scanned token. */
134 ]b4_locations_if([b4_position_type
[ yyendpos
;]])[
137 * A place where the scanner can store the semantic value of the
138 * last scanned token. */
139 protected ]b4_union_name
[ yylval
;]])
141 b4_single_class_if([], [[/**
142 * Entry point for the scanner. Returns the token identifier corresponding
143 * to the next token and ]b4_pure_if([prepares to return], [stores])[
144 * the semantic value]b4_locations_if([ and beginning/ending positions])[
146 * @@return the token identifier corresponding to the next token. */
147 abstract int yylex (]b4_lex_param_decl
) b4_maybe_throws([b4_lex_throws
])[;
150 * Entry point for error reporting. Emits an error
151 * ]b4_locations_if([ referring to the given location])[in a user-defined
154 * ]b4_locations_if([loc], [[The location of the element to which the
155 * error message is related]])[
156 * @@param s The string for the error message. */
157 abstract void yyerror (]b4_locations_if([b4_location_type
[ loc
, ]])[String s
);]])
160 /** The object doing lexical analysis for us. */
161 private Lexer yylex
;])
165 * Instantiates the Bison-generated parser. ]b4_pure_if([
166 * @@param yylex The scanner that will supply tokens to the parser.])[
168 public ]b4_parser_class_name
[ (]b4_parse_param_decl([b4_pure_if([Lexer yylex
])])[) {
169 ]b4_pure_if(this.yylex
= yylex
;)
173 private java
.io
.PrintStream debugStream
= System
.err
;
176 * Return the <tt>PrintStream</tt> on which the debugging output is
179 public final java
.io
.PrintStream
getDebugStream () { return debugStream
; }
182 * Set the <tt>PrintStream</tt> on which the debug output is printed.
183 * @@param s The stream that is used for debugging output.
185 public final void setDebugStream(java
.io
.PrintStream s
) { debugStream
= s
; }
187 private int yydebug
= 0;
190 * Answer the verbosity of the debugging output; 0 means that all kinds of
191 * output from the parser are suppressed.
193 public final int getDebugLevel() { return yydebug
; }
196 * Set the verbosity of the debugging output; 0 means that all kinds of
197 * output from the parser are suppressed.
198 * @@param level The verbosity level for debugging output.
200 public final void setDebugLevel(int level
) { yydebug
= level
; }
203 private final int yylex (]b4_lex_param_decl
) b4_maybe_throws([b4_lex_throws
]) [{
204 return yylex
.yylex (]b4_lex_param_call
[);
206 protected final void yyerror (]b4_locations_if([b4_location_type
[ loc
, ]])[String s
) {
207 yylex
.yyerror (]b4_locations_if([loc
, ])[s
);
210 protected final void yyerror (String s
) {
211 yyerror ((Location
)null, s
);
213 protected final void yyerror (]b4_position_type
[ loc
, String s
) {
214 yyerror (new ]b4_location_type
[ (loc
), s
);
217 [protected final void yycdebug (String s
) {
219 debugStream
.println (s
);
222 private final class Stack
{
223 private int[] stateStack
= new int[16];
224 ]b4_locations_if([[private ]b4_location_type
[[] locStack
= new ]b4_location_type
[[16];]])[
225 private ]b4_union_name
[[] valueStack
= new ]b4_union_name
[[16];
227 public int size
= 16;
228 public int height
= -1;
230 public final void push (int state
, ]b4_union_name
[ value
]dnl
231 b4_locations_if([, ]b4_location_type
[ loc
])[) {
235 int[] newStateStack
= new int[size
* 2];
236 System
.arraycopy (stateStack
, 0, newStateStack
, 0, height
);
237 stateStack
= newStateStack
;
239 ]b4_location_type
[[] newLocStack
= new ]b4_location_type
[[size
* 2];
240 System
.arraycopy (locStack
, 0, newLocStack
, 0, height
);
241 locStack
= newLocStack
;]])
243 b4_union_name
[[] newValueStack
= new ]b4_union_name
[[size
* 2];
244 System
.arraycopy (valueStack
, 0, newValueStack
, 0, height
);
245 valueStack
= newValueStack
;
250 stateStack
[height
] = state
;
251 ]b4_locations_if([[locStack
[height
] = loc
;]])[
252 valueStack
[height
] = value
;
255 public final void pop () {
259 public final void pop (int num
) {
260 // Avoid memory leaks... garbage collection is a white lie!
262 java
.util
.Arrays
.fill (valueStack
, height
- num
+ 1, height
, null);
263 ]b4_locations_if([[java
.util
.Arrays
.fill (locStack
, height
- num
+ 1, height
, null);]])[
268 public final int stateAt (int i
) {
269 return stateStack
[height
- i
];
272 ]b4_locations_if([[public final ]b4_location_type
[ locationAt (int i
) {
273 return locStack
[height
- i
];
276 ]])[public final ]b4_union_name
[ valueAt (int i
) {
277 return valueStack
[height
- i
];
280 // Print the state stack on the debug stream.
281 public void print (java
.io
.PrintStream out
)
283 out
.print ("Stack now");
285 for (int i
= 0; i
< height
; i
++)
288 out
.print (stateStack
[i
]);
295 * Returned by a Bison action in order to stop the parsing process and
296 * return success (<tt>true</tt>). */
297 public static final int YYACCEPT
= 0;
300 * Returned by a Bison action in order to stop the parsing process and
301 * return failure (<tt>false</tt>). */
302 public static final int YYABORT
= 1;
305 * Returned by a Bison action in order to start error recovery without
306 * printing an error message. */
307 public static final int YYERROR
= 2;
310 * Returned by a Bison action in order to print an error message and start
312 public static final int YYFAIL
= 3;
314 private static final int YYNEWSTATE
= 4;
315 private static final int YYDEFAULT
= 5;
316 private static final int YYREDUCE
= 6;
317 private static final int YYERRLAB1
= 7;
318 private static final int YYRETURN
= 8;
320 private int yyerrstatus_
= 0;
323 * Return whether error recovery is being done. In this state, the parser
324 * reads token until it reaches a known state, and then restarts normal
326 public final boolean yyrecovering ()
328 return yyerrstatus_
== 0;
331 private int yyaction (int yyn
, Stack yystack
, int yylen
)
333 ]b4_union_name
[ yyval
;
334 ]b4_locations_if([b4_location_type
[ yyloc
= yylloc (yystack
, yylen
);]])[
336 /* If YYLEN is nonzero, implement the default value of the action:
337 `$$ = $1'. Otherwise, use the top of the stack.
339 Otherwise, the following line sets YYVAL to garbage.
340 This behavior is undocumented and Bison
341 users should not rely upon it. */
343 yyval
= yystack
.valueAt (yylen
- 1);
345 yyval
= yystack
.valueAt (0);
347 yy_reduce_print (yyn
, yystack
);
355 yy_symbol_print ("-> $$ =", yyr1_
[yyn
], yyval
]b4_locations_if([, yyloc
])[);
360 /* Shift the result of the reduction. */
362 int yystate
= yypgoto_
[yyn
- yyntokens_
] + yystack
.stateAt (0);
363 if (0 <= yystate
&& yystate
<= yylast_
364 && yycheck_
[yystate
] == yystack
.stateAt (0))
365 yystate
= yytable_
[yystate
];
367 yystate
= yydefgoto_
[yyn
- yyntokens_
];
369 yystack
.push (yystate
, yyval
]b4_locations_if([, yyloc
])[);
373 /* Return YYSTR after stripping away unnecessary quotes and
374 backslashes, so that it's suitable for yyerror. The heuristic is
375 that double-quoting is unnecessary unless the string contains an
376 apostrophe, a comma, or backslash (other than backslash-backslash).
377 YYSTR is taken from yytname. */
378 private final String
yytnamerr_ (String yystr
)
380 if (yystr
.charAt (0) == '"')
382 StringBuffer yyr
= new StringBuffer ();
383 strip_quotes
: for (int i
= 1; i
< yystr
.length (); i
++)
384 switch (yystr
.charAt (i
))
391 if (yystr
.charAt(++i
) != '\\')
395 yyr
.append (yystr
.charAt (i
));
399 return yyr
.toString ();
402 else if (yystr
.equals ("$end"))
403 return "end of input";
408 /*--------------------------------.
409 | Print this symbol on YYOUTPUT. |
410 `--------------------------------*/
412 private void yy_symbol_print (String s
, int yytype
,
413 ]b4_union_name
[ yyvaluep
]dnl
414 b4_locations_if([, Object yylocationp
])[)
417 yycdebug (s
+ (yytype
< yyntokens_ ?
" token " : " nterm ")
418 + yytname_
[yytype
] + " ("]b4_locations_if([
419 + yylocationp
+ ": "])[
420 + (yyvaluep
== null ?
"(null)" : yyvaluep
) + ")");
424 * Parse input from the scanner that was specified at object construction
425 * time. Return whether the end of the input was reached successfully.
427 * @@return <tt>true</tt> if the parsing succeeds. Note that this does not
428 * imply that there were no syntax errors.
430 public boolean parse () ]b4_maybe_throws([b4_throws
])[
432 /// Lookahead and lookahead in internal form.
433 int yychar
= yyempty_
;
441 Stack yystack
= new Stack ();
443 /* Error handling. */
445 ]b4_locations_if([/// The location where the error started.
446 ]b4_location_type
[ yyerrloc
= null;
448 /// ]b4_location_type[ of the lookahead.
449 ]b4_location_type
[ yylloc
= new ]b4_location_type
[ (null, null);
452 ]b4_location_type
[ yyloc
;])
454 /// Semantic value of the lookahead.
455 b4_union_name
[ yylval
= null;
459 yycdebug ("Starting parse\n");
462 ]m4_ifdef([b4_initial_action
], [
463 m4_pushdef([b4_at_dollar
], [yylloc
])dnl
464 m4_pushdef([b4_dollar_dollar
], [yylval
])dnl
465 /* User initialization code. */
466 b4_user_initial_action
467 m4_popdef([b4_dollar_dollar
])dnl
468 m4_popdef([b4_at_dollar
])])dnl
470 [ /* Initialize the stack. */
471 yystack
.push (yystate
, yylval
]b4_locations_if([, yylloc
])[);
473 int label
= YYNEWSTATE
;
477 /* New state. Unlike in the C/C++ skeletons, the state is already
478 pushed when we come here. */
480 yycdebug ("Entering state " + yystate
+ "\n");
482 yystack
.print (debugStream
);
485 if (yystate
== yyfinal_
)
488 /* Take a decision. First try without lookahead. */
489 yyn
= yypact_
[yystate
];
490 if (yyn
== yypact_ninf_
)
496 /* Read a lookahead token. */
497 if (yychar
== yyempty_
)
499 yycdebug ("Reading a token: ");
500 yychar
= yylex (]b4_lex_param_call
[);]
502 b4_pure_if([yylloc
= new ]b4_location_type
[(yylex
.getStartPos (),
503 yylex
.getEndPos ());],
504 [yylloc
= new ]b4_location_type
[(this.yystartpos
,
506 b4_pure_if([yylval
= yylex
.getLVal ()], [yylval
= this.yylval
]);[
509 /* Convert token to internal form. */
512 yychar
= yytoken
= EOF
;
513 yycdebug ("Now at end of input.\n");
517 yytoken
= yytranslate_ (yychar
);
518 yy_symbol_print ("Next token is", yytoken
,
519 yylval
]b4_locations_if([, yylloc
])[);
522 /* If the proper action on seeing token YYTOKEN is to reduce or to
523 detect an error, take that action. */
525 if (yyn
< 0 || yylast_
< yyn
|| yycheck_
[yyn
] != yytoken
)
528 /* <= 0 means reduce or error. */
529 else if ((yyn
= yytable_
[yyn
]) <= 0)
531 if (yyn
== 0 || yyn
== yytable_ninf_
)
542 /* Shift the lookahead token. */
543 yy_symbol_print ("Shifting", yytoken
,
544 yylval
]b4_locations_if([, yylloc
])[);
546 /* Discard the token being shifted. */
549 /* Count tokens shifted since error; after three, turn off error
551 if (yyerrstatus_
> 0)
555 yystack
.push (yystate
, yylval
]b4_locations_if([, yylloc
])[);
560 /*-----------------------------------------------------------.
561 | yydefault -- do the default action for the current state. |
562 `-----------------------------------------------------------*/
564 yyn
= yydefact_
[yystate
];
571 /*-----------------------------.
572 | yyreduce -- Do a reduction. |
573 `-----------------------------*/
576 label
= yyaction (yyn
, yystack
, yylen
);
577 yystate
= yystack
.stateAt (0);
580 /*------------------------------------.
581 | yyerrlab -- here on detecting error |
582 `------------------------------------*/
584 /* If not already recovering from an error, report this error. */
585 if (yyerrstatus_
== 0)
588 yyerror (]b4_locations_if([yylloc
, ])[yysyntax_error (yystate
, yytoken
));
591 ]b4_locations_if([yyerrloc
= yylloc
;])[
592 if (yyerrstatus_
== 3)
594 /* If just tried and failed to reuse lookahead token after an
595 error, discard it. */
599 /* Return failure if at end of input. */
607 /* Else will try to reuse lookahead token after shifting the error
612 /*---------------------------------------------------.
613 | errorlab -- error raised explicitly by YYERROR. |
614 `---------------------------------------------------*/
617 ]b4_locations_if([yyerrloc
= yystack
.locationAt (yylen
- 1);])[
618 /* Do not reclaim the symbols of the rule which action triggered
622 yystate
= yystack
.stateAt (0);
626 /*-------------------------------------------------------------.
627 | yyerrlab1 -- common code for both syntax error and YYERROR. |
628 `-------------------------------------------------------------*/
630 yyerrstatus_
= 3; /* Each real token shifted decrements this. */
634 yyn
= yypact_
[yystate
];
635 if (yyn
!= yypact_ninf_
)
638 if (0 <= yyn
&& yyn
<= yylast_
&& yycheck_
[yyn
] == yyterror_
)
646 /* Pop the current state because it cannot handle the error token. */
647 if (yystack
.height
== 1)
650 ]b4_locations_if([yyerrloc
= yystack
.locationAt (0);])[
652 yystate
= yystack
.stateAt (0);
654 yystack
.print (debugStream
);
658 /* Muck with the stack to setup for yylloc. */
659 yystack
.push (0, null, yylloc
);
660 yystack
.push (0, null, yyerrloc
);
661 yyloc
= yylloc (yystack
, 2);
664 /* Shift the error token. */
665 yy_symbol_print ("Shifting", yystos_
[yyn
],
666 yylval
]b4_locations_if([, yyloc
])[);
669 yystack
.push (yyn
, yylval
]b4_locations_if([, yyloc
])[);
683 // Generate an error message.
684 private String
yysyntax_error (int yystate
, int tok
)
688 int yyn
= yypact_
[yystate
];
689 if (yypact_ninf_
< yyn
&& yyn
<= yylast_
)
693 /* Start YYX at -YYN if negative to avoid negative indexes in
695 int yyxbegin
= yyn
< 0 ?
-yyn
: 0;
697 /* Stay within bounds of both yycheck and yytname. */
698 int yychecklim
= yylast_
- yyn
+ 1;
699 int yyxend
= yychecklim
< yyntokens_ ? yychecklim
: yyntokens_
;
701 for (int x
= yyxbegin
; x
< yyxend
; ++x
)
702 if (yycheck_
[x
+ yyn
] == x
&& x
!= yyterror_
)
705 // FIXME: This method of building the message is not compatible
706 // with internationalization.
707 res
= new StringBuffer ("syntax error, unexpected ");
708 res
.append (yytnamerr_ (yytname_
[tok
]));
712 for (int x
= yyxbegin
; x
< yyxend
; ++x
)
713 if (yycheck_
[x
+ yyn
] == x
&& x
!= yyterror_
)
715 res
.append (count
++ == 0 ?
", expecting " : " or ");
716 res
.append (yytnamerr_ (yytname_
[x
]));
719 return res
.toString ();
723 return "syntax error";
727 /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
729 private static final ]b4_int_type_for([b4_pact
])[ yypact_ninf_
= ]b4_pact_ninf
[;
730 private static final ]b4_int_type_for([b4_pact
])[ yypact_
[] =
735 /* YYDEFACT[S] -- default rule to reduce with in state S when YYTABLE
736 doesn't specify something else to do. Zero means the default is an
738 private static final ]b4_int_type_for([b4_defact
])[ yydefact_
[] =
743 /* YYPGOTO[NTERM-NUM]. */
744 private static final ]b4_int_type_for([b4_pgoto
])[ yypgoto_
[] =
749 /* YYDEFGOTO[NTERM-NUM]. */
750 private static final ]b4_int_type_for([b4_defgoto
])[
756 /* YYTABLE[YYPACT[STATE-NUM]]. What to do in state STATE-NUM. If
757 positive, shift that token. If negative, reduce the rule which
758 number is the opposite. If zero, do what YYDEFACT says. */
759 private static final ]b4_int_type_for([b4_table
])[ yytable_ninf_
= ]b4_table_ninf
[;
760 private static final ]b4_int_type_for([b4_table
])[
767 private static final ]b4_int_type_for([b4_check
])[
773 /* STOS_[STATE-NUM] -- The (internal number of the) accessing
774 symbol of state STATE-NUM. */
775 private static final ]b4_int_type_for([b4_stos
])[
781 /* TOKEN_NUMBER_[YYLEX-NUM] -- Internal symbol number corresponding
783 private static final ]b4_int_type_for([b4_toknum
])[
789 /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives. */
790 private static final ]b4_int_type_for([b4_r1
])[
796 /* YYR2[YYN] -- Number of symbols composing right hand side of rule YYN. */
797 private static final ]b4_int_type_for([b4_r2
])[
803 /* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
804 First, the terminals, then, starting at \a yyntokens_, nonterminals. */
805 private static final String yytname_
[] =
810 /* YYRHS -- A `-1'-separated list of the rules' RHS. */
811 private static final ]b4_int_type_for([b4_rhs
])[ yyrhs_
[] =
816 /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in
818 private static final ]b4_int_type_for([b4_prhs
])[ yyprhs_
[] =
823 /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
824 private static final ]b4_int_type_for([b4_rline
])[ yyrline_
[] =
829 // Report on the debug stream that the rule yyrule is going to be reduced.
830 private void yy_reduce_print (int yyrule
, Stack yystack
)
835 int yylno
= yyrline_
[yyrule
];
836 int yynrhs
= yyr2_
[yyrule
];
837 /* Print the symbols being reduced, and their result. */
838 yycdebug ("Reducing stack by rule " + (yyrule
- 1)
839 + " (line " + yylno
+ "), ");
841 /* The symbols being reduced. */
842 for (int yyi
= 0; yyi
< yynrhs
; yyi
++)
843 yy_symbol_print (" $" + (yyi
+ 1) + " =",
844 yyrhs_
[yyprhs_
[yyrule
] + yyi
],
845 ]b4_rhs_value(yynrhs
, yyi
+ 1)b4_locations_if([,
846 b4_rhs_location(yynrhs
, yyi
+ 1)])[);
849 /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */
850 private static final ]b4_int_type_for([b4_translate
])[ yytranslate_table_
[] =
855 private static final ]b4_int_type_for([b4_translate
])[ yytranslate_ (int t
)
857 if (t
>= 0 && t
<= yyuser_token_number_max_
)
858 return yytranslate_table_
[t
];
860 return yyundef_token_
;
863 private static final int yylast_
= ]b4_last
[;
864 private static final int yynnts_
= ]b4_nterms_number
[;
865 private static final int yyempty_
= -2;
866 private static final int yyfinal_
= ]b4_final_state_number
[;
867 private static final int yyterror_
= 1;
868 private static final int yyerrcode_
= 256;
869 private static final int yyntokens_
= ]b4_tokens_number
[;
871 private static final int yyuser_token_number_max_
= ]b4_user_token_number_max
[;
872 private static final int yyundef_token_
= ]b4_undef_token_number
[;
874 ]/* User implementation code. */
875 b4_percent_code_get
[]dnl