]> git.saurik.com Git - bison.git/blob - tests/java.at
6d991c1b5e539dbabd185acab692c2865da83ca6
[bison.git] / tests / java.at
1 # Simple calculator. -*- Autotest -*-
2
3 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4 # Foundation, Inc.
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 AT_BANNER([[Java Calculator.]])
22
23
24 # ------------------------- #
25 # Helping Autotest macros. #
26 # ------------------------- #
27
28
29 # _AT_DATA_JAVA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES], [BISON-EPILOGUE])
30 # ----------------------------------------------------------------------
31 # Produce `calc.y'. Don't call this macro directly, because it contains
32 # some occurrences of `$1' etc. which will be interpreted by m4. So
33 # you should call it with $1, $2, and $3 as arguments, which is what
34 # AT_DATA_JAVA_CALC_Y does.
35 m4_define([_AT_DATA_JAVA_CALC_Y],
36 [m4_if([$1$2$3], $[1]$[2]$[3], [],
37 [m4_fatal([$0: Invalid arguments: $@])])dnl
38 AT_DATA([Calc.y],
39 [[/* Infix notation calculator--calc */
40 %language "Java"
41 %name-prefix "Calc"]
42 %define parser_class_name "Calc"
43 %define public
44
45 $4[
46 %code imports {
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;
52 }
53
54 /* Bison Declarations */
55 %token <Integer> NUM "number"
56 %type <Integer> exp
57
58 %nonassoc '=' /* comparison */
59 %left '-' '+'
60 %left '*' '/'
61 %left NEG /* negation--unary minus */
62 %right '^' /* exponentiation */
63
64 /* Grammar follows */
65 %%
66 input:
67 line
68 | input line
69 ;
70
71 line:
72 '\n'
73 | exp '\n'
74 | error '\n'
75 ;
76
77 exp:
78 NUM { $$ = $1; }
79 | exp '=' exp
80 {
81 if ($1.intValue () != $3.intValue ())
82 yyerror ("calc: error: " + $1 + " != " + $3);
83 }
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 (),
91 $3.intValue ())); }
92 | '(' exp ')' { $$ = $2; }
93 | '(' error ')' { $$ = new Integer (1111); }
94 | '!' { $$ = new Integer (0); return YYERROR; }
95 | '-' error { $$ = new Integer (0); return YYERROR; }
96 ;
97 %%
98
99 class Position {
100 public int line;
101
102 public Position ()
103 {
104 line = 0;
105 }
106
107 public Position (int l)
108 {
109 line = l;
110 }
111
112 public long getHashCode ()
113 {
114 return line;
115 }
116
117 public boolean equals (Position l)
118 {
119 return l.line == line;
120 }
121
122 public String toString ()
123 {
124 return Integer.toString (line);
125 }
126
127 public int lineno ()
128 {
129 return line;
130 }
131 }
132
133 ]$5
134 ])
135 ])# _AT_DATA_JAVA_CALC_Y
136
137
138 # AT_DATA_CALC_Y([BISON-OPTIONS], [BISON-EPILOGUE])
139 # -------------------------------------------------
140 # Produce `calc.y'.
141 m4_define([AT_DATA_JAVA_CALC_Y],
142 [_AT_DATA_JAVA_CALC_Y($[1], $[2], $[3], [$1], [$2])
143 ])
144
145
146
147 # AT_JAVA_COMPILE(SOURCE)
148 # -----------------------
149 # Compile SOURCES into Java class files. Skip the test if java or javac is
150 # not installed.
151 m4_define([AT_JAVA_COMPILE],
152 [AT_CHECK([test -n "$CONF_JAVA$CONF_JAVAC" || exit 77])
153 AT_CHECK([$SHELL ../../../javacomp.sh $1],
154 0, [ignore], [ignore])])
155
156
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])])
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.
173 m4_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 ]])
179 AT_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.
184 AT_DATA([[expout]],
185 [$3
186 ])
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]])
195 # 4. Check
196 AT_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.
203 m4_define([_AT_CHECK_JAVA_CALC],
204 [# We use integers to avoid dependencies upon the precision of doubles.
205 AT_SETUP([Calculator $1])
206
207 AT_BISON_OPTION_PUSHDEFS([$1])
208
209 AT_DATA_JAVA_CALC_Y([$1
210 %code {
211 $2
212 }], [$3])
213
214 AT_CHECK([bison -o Calc.java Calc.y])
215 AT_JAVA_COMPILE([Calc.java])
216
217 # Test the priorities.
218 AT_DATA([[input]],
219 [[1 + 2 * 3 = 7
220 1 + 2 * -3 = -5
221
222 -1^2 = -1
223 (-1)^2 = 1
224
225 ---1 = -1
226
227 1 - 2 - 3 = -4
228 1 - (2 - 3) = 2
229
230 2^2^3 = 256
231 (2^2)^3 = 64
232 ]])
233 AT_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 '!'
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])
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
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
286
287 AT_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.
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
300
301
302 # ------------------------ #
303 # Simple LALR Calculator. #
304 # ------------------------ #
305
306 dnl AT_CHECK_JAVA_CALC([], [])
307
308 AT_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
360 AT_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 ]], [[
368 class 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
415 dnl AT_CHECK_JAVA_CALC([%pure-parser %locations], [])