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 2, or (at your option)
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 AT_BANNER([[Java Calculator.]])
23 # ------------------------- #
24 # Helping Autotest macros. #
25 # ------------------------- #
28 # _AT_DATA_JAVA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES])
29 # ----------------------------------------------------------------------
30 # Produce `calc.y'. Don't call this macro directly, because it contains
31 # some occurrences of `$1' etc. which will be interpreted by m4. So
32 # you should call it with $1, $2, and $3 as arguments, which is what
33 # AT_DATA_JAVA_CALC_Y does.
34 m4_define([_AT_DATA_JAVA_CALC_Y],
35 [m4_if([$1$2$3], $[1]$[2]$[3], [],
36 [m4_fatal([$0: Invalid arguments: $@])])dnl
38 [[/* Infix notation calculator--calc */
41 %define parser_class_name "Calc"
47 import java.io.StreamTokenizer;
48 import java.io.InputStream;
49 import java.io.InputStreamReader;
50 import java.io.Reader;
51 import java.io.IOException;
54 /* Bison Declarations */
55 %token <Integer> NUM "number"
58 %nonassoc '=' /* comparison */
61 %left NEG /* negation--unary minus */
62 %right '^' /* exponentiation */
81 if ($1.intValue () != $3.intValue ())
82 yyerror ("calc: error: " + $1 + " != " + $3);
84 | exp '+' exp { $$ = new Integer ($1.intValue () + $3.intValue ()); }
85 | exp '-' exp { $$ = new Integer ($1.intValue () - $3.intValue ()); }
86 | exp '*' exp { $$ = new Integer ($1.intValue () * $3.intValue ()); }
87 | exp '/' exp { $$ = new Integer ($1.intValue () / $3.intValue ()); }
88 | '-' exp %prec NEG { $$ = new Integer (-$2.intValue ()); }
89 | exp '^' exp { $$ = new Integer ((int)
90 Math.pow ($1.intValue (),
92 | '(' exp ')' { $$ = $2; }
93 | '(' error ')' { $$ = new Integer (1111); }
94 | '!' { $$ = new Integer (0); return YYERROR; }
95 | '-' error { $$ = new Integer (0); return YYERROR; }
103 class CalcLexer implements Calc.Lexer {
107 public ]AT_LEXPARAM_IF([[YYLexer]], [[CalcLexer]]) (InputStream is)
109 st = new StreamTokenizer (new InputStreamReader (is));
111 st.eolIsSignificant (true);
112 st.whitespaceChars (9, 9);
113 st.whitespaceChars (32, 32);
114 st.wordChars (48, 57);
119 Position yyendpos = new Position (1);
121 public Position getStartPos() {
125 public Position getEndPos() {
129 public void yyerror (Calc.Location l, String s)
132 System.err.println (s);
134 System.err.println (l.begin + ": " + s);
137 public void yyerror (String s)
139 System.err.println (s);
145 public Object getLVal() {
149 public int yylex () throws IOException {
150 int ttype = st.nextToken ();
151 ]AT_LOCATION_IF([[yystartpos = yyendpos;]])[
152 if (ttype == st.TT_EOF)
155 else if (ttype == st.TT_EOL)
157 ]AT_LOCATION_IF([[yyendpos = new Position (yyendpos.lineno () + 1);]])[
161 else if (ttype == st.TT_WORD)
163 yylval = new Integer (st.sval);
186 public Position (int l)
191 public long getHashCode ()
196 public boolean equals (Position l)
198 return l.line == line;
201 public String toString ()
203 return Integer.toString (line);
213 ])# _AT_DATA_JAVA_CALC_Y
216 # AT_DATA_CALC_Y([BISON-OPTIONS])
217 # -------------------------------------------------
219 m4_define([AT_DATA_JAVA_CALC_Y],
220 [_AT_DATA_JAVA_CALC_Y($[1], $[2], $[3], [$1])
225 # AT_JAVA_COMPILE(SOURCE)
226 # -----------------------
227 # Compile SOURCES into Java class files. Skip the test if java or javac is
229 m4_define([AT_JAVA_COMPILE],
230 [AT_CHECK([test -n "$CONF_JAVA" || exit 77
231 test -n "$CONF_JAVAC" || exit 77])
232 AT_CHECK([$SHELL ../../../javacomp.sh $1],
233 0, [ignore], [ignore])])
236 # AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
237 # -----------------------------------------------------------------
238 m4_define([AT_JAVA_PARSER_CHECK],
239 [AT_CHECK([$5 $SHELL ../../../javaexec.sh $1], [$2], [$3], [$4])])
242 # _AT_CHECK_JAVA_CALC_ERROR(BISON-OPTIONS, INPUT,
243 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
244 # ---------------------------------------------------------
245 # Run `calc' on INPUT, and expect a `syntax error' message.
247 # If INPUT starts with a slash, it is used as absolute input file name,
248 # otherwise as contents.
250 # The VERBOSE-AND-LOCATED-ERROR-MESSAGE is stripped of locations
251 # and expected tokens if necessary, and compared with the output.
252 m4_define([_AT_CHECK_JAVA_CALC_ERROR],
253 [m4_bmatch([$2], [^/],
254 [AT_JAVA_PARSER_CHECK([Calc < $2], 0, [], [stderr])],
258 AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])])
260 # Normalize the observed and expected error messages, depending upon the
262 # 1. Create the reference error message.
266 # 2. If locations are not used, remove them.
267 AT_YYERROR_SEES_LOC_IF([],
268 [[sed 's/^[-0-9.]*: //' expout >at-expout
269 mv at-expout expout]])
270 # 3. If error-verbose is not used, strip the`, unexpected....' part.
271 m4_bmatch([$1], [%error-verbose], [],
272 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
273 mv at-expout expout]])
275 AT_CHECK([cat stderr], 0, [expout])
278 # _AT_CHECK_JAVA_CALC([BISON-DIRECTIVES], [BISON-CODE])
279 # -----------------------------------------------------------------------
280 # Start a testing chunk which compiles `calc' grammar with
281 # BISON-DIRECTIVES, and performs several tests over the parser.
282 m4_define([_AT_CHECK_JAVA_CALC],
283 [# We use integers to avoid dependencies upon the precision of doubles.
284 AT_SETUP([Calculator $1])
286 AT_BISON_OPTION_PUSHDEFS([$1])
288 AT_DATA_JAVA_CALC_Y([$1
293 AT_CHECK([bison -o Calc.java Calc.y])
294 AT_JAVA_COMPILE([Calc.java])
296 # Test the priorities.
312 AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])
315 # Some syntax errors.
316 _AT_CHECK_JAVA_CALC_ERROR([$1], [0 0],
317 [1: syntax error, unexpected number])
318 _AT_CHECK_JAVA_CALC_ERROR([$1], [1//2],
319 [1: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
320 _AT_CHECK_JAVA_CALC_ERROR([$1], [error],
321 [1: syntax error, unexpected $undefined])
322 _AT_CHECK_JAVA_CALC_ERROR([$1], [1 = 2 = 3],
323 [1: syntax error, unexpected '='])
324 _AT_CHECK_JAVA_CALC_ERROR([$1], [
326 [2: syntax error, unexpected '+'])
327 # Exercise error messages with EOF: work on an empty file.
328 _AT_CHECK_JAVA_CALC_ERROR([$1], [/dev/null],
329 [1: syntax error, unexpected end of input])
331 # Exercise the error token: without it, we die at the first error,
334 # - have several errors which exercise different shift/discardings
335 # - (): nothing to pop, nothing to discard
336 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
337 # - (* * *): nothing to pop, a lot to discard
338 # - (1 + 2 * *): some to pop and discard
340 # - test the action associated to `error'
342 # - check the lookahead that triggers an error is not discarded
343 # when we enter error recovery. Below, the lookahead causing the
344 # first error is ")", which is needed to recover from the error and
345 # produce the "0" that triggers the "0 != 1" error.
347 _AT_CHECK_JAVA_CALC_ERROR([$1],
348 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
349 [1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
350 1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
351 1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
352 1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
353 calc: error: 4444 != 1])
355 # The same, but this time exercising explicitly triggered syntax errors.
356 # POSIX says the lookahead causing the error should not be discarded.
357 _AT_CHECK_JAVA_CALC_ERROR([$1], [(!) + (0 0) = 1],
358 [1: syntax error, unexpected number
359 calc: error: 2222 != 1])
360 _AT_CHECK_JAVA_CALC_ERROR([$1], [(- *) + (0 0) = 1],
361 [1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
362 1: syntax error, unexpected number
363 calc: error: 2222 != 1])
364 AT_BISON_OPTION_POPDEFS
367 ])# _AT_CHECK_JAVA_CALC
370 # AT_CHECK_JAVA_CALC([BISON-DIRECTIVES])
371 # --------------------------------------------------------
372 # Start a testing chunk which compiles `calc' grammar with
373 # BISON-DIRECTIVES, and performs several tests over the parser.
374 # Run the test with and without %error-verbose.
375 m4_define([AT_CHECK_JAVA_CALC],
376 [_AT_CHECK_JAVA_CALC([$1], [$2])
377 _AT_CHECK_JAVA_CALC([%error-verbose $1], [$2])
378 _AT_CHECK_JAVA_CALC([%locations $1], [$2])
379 _AT_CHECK_JAVA_CALC([%error-verbose %locations $1], [$2])
380 ])# AT_CHECK_JAVA_CALC
383 # ------------------------ #
384 # Simple LALR Calculator. #
385 # ------------------------ #
387 AT_CHECK_JAVA_CALC([], [[
388 public static void main (String args[]) throws IOException
390 CalcLexer l = new CalcLexer (System.in);
391 Calc p = new Calc (l);
396 AT_CHECK_JAVA_CALC([%lex-param { InputStream is } ], [[
397 public static void main (String args[]) throws IOException
399 new Calc (System.in).parse ();