]> git.saurik.com Git - bison.git/blame - tests/java.at
* fnchange.lst: Reflect the renaming of c++ into cxx.
[bison.git] / tests / java.at
CommitLineData
0049ec86 1# Java tests for simple calculator. -*- Autotest -*-
8405b70c 2
0049ec86 3# Copyright (C) 2007 Free Software Foundation, Inc.
8405b70c
PB
4
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)
8# any later version.
9
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.
14
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
18# 02110-1301, USA.
19
20AT_BANNER([[Java Calculator.]])
21
22
23# ------------------------- #
24# Helping Autotest macros. #
25# ------------------------- #
26
27
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.
34m4_define([_AT_DATA_JAVA_CALC_Y],
35[m4_if([$1$2$3], $[1]$[2]$[3], [],
36 [m4_fatal([$0: Invalid arguments: $@])])dnl
37AT_DATA([Calc.y],
38[[/* Infix notation calculator--calc */
39%language "Java"
40%name-prefix "Calc"]
41%define parser_class_name "Calc"
42%define public
43
44$4[
45%code imports {
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;
51}
52
53/* Bison Declarations */
54%token <Integer> NUM "number"
55%type <Integer> exp
56
57%nonassoc '=' /* comparison */
58%left '-' '+'
59%left '*' '/'
60%left NEG /* negation--unary minus */
61%right '^' /* exponentiation */
62
63/* Grammar follows */
64%%
65input:
66 line
67| input line
68;
69
70line:
71 '\n'
72| exp '\n'
73| error '\n'
74;
75
76exp:
77 NUM { $$ = $1; }
78| exp '=' exp
79 {
80 if ($1.intValue () != $3.intValue ())
81 yyerror ("calc: error: " + $1 + " != " + $3);
82 }
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 (),
90 $3.intValue ())); }
91| '(' exp ')' { $$ = $2; }
92| '(' error ')' { $$ = new Integer (1111); }
93| '!' { $$ = new Integer (0); return YYERROR; }
94| '-' error { $$ = new Integer (0); return YYERROR; }
95;
96%%
97
98 class Position {
99 public int line;
100
101 public Position ()
102 {
103 line = 0;
104 }
105
106 public Position (int l)
107 {
108 line = l;
109 }
110
111 public long getHashCode ()
112 {
113 return line;
114 }
115
116 public boolean equals (Position l)
117 {
118 return l.line == line;
119 }
120
121 public String toString ()
122 {
123 return Integer.toString (line);
124 }
125
126 public int lineno ()
127 {
128 return line;
129 }
130 }
131
132]$5
133])
134])# _AT_DATA_JAVA_CALC_Y
135
136
137# AT_DATA_CALC_Y([BISON-OPTIONS], [BISON-EPILOGUE])
138# -------------------------------------------------
139# Produce `calc.y'.
140m4_define([AT_DATA_JAVA_CALC_Y],
141[_AT_DATA_JAVA_CALC_Y($[1], $[2], $[3], [$1], [$2])
142])
143
144
145
146# AT_JAVA_COMPILE(SOURCE)
147# -----------------------
148# Compile SOURCES into Java class files. Skip the test if java or javac is
149# not installed.
150m4_define([AT_JAVA_COMPILE],
0049ec86
PB
151[AT_CHECK([test -n "$CONF_JAVA" || exit 77
152test -n "$CONF_JAVAC" || exit 77])
8405b70c
PB
153AT_CHECK([$SHELL ../../../javacomp.sh $1],
154 0, [ignore], [ignore])])
155
156
157# AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
158# -----------------------------------------------------------------
159m4_define([AT_JAVA_PARSER_CHECK],
160[AT_CHECK([$5 $SHELL ../../../javaexec.sh $1], [$2], [$3], [$4])])
161
162
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.
167#
168# If INPUT starts with a slash, it is used as absolute input file name,
169# otherwise as contents.
170#
171# The VERBOSE-AND-LOCATED-ERROR-MESSAGE is stripped of locations
172# and expected tokens if necessary, and compared with the output.
173m4_define([_AT_CHECK_JAVA_CALC_ERROR],
174[m4_bmatch([$2], [^/],
175 [AT_JAVA_PARSER_CHECK([Calc < $2], 0, [], [stderr])],
176 [AT_DATA([[input]],
177[[$2
178]])
179AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])])
180
181# Normalize the observed and expected error messages, depending upon the
182# options.
183# 1. Create the reference error message.
184AT_DATA([[expout]],
185[$3
186])
187# 2. If locations are not used, remove them.
188AT_YYERROR_SEES_LOC_IF([],
189[[sed 's/^[-0-9.]*: //' expout >at-expout
190mv at-expout expout]])
191# 3. If error-verbose is not used, strip the`, unexpected....' part.
192m4_bmatch([$1], [%error-verbose], [],
193[[sed 's/syntax error, .*$/syntax error/' expout >at-expout
194mv at-expout expout]])
195# 4. Check
196AT_CHECK([cat stderr], 0, [expout])
197])
198
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.
203m4_define([_AT_CHECK_JAVA_CALC],
204[# We use integers to avoid dependencies upon the precision of doubles.
205AT_SETUP([Calculator $1])
206
207AT_BISON_OPTION_PUSHDEFS([$1])
208
209AT_DATA_JAVA_CALC_Y([$1
210%code {
211$2
212}], [$3])
213
214AT_CHECK([bison -o Calc.java Calc.y])
215AT_JAVA_COMPILE([Calc.java])
216
217# Test the priorities.
218AT_DATA([[input]],
219[[1 + 2 * 3 = 7
2201 + 2 * -3 = -5
221
222-1^2 = -1
223(-1)^2 = 1
224
225---1 = -1
226
2271 - 2 - 3 = -4
2281 - (2 - 3) = 2
229
2302^2^3 = 256
231(2^2)^3 = 64
232]])
233AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])
234
235
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], [
246+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])
251
252# Exercise the error token: without it, we die at the first error,
253# hence be sure to
254#
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
260#
261# - test the action associated to `error'
262#
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.
267#
268_AT_CHECK_JAVA_CALC_ERROR([$1],
269 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
270[1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
2711: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
2721: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
2731: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
274calc: error: 4444 != 1])
275
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
280calc: error: 2222 != 1])
281_AT_CHECK_JAVA_CALC_ERROR([$1], [(- *) + (0 0) = 1],
282[1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
2831: syntax error, unexpected number
284calc: error: 2222 != 1])
285AT_BISON_OPTION_POPDEFS
286
287AT_CLEANUP
288])# _AT_CHECK_JAVA_CALC
289
290
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.
296m4_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
300
301
302# ------------------------ #
303# Simple LALR Calculator. #
304# ------------------------ #
305
306dnl AT_CHECK_JAVA_CALC([], [])
307
308AT_CHECK_JAVA_CALC([%define single_class %locations], [[
309 StreamTokenizer st;
310
311 public Calc (InputStream is)
312 {
313 Reader r = new InputStreamReader (is);
314 st = new StreamTokenizer(r);
315 st.resetSyntax ();
316 st.eolIsSignificant (true);
317 st.whitespaceChars (9, 9);
318 st.whitespaceChars (32, 32);
319 st.wordChars (48, 57);
320
321 yyendpos = new Position (1);
322 }
323
324 public int yylex () throws IOException {
325 int ttype = st.nextToken ();
326 yystartpos = yyendpos;
327 if (ttype == st.TT_EOF)
328 return EOF;
329
330 else if (ttype == st.TT_EOL)
331 {
332 yyendpos = new Position (yyendpos.lineno () + 1);
333 return (int) '\n';
334 }
335
336 else if (ttype == st.TT_WORD)
337 {
338 yylval = new Integer (st.sval);
339 return NUM;
340 }
341
342 else
343 return st.ttype;
344 }
345
346 public void yyerror (Location l, String s)
347 {
348 if (l == null)
349 System.err.println (s);
350 else
351 System.err.println (l.begin + ": " + s);
352 }
353
354 public static void main (String args[]) throws IOException
355 {
356 new Calc (System.in).parse ();
357 }
358]])
359
360AT_CHECK_JAVA_CALC([%pure-parser], [[
361 public static void main (String args[]) throws IOException
362 {
363 CalcLexer l = new CalcLexer (System.in);
364 Calc p = new Calc (l);
365 p.parse ();
366 }
367]], [[
368class CalcLexer implements Calc.Lexer {
369 Integer yylval;
370
371 StreamTokenizer st;
372
373 public Object getLVal ()
374 {
375 return yylval;
376 }
377
378 public CalcLexer (InputStream is)
379 {
380 Reader r = new InputStreamReader (is);
381 st = new StreamTokenizer(r);
382 st.resetSyntax ();
383 st.eolIsSignificant (true);
384 st.whitespaceChars (9, 9);
385 st.whitespaceChars (32, 32);
386 st.wordChars (48, 57);
387 }
388
389 public int yylex () throws IOException {
390 int ttype = st.nextToken ();
391 if (ttype == st.TT_EOF)
392 return Calc.EOF;
393
394 else if (ttype == st.TT_EOL)
395 return (int) '\n';
396
397 else if (ttype == st.TT_WORD)
398 {
399 yylval = new Integer (st.sval);
400 return Calc.NUM;
401 }
402
403 else
404 return st.ttype;
405 }
406
407
408 public void yyerror (String s)
409 {
410 System.err.println (s);
411 }
412}
413]])
414
415dnl AT_CHECK_JAVA_CALC([%pure-parser %locations], [])