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], [BISON-EPILOGUE])
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"
46 import java.io.StreamTokenizer;
47 import java.io.InputStream;
48 import java.io.InputStreamReader;
49 import java.io.Reader;
50 import java.io.IOException;
53 /* Bison Declarations */
54 %token <Integer> NUM "number"
57 %nonassoc '=' /* comparison */
60 %left NEG /* negation--unary minus */
61 %right '^' /* exponentiation */
80 if ($1.intValue () != $3.intValue ())
81 yyerror ("calc: error: " + $1 + " != " + $3);
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 '/' exp { $$ = new Integer ($1.intValue () / $3.intValue ()); }
87 | '-' exp %prec NEG { $$ = new Integer (-$2.intValue ()); }
88 | exp '^' exp { $$ = new Integer ((int)
89 Math.pow ($1.intValue (),
91 | '(' exp ')' { $$ = $2; }
92 | '(' error ')' { $$ = new Integer (1111); }
93 | '!' { $$ = new Integer (0); return YYERROR; }
94 | '-' error { $$ = new Integer (0); return YYERROR; }
106 public Position (int l)
111 public long getHashCode ()
116 public boolean equals (Position l)
118 return l.line == line;
121 public String toString ()
123 return Integer.toString (line);
134 ])# _AT_DATA_JAVA_CALC_Y
137 # AT_DATA_CALC_Y([BISON-OPTIONS], [BISON-EPILOGUE])
138 # -------------------------------------------------
140 m4_define([AT_DATA_JAVA_CALC_Y],
141 [_AT_DATA_JAVA_CALC_Y($[1], $[2], $[3], [$1], [$2])
146 # AT_JAVA_COMPILE(SOURCE)
147 # -----------------------
148 # Compile SOURCES into Java class files. Skip the test if java or javac is
150 m4_define([AT_JAVA_COMPILE],
151 [AT_CHECK([test -n "$CONF_JAVA" || exit 77
152 test -n "$CONF_JAVAC" || exit 77])
153 AT_CHECK([$SHELL ../../../javacomp.sh $1],
154 0, [ignore], [ignore])])
157 # AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
158 # -----------------------------------------------------------------
159 m4_define([AT_JAVA_PARSER_CHECK],
160 [AT_CHECK([$5 $SHELL ../../../javaexec.sh $1], [$2], [$3], [$4])])
163 # _AT_CHECK_JAVA_CALC_ERROR(BISON-OPTIONS, INPUT,
164 # [VERBOSE-AND-LOCATED-ERROR-MESSAGE])
165 # ---------------------------------------------------------
166 # Run `calc' on INPUT, and expect a `syntax error' message.
168 # If INPUT starts with a slash, it is used as absolute input file name,
169 # otherwise as contents.
171 # The VERBOSE-AND-LOCATED-ERROR-MESSAGE is stripped of locations
172 # and expected tokens if necessary, and compared with the output.
173 m4_define([_AT_CHECK_JAVA_CALC_ERROR],
174 [m4_bmatch([$2], [^/],
175 [AT_JAVA_PARSER_CHECK([Calc < $2], 0, [], [stderr])],
179 AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])])
181 # Normalize the observed and expected error messages, depending upon the
183 # 1. Create the reference error message.
187 # 2. If locations are not used, remove them.
188 AT_YYERROR_SEES_LOC_IF([],
189 [[sed 's/^[-0-9.]*: //' expout >at-expout
190 mv at-expout expout]])
191 # 3. If error-verbose is not used, strip the`, unexpected....' part.
192 m4_bmatch([$1], [%error-verbose], [],
193 [[sed 's/syntax error, .*$/syntax error/' expout >at-expout
194 mv at-expout expout]])
196 AT_CHECK([cat stderr], 0, [expout])
199 # _AT_CHECK_JAVA_CALC([BISON-DIRECTIVES], [BISON-CODE], [BISON-EPILOGUE])
200 # -----------------------------------------------------------------------
201 # Start a testing chunk which compiles `calc' grammar with
202 # BISON-DIRECTIVES, and performs several tests over the parser.
203 m4_define([_AT_CHECK_JAVA_CALC],
204 [# We use integers to avoid dependencies upon the precision of doubles.
205 AT_SETUP([Calculator $1])
207 AT_BISON_OPTION_PUSHDEFS([$1])
209 AT_DATA_JAVA_CALC_Y([$1
214 AT_CHECK([bison -o Calc.java Calc.y])
215 AT_JAVA_COMPILE([Calc.java])
217 # Test the priorities.
233 AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])
236 # Some syntax errors.
237 _AT_CHECK_JAVA_CALC_ERROR([$1], [0 0],
238 [1: syntax error, unexpected number])
239 _AT_CHECK_JAVA_CALC_ERROR([$1], [1//2],
240 [1: syntax error, unexpected '/', expecting number or '-' or '(' or '!'])
241 _AT_CHECK_JAVA_CALC_ERROR([$1], [error],
242 [1: syntax error, unexpected $undefined])
243 _AT_CHECK_JAVA_CALC_ERROR([$1], [1 = 2 = 3],
244 [1: syntax error, unexpected '='])
245 _AT_CHECK_JAVA_CALC_ERROR([$1], [
247 [2: syntax error, unexpected '+'])
248 # Exercise error messages with EOF: work on an empty file.
249 _AT_CHECK_JAVA_CALC_ERROR([$1], [/dev/null],
250 [1: syntax error, unexpected end of input])
252 # Exercise the error token: without it, we die at the first error,
255 # - have several errors which exercise different shift/discardings
256 # - (): nothing to pop, nothing to discard
257 # - (1 + 1 + 1 +): a lot to pop, nothing to discard
258 # - (* * *): nothing to pop, a lot to discard
259 # - (1 + 2 * *): some to pop and discard
261 # - test the action associated to `error'
263 # - check the lookahead that triggers an error is not discarded
264 # when we enter error recovery. Below, the lookahead causing the
265 # first error is ")", which is needed to recover from the error and
266 # produce the "0" that triggers the "0 != 1" error.
268 _AT_CHECK_JAVA_CALC_ERROR([$1],
269 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
270 [1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
271 1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
272 1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
273 1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
274 calc: error: 4444 != 1])
276 # The same, but this time exercising explicitly triggered syntax errors.
277 # POSIX says the lookahead causing the error should not be discarded.
278 _AT_CHECK_JAVA_CALC_ERROR([$1], [(!) + (0 0) = 1],
279 [1: syntax error, unexpected number
280 calc: error: 2222 != 1])
281 _AT_CHECK_JAVA_CALC_ERROR([$1], [(- *) + (0 0) = 1],
282 [1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
283 1: syntax error, unexpected number
284 calc: error: 2222 != 1])
285 AT_BISON_OPTION_POPDEFS
288 ])# _AT_CHECK_JAVA_CALC
291 # AT_CHECK_JAVA_CALC([BISON-DIRECTIVES], [BISON-EPILOGUE])
292 # --------------------------------------------------------
293 # Start a testing chunk which compiles `calc' grammar with
294 # BISON-DIRECTIVES, and performs several tests over the parser.
295 # Run the test with and without %error-verbose.
296 m4_define([AT_CHECK_JAVA_CALC],
297 [_AT_CHECK_JAVA_CALC([$1], [$2], [$3])
298 _AT_CHECK_JAVA_CALC([%error-verbose $1], [$2], [$3])
299 ])# AT_CHECK_JAVA_CALC
302 # ------------------------ #
303 # Simple LALR Calculator. #
304 # ------------------------ #
306 dnl AT_CHECK_JAVA_CALC([], [])
308 AT_CHECK_JAVA_CALC([%define single_class %locations], [[
311 public Calc (InputStream is)
313 Reader r = new InputStreamReader (is);
314 st = new StreamTokenizer(r);
316 st.eolIsSignificant (true);
317 st.whitespaceChars (9, 9);
318 st.whitespaceChars (32, 32);
319 st.wordChars (48, 57);
321 yyendpos = new Position (1);
324 public int yylex () throws IOException {
325 int ttype = st.nextToken ();
326 yystartpos = yyendpos;
327 if (ttype == st.TT_EOF)
330 else if (ttype == st.TT_EOL)
332 yyendpos = new Position (yyendpos.lineno () + 1);
336 else if (ttype == st.TT_WORD)
338 yylval = new Integer (st.sval);
346 public void yyerror (Location l, String s)
349 System.err.println (s);
351 System.err.println (l.begin + ": " + s);
354 public static void main (String args[]) throws IOException
356 new Calc (System.in).parse ();
360 AT_CHECK_JAVA_CALC([%pure-parser], [[
361 public static void main (String args[]) throws IOException
363 CalcLexer l = new CalcLexer (System.in);
364 Calc p = new Calc (l);
368 class CalcLexer implements Calc.Lexer {
373 public Object getLVal ()
378 public CalcLexer (InputStream is)
380 Reader r = new InputStreamReader (is);
381 st = new StreamTokenizer(r);
383 st.eolIsSignificant (true);
384 st.whitespaceChars (9, 9);
385 st.whitespaceChars (32, 32);
386 st.wordChars (48, 57);
389 public int yylex () throws IOException {
390 int ttype = st.nextToken ();
391 if (ttype == st.TT_EOF)
394 else if (ttype == st.TT_EOL)
397 else if (ttype == st.TT_WORD)
399 yylval = new Integer (st.sval);
408 public void yyerror (String s)
410 System.err.println (s);
415 dnl AT_CHECK_JAVA_CALC([%pure-parser %locations], [])