1 # Java tests for simple calculator. -*- Autotest -*-
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 AT_BANNER([[Java Calculator.]])
21 # ------------------------- #
22 # Helping Autotest macros. #
23 # ------------------------- #
26 # _AT_DATA_JAVA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES])
27 # ----------------------------------------------------------------------
28 # Produce `calc.y'. Don't call this macro directly, because it contains
29 # some occurrences of `$1' etc. which will be interpreted by m4. So
30 # you should call it with $1, $2, and $3 as arguments, which is what
31 # AT_DATA_JAVA_CALC_Y does.
32 m4_define([_AT_DATA_JAVA_CALC_Y],
33 [m4_if([$1$2$3], $[1]$[2]$[3], [],
34 [m4_fatal([$0: Invalid arguments: $@])])dnl
36 [[/* Infix notation calculator--calc */
39 %define parser_class_name "Calc"
45 import java.io.StreamTokenizer;
46 import java.io.InputStream;
47 import java.io.InputStreamReader;
48 import java.io.Reader;
49 import java.io.IOException;
52 /* Bison Declarations */
53 %token <Integer> NUM "number"
56 %nonassoc '=' /* comparison */
59 %left NEG /* negation--unary minus */
60 %right '^' /* exponentiation */
79 if ($1.intValue () != $3.intValue ())
80 yyerror ("calc: error: " + $1 + " != " + $3);
82 | exp '+' exp { $$ = new Integer ($1.intValue () + $3.intValue ()); }
83 | exp '-' exp { $$ = new Integer ($1.intValue () - $3.intValue ()); }
84 | exp '*' exp { $$ = new Integer ($1.intValue () * $3.intValue ()); }
85 | exp '/' exp { $$ = new Integer ($1.intValue () / $3.intValue ()); }
86 | '-' exp %prec NEG { $$ = new Integer (-$2.intValue ()); }
87 | exp '^' exp { $$ = new Integer ((int)
88 Math.pow ($1.intValue (),
90 | '(' exp ')' { $$ = $2; }
91 | '(' error ')' { $$ = new Integer (1111); }
92 | '!' { $$ = new Integer (0); return YYERROR; }
93 | '-' error { $$ = new Integer (0); return YYERROR; }
101 class CalcLexer implements Calc.Lexer {
105 public ]AT_LEXPARAM_IF([[YYLexer]], [[CalcLexer]]) (InputStream is)
107 st = new StreamTokenizer (new InputStreamReader (is));
109 st.eolIsSignificant (true);
110 st.whitespaceChars (9, 9);
111 st.whitespaceChars (32, 32);
112 st.wordChars (48, 57);
117 Position yyendpos = new Position (1);
119 public Position getStartPos() {
123 public Position getEndPos() {
127 public void yyerror (Calc.Location l, String s)
130 System.err.println (s);
132 System.err.println (l.begin + ": " + s);
135 public void yyerror (String s)
137 System.err.println (s);
143 public Object getLVal() {
147 public int yylex () throws IOException {
148 int ttype = st.nextToken ();
149 ]AT_LOCATION_IF([[yystartpos = yyendpos;]])[
150 if (ttype == st.TT_EOF)
153 else if (ttype == st.TT_EOL)
155 ]AT_LOCATION_IF([[yyendpos = new Position (yyendpos.lineno () + 1);]])[
159 else if (ttype == st.TT_WORD)
161 yylval = new Integer (st.sval);
184 public Position (int l)
189 public long getHashCode ()
194 public boolean equals (Position l)
196 return l.line == line;
199 public String toString ()
201 return Integer.toString (line);
211 ])# _AT_DATA_JAVA_CALC_Y
214 # AT_DATA_CALC_Y([BISON-OPTIONS])
215 # -------------------------------------------------
217 m4_define([AT_DATA_JAVA_CALC_Y],
218 [_AT_DATA_JAVA_CALC_Y($[1], $[2], $[3], [$1])
223 # AT_JAVA_COMPILE(SOURCE)
224 # -----------------------
225 # Compile SOURCES into Java class files. Skip the test if java or javac is
227 m4_define([AT_JAVA_COMPILE],
228 [AT_CHECK([test -n "$CONF_JAVA" || exit 77
229 test -n "$CONF_JAVAC" || exit 77])
230 AT_CHECK([$SHELL ../../../javacomp.sh $1],
231 0, [ignore], [ignore])])
234 # AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
235 # -----------------------------------------------------------------
236 m4_define([AT_JAVA_PARSER_CHECK],
237 [AT_CHECK([$5 $SHELL ../../../javaexec.sh $1], [$2], [$3], [$4])])
240 # _AT_CHECK_JAVA_CALC_ERROR(BISON-OPTIONS, INPUT,
241 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
242 # ---------------------------------------------------------
243 # Run `calc' on INPUT, and expect a `syntax error' message.
245 # If INPUT starts with a slash, it is used as absolute input file name,
246 # otherwise as contents.
248 # The VERBOSE-AND-LOCATED-ERROR-MESSAGE is stripped of locations
249 # and expected tokens if necessary, and compared with the output.
250 m4_define([_AT_CHECK_JAVA_CALC_ERROR],
251 [m4_bmatch([$2], [^/],
252 [AT_JAVA_PARSER_CHECK([Calc < $2], 0, [], [stderr])],
256 AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])])
258 # Normalize the observed and expected error messages, depending upon the
260 # 1. Create the reference error message.
264 # 2. If locations are not used, remove them.
265 AT_YYERROR_SEES_LOC_IF([],
266 [[sed 's/^[-0-9.]*: //' expout >at-expout
267 mv at-expout expout]])
268 # 3. If error-verbose is not used, strip the`, unexpected....' part.
269 m4_bmatch([$1], [%error-verbose], [],
270 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
271 mv at-expout expout]])
273 AT_CHECK([cat stderr], 0, [expout])
276 # _AT_CHECK_JAVA_CALC([BISON-DIRECTIVES], [BISON-CODE])
277 # -----------------------------------------------------------------------
278 # Start a testing chunk which compiles `calc' grammar with
279 # BISON-DIRECTIVES, and performs several tests over the parser.
280 m4_define([_AT_CHECK_JAVA_CALC],
281 [# We use integers to avoid dependencies upon the precision of doubles.
282 AT_SETUP([Calculator $1])
284 AT_BISON_OPTION_PUSHDEFS([$1])
286 AT_DATA_JAVA_CALC_Y([$1
291 AT_CHECK([bison -o Calc.java Calc.y])
292 AT_JAVA_COMPILE([Calc.java])
294 # Test the priorities.
310 AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])
313 # Some syntax errors.
314 _AT_CHECK_JAVA_CALC_ERROR([$1], [0 0],
315 [1: syntax error, unexpected number])
316 _AT_CHECK_JAVA_CALC_ERROR([$1], [1//2],
317 [1: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
318 _AT_CHECK_JAVA_CALC_ERROR([$1], [error],
319 [1: syntax error, unexpected $undefined])
320 _AT_CHECK_JAVA_CALC_ERROR([$1], [1 = 2 = 3],
321 [1: syntax error, unexpected '='])
322 _AT_CHECK_JAVA_CALC_ERROR([$1], [
324 [2: syntax error, unexpected '+'])
325 # Exercise error messages with EOF: work on an empty file.
326 _AT_CHECK_JAVA_CALC_ERROR([$1], [/dev/null],
327 [1: syntax error, unexpected end of input])
329 # Exercise the error token: without it, we die at the first error,
332 # - have several errors which exercise different shift/discardings
333 # - (): nothing to pop, nothing to discard
334 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
335 # - (* * *): nothing to pop, a lot to discard
336 # - (1 + 2 * *): some to pop and discard
338 # - test the action associated to `error'
340 # - check the lookahead that triggers an error is not discarded
341 # when we enter error recovery. Below, the lookahead causing the
342 # first error is ")", which is needed to recover from the error and
343 # produce the "0" that triggers the "0 != 1" error.
345 _AT_CHECK_JAVA_CALC_ERROR([$1],
346 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
347 [1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
348 1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
349 1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
350 1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
351 calc: error: 4444 != 1])
353 # The same, but this time exercising explicitly triggered syntax errors.
354 # POSIX says the lookahead causing the error should not be discarded.
355 _AT_CHECK_JAVA_CALC_ERROR([$1], [(!) + (0 0) = 1],
356 [1: syntax error, unexpected number
357 calc: error: 2222 != 1])
358 _AT_CHECK_JAVA_CALC_ERROR([$1], [(- *) + (0 0) = 1],
359 [1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
360 1: syntax error, unexpected number
361 calc: error: 2222 != 1])
362 AT_BISON_OPTION_POPDEFS
365 ])# _AT_CHECK_JAVA_CALC
368 # AT_CHECK_JAVA_CALC([BISON-DIRECTIVES])
369 # --------------------------------------------------------
370 # Start a testing chunk which compiles `calc' grammar with
371 # BISON-DIRECTIVES, and performs several tests over the parser.
372 # Run the test with and without %error-verbose.
373 m4_define([AT_CHECK_JAVA_CALC],
374 [_AT_CHECK_JAVA_CALC([$1], [$2])
375 _AT_CHECK_JAVA_CALC([%error-verbose $1], [$2])
376 _AT_CHECK_JAVA_CALC([%locations $1], [$2])
377 _AT_CHECK_JAVA_CALC([%error-verbose %locations $1], [$2])
378 ])# AT_CHECK_JAVA_CALC
381 # ------------------------ #
382 # Simple LALR Calculator. #
383 # ------------------------ #
385 AT_CHECK_JAVA_CALC([], [[
386 public static void main (String args[]) throws IOException
388 CalcLexer l = new CalcLexer (System.in);
389 Calc p = new Calc (l);
394 AT_CHECK_JAVA_CALC([%lex-param { InputStream is } ], [[
395 public static void main (String args[]) throws IOException
397 new Calc (System.in).parse ();