]> git.saurik.com Git - bison.git/blame - tests/java.at
Use ASCII for Sebastien Fricker's name.
[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
01b477c6 28# _AT_DATA_JAVA_CALC_Y($1, $2, $3, [BISON-DIRECTIVES])
8405b70c
PB
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"
01b477c6 40%name-prefix "Calc"
8405b70c
PB
41%define parser_class_name "Calc"
42%define public
43
01b477c6
PB
44]$4[
45
8405b70c
PB
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%%
66input:
67 line
68| input line
69;
70
71line:
72 '\n'
73| exp '\n'
74| error '\n'
75;
76
77exp:
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;
01b477c6
PB
97
98]AT_LEXPARAM_IF([[
99%code lexer {
100]],
101[[
8405b70c 102%%
01b477c6
PB
103class CalcLexer implements Calc.Lexer {
104]])[
105 StreamTokenizer st;
106
107 public ]AT_LEXPARAM_IF([[YYLexer]], [[CalcLexer]]) (InputStream is)
108 {
109 st = new StreamTokenizer (new InputStreamReader (is));
110 st.resetSyntax ();
111 st.eolIsSignificant (true);
112 st.whitespaceChars (9, 9);
113 st.whitespaceChars (32, 32);
114 st.wordChars (48, 57);
115 }
116
117AT_LOCATION_IF([[
118 Position yystartpos;
119 Position yyendpos = new Position (1);
8405b70c 120
01b477c6
PB
121 public Position getStartPos() {
122 return yystartpos;
8405b70c
PB
123 }
124
01b477c6
PB
125 public Position getEndPos() {
126 return yyendpos;
127 }
128
129 public void yyerror (Calc.Location l, String s)
130 {
131 if (l == null)
132 System.err.println (s);
133 else
134 System.err.println (l.begin + ": " + s);
135 }
136]], [[
137 public void yyerror (String s)
138 {
139 System.err.println (s);
140 }
141]])[
142
143 Integer yylval;
144
145 public Object getLVal() {
146 return yylval;
147 }
148
149 public int yylex () throws IOException {
150 int ttype = st.nextToken ();
151 ]AT_LOCATION_IF([[yystartpos = yyendpos;]])[
152 if (ttype == st.TT_EOF)
153 return Calc.EOF;
154
155 else if (ttype == st.TT_EOL)
156 {
157 ]AT_LOCATION_IF([[yyendpos = new Position (yyendpos.lineno () + 1);]])[
158 return (int) '\n';
159 }
160
161 else if (ttype == st.TT_WORD)
162 {
163 yylval = new Integer (st.sval);
164 return Calc.NUM;
165 }
166
167 else
168 return st.ttype;
169 }
170
171
172]AT_LEXPARAM_IF([[
173};
174%%]], [[
175}]])
176
177[
178class Position {
179 public int line;
180
181 public Position ()
182 {
183 line = 0;
184 }
185
186 public Position (int l)
187 {
188 line = l;
189 }
190
191 public long getHashCode ()
192 {
193 return line;
194 }
195
196 public boolean equals (Position l)
197 {
198 return l.line == line;
199 }
200
201 public String toString ()
202 {
203 return Integer.toString (line);
204 }
205
206 public int lineno ()
207 {
208 return line;
209 }
210}
211
212]])
8405b70c
PB
213])# _AT_DATA_JAVA_CALC_Y
214
215
01b477c6 216# AT_DATA_CALC_Y([BISON-OPTIONS])
8405b70c
PB
217# -------------------------------------------------
218# Produce `calc.y'.
219m4_define([AT_DATA_JAVA_CALC_Y],
01b477c6 220[_AT_DATA_JAVA_CALC_Y($[1], $[2], $[3], [$1])
8405b70c
PB
221])
222
223
224
225# AT_JAVA_COMPILE(SOURCE)
226# -----------------------
227# Compile SOURCES into Java class files. Skip the test if java or javac is
228# not installed.
229m4_define([AT_JAVA_COMPILE],
0049ec86
PB
230[AT_CHECK([test -n "$CONF_JAVA" || exit 77
231test -n "$CONF_JAVAC" || exit 77])
8405b70c
PB
232AT_CHECK([$SHELL ../../../javacomp.sh $1],
233 0, [ignore], [ignore])])
234
235
236# AT_JAVA_PARSER_CHECK(COMMAND, EXIT-STATUS, EXPOUT, EXPERR, [PRE])
237# -----------------------------------------------------------------
238m4_define([AT_JAVA_PARSER_CHECK],
239[AT_CHECK([$5 $SHELL ../../../javaexec.sh $1], [$2], [$3], [$4])])
240
241
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.
246#
247# If INPUT starts with a slash, it is used as absolute input file name,
248# otherwise as contents.
249#
250# The VERBOSE-AND-LOCATED-ERROR-MESSAGE is stripped of locations
251# and expected tokens if necessary, and compared with the output.
252m4_define([_AT_CHECK_JAVA_CALC_ERROR],
253[m4_bmatch([$2], [^/],
254 [AT_JAVA_PARSER_CHECK([Calc < $2], 0, [], [stderr])],
255 [AT_DATA([[input]],
256[[$2
257]])
258AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])])
259
260# Normalize the observed and expected error messages, depending upon the
261# options.
262# 1. Create the reference error message.
263AT_DATA([[expout]],
264[$3
265])
266# 2. If locations are not used, remove them.
267AT_YYERROR_SEES_LOC_IF([],
268[[sed 's/^[-0-9.]*: //' expout >at-expout
269mv at-expout expout]])
270# 3. If error-verbose is not used, strip the`, unexpected....' part.
271m4_bmatch([$1], [%error-verbose], [],
272[[sed 's/syntax error, .*$/syntax error/' expout >at-expout
273mv at-expout expout]])
274# 4. Check
275AT_CHECK([cat stderr], 0, [expout])
276])
277
01b477c6 278# _AT_CHECK_JAVA_CALC([BISON-DIRECTIVES], [BISON-CODE])
8405b70c
PB
279# -----------------------------------------------------------------------
280# Start a testing chunk which compiles `calc' grammar with
281# BISON-DIRECTIVES, and performs several tests over the parser.
282m4_define([_AT_CHECK_JAVA_CALC],
283[# We use integers to avoid dependencies upon the precision of doubles.
284AT_SETUP([Calculator $1])
285
286AT_BISON_OPTION_PUSHDEFS([$1])
287
288AT_DATA_JAVA_CALC_Y([$1
289%code {
290$2
01b477c6 291}])
8405b70c
PB
292
293AT_CHECK([bison -o Calc.java Calc.y])
294AT_JAVA_COMPILE([Calc.java])
295
296# Test the priorities.
297AT_DATA([[input]],
298[[1 + 2 * 3 = 7
2991 + 2 * -3 = -5
300
301-1^2 = -1
302(-1)^2 = 1
303
304---1 = -1
305
3061 - 2 - 3 = -4
3071 - (2 - 3) = 2
308
3092^2^3 = 256
310(2^2)^3 = 64
311]])
312AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])
313
314
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], [
325+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])
330
331# Exercise the error token: without it, we die at the first error,
332# hence be sure to
333#
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
339#
340# - test the action associated to `error'
341#
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.
346#
347_AT_CHECK_JAVA_CALC_ERROR([$1],
348 [() + (1 + 1 + 1 +) + (* * *) + (1 * 2 * *) = 1],
349[1: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
3501: syntax error, unexpected ')', expecting number or '-' or '(' or '!'
3511: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
3521: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
353calc: error: 4444 != 1])
354
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
359calc: error: 2222 != 1])
360_AT_CHECK_JAVA_CALC_ERROR([$1], [(- *) + (0 0) = 1],
361[1: syntax error, unexpected '*', expecting number or '-' or '(' or '!'
3621: syntax error, unexpected number
363calc: error: 2222 != 1])
364AT_BISON_OPTION_POPDEFS
365
366AT_CLEANUP
367])# _AT_CHECK_JAVA_CALC
368
369
01b477c6 370# AT_CHECK_JAVA_CALC([BISON-DIRECTIVES])
8405b70c
PB
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.
375m4_define([AT_CHECK_JAVA_CALC],
01b477c6
PB
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])
8405b70c
PB
380])# AT_CHECK_JAVA_CALC
381
382
383# ------------------------ #
384# Simple LALR Calculator. #
385# ------------------------ #
386
01b477c6 387AT_CHECK_JAVA_CALC([], [[
8405b70c
PB
388 public static void main (String args[]) throws IOException
389 {
390 CalcLexer l = new CalcLexer (System.in);
391 Calc p = new Calc (l);
392 p.parse ();
393 }
01b477c6 394]])
8405b70c 395
01b477c6
PB
396AT_CHECK_JAVA_CALC([%lex-param { InputStream is } ], [[
397 public static void main (String args[]) throws IOException
8405b70c 398 {
01b477c6 399 new Calc (System.in).parse ();
8405b70c 400 }
8405b70c 401]])