]> git.saurik.com Git - bison.git/blob - tests/calc.at
* src/vcg.h (graph_s): color, textcolor, bordercolor are now
[bison.git] / tests / calc.at
1 # Checking the output filenames. -*- Autotest -*-
2 # Copyright 2000, 2001 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 # 02111-1307, USA.
18
19 AT_BANNER([[Simple Calculator.]])
20
21 ## ---------------------------------------------------- ##
22 ## Compile the grammar described in the documentation. ##
23 ## ---------------------------------------------------- ##
24
25
26 # ------------------------- #
27 # Helping Autotest macros. #
28 # ------------------------- #
29
30
31 # _AT_DATA_CALC_Y($1, $2, $3, [CPP-DIRECTIVES])
32 # ---------------------------------------------
33 # Produce `calc.y'. Don't call this macro directly, because it contains
34 # some occurrences of `$1' etc. which will be interpreted by m4. So
35 # you should call it with $1, $2, and $3 as arguments, which is what
36 # AT_DATA_CALC_Y does.
37 m4_define([_AT_DATA_CALC_Y],
38 [m4_if([$1$2$3], $[1]$[2]$[3], [],
39 [m4_fatal([$0: Invalid arguments: $@])])dnl
40 AT_DATA([calc.y],
41 [[/* Infix notation calculator--calc */
42
43 %{
44 #include <config.h>
45 #include <stdio.h>
46
47 #if STDC_HEADERS
48 # include <stdlib.h>
49 # include <string.h>
50 #else
51 char *strcat(char *dest, const char *src);
52 #endif
53 #include <ctype.h>
54 ]$4[
55
56 static int power (int base, int exponent);
57 static void yyerror (const char *s);
58 static int yylex (void);
59 static int yygetc (void);
60 static void yyungetc (int c);
61
62 extern void perror (const char *s);
63 %}
64
65 /* BISON Declarations */
66 %token NUM
67
68 %nonassoc '=' /* comparison */
69 %left '-' '+'
70 %left '*' '/'
71 %left NEG /* negation--unary minus */
72 %right '^' /* exponentiation */
73
74 /* Grammar follows */
75 %%
76 input:
77 /* empty string */
78 | input line
79 ;
80
81 line:
82 '\n'
83 | exp '\n'
84 ;
85
86 exp:
87 NUM { $$ = $1; }
88 | exp '=' exp
89 {
90 if ($1 != $3)
91 printf ("calc: error: %d != %d\n", $1, $3);
92 $$ = $1 == $3;
93 }
94 | exp '+' exp { $$ = $1 + $3; }
95 | exp '-' exp { $$ = $1 - $3; }
96 | exp '*' exp { $$ = $1 * $3; }
97 | exp '/' exp { $$ = $1 / $3; }
98 | '-' exp %prec NEG { $$ = -$2; }
99 | exp '^' exp { $$ = power ($1, $3); }
100 | '(' exp ')' { $$ = $2; }
101 ;
102 %%
103 /* The input. */
104 FILE *yyin;
105
106 static void
107 yyerror (const char *s)
108 {
109 #if YYLSP_NEEDED
110 fprintf (stderr, "%d.%d:%d.%d: ",
111 yylloc.first_line, yylloc.first_column,
112 yylloc.last_line, yylloc.last_column);
113 #endif
114 fprintf (stderr, "%s\n", s);
115 }
116
117 static int
118 yygetc (void)
119 {
120 int res = getc (yyin);
121 #if YYLSP_NEEDED
122 if (res == '\n')
123 {
124 yylloc.last_line++;
125 yylloc.last_column = 0;
126 }
127 else
128 yylloc.last_column++;
129 #endif
130 return res;
131 }
132
133
134 static void
135 yyungetc (int c)
136 {
137 #if YYLSP_NEEDED
138 /* Wrong when C == `\n'. */
139 yylloc.last_column--;
140 #endif
141 ungetc (c, yyin);
142 }
143
144 static int
145 read_signed_integer (void)
146 {
147 int c = yygetc ();
148 int sign = 1;
149 int n = 0;
150
151 if (c == '-')
152 {
153 c = yygetc ();
154 sign = -1;
155 }
156
157 while (isdigit (c))
158 {
159 n = 10 * n + (c - '0');
160 c = yygetc ();
161 }
162
163 yyungetc (c);
164
165 return sign * n;
166 }
167
168
169
170 /*---------------------------------------------------------------.
171 | Lexical analyzer returns an integer on the stack and the token |
172 | NUM, or the ASCII character read if not a number. Skips all |
173 | blanks and tabs, returns 0 for EOF. |
174 `---------------------------------------------------------------*/
175
176 static int
177 yylex (void)
178 {
179 int c;
180
181 #if YYLSP_NEEDED
182 yylloc.first_column = yylloc.last_column;
183 yylloc.first_line = yylloc.last_line;
184 #endif
185
186 /* Skip white space. */
187 while ((c = yygetc ()) == ' ' || c == '\t')
188 {
189 #if YYLSP_NEEDED
190 yylloc.first_column = yylloc.last_column;
191 yylloc.first_line = yylloc.last_line;
192 #endif
193 }
194
195 /* process numbers */
196 if (c == '.' || isdigit (c))
197 {
198 yyungetc (c);
199 yylval = read_signed_integer ();
200 return NUM;
201 }
202
203 /* Return end-of-file. */
204 if (c == EOF)
205 return 0;
206
207 /* Return single chars. */
208 return c;
209 }
210
211 static int
212 power (int base, int exponent)
213 {
214 int res = 1;
215 if (exponent < 0)
216 exit (1);
217 for (/* Niente */; exponent; --exponent)
218 res *= base;
219 return res;
220 }
221
222 int
223 main (int argn, const char **argv)
224 {
225 if (argn == 2)
226 yyin = fopen (argv[1], "r");
227 else
228 yyin = stdin;
229
230 if (!stdin)
231 {
232 perror (argv[1]);
233 exit (1);
234 }
235
236 #if YYDEBUG
237 yydebug = 1;
238 #endif
239 #if YYLSP_NEEDED
240 yylloc.last_column = 0;
241 yylloc.last_line = 1;
242 #endif
243 yyparse ();
244 return 0;
245 }
246 ]])
247 ])# _AT_DATA_CALC_Y
248
249
250 # AT_DATA_CALC_Y([BISON-OPTIONS])
251 # -------------------------------
252 # Produce `calc.y'.
253 m4_define([AT_DATA_CALC_Y],
254 [_AT_DATA_CALC_Y($[1], $[2], $[3],
255 [m4_if(m4_regexp([$1], [--yyerror-verbose]),
256 [-1], [],
257 [[#define YYERROR_VERBOSE]])])])
258
259
260
261 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT)
262 # ------------------------------------
263 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
264 # If `--debug' is passed to bison, discard all the debugging traces
265 # preserving only the `parse errors'. Note that since there should be
266 # none, the `grep' will fail with exit status 1.
267 m4_define([_AT_CHECK_CALC],
268 [AT_DATA([[input]],
269 [[$2
270 ]])
271 m4_if(m4_regexp([$1], [--debug]),
272 [-1],
273 [AT_CHECK([./calc <input],
274 [0], [], [])],
275 [AT_CHECK([calc ./input 2>&1 >/dev/null | grep 'parse error' >&2],
276 [1], [], [])])])
277
278
279 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT,
280 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
281 # ------------------------------------------------------------
282 # Run `calc' on INPUT, and expect STDERR.
283 m4_define([_AT_CHECK_CALC_ERROR],
284 [AT_DATA([[input]],
285 [[$2
286 ]])
287
288 AT_CHECK([./calc <input 2>&1 >/dev/null | grep 'parse error' >&2], 0,
289 [],
290 [m4_if(m4_regexp([$1], [--location]),
291 [-1], [], [$3: ])[]dnl
292 parse error[]dnl
293 m4_if(m4_regexp([$1], [--yyerror-verbose]),
294 [-1], [], [$4])[]dnl
295
296 ])])
297
298
299 # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
300 # --------------------------------------------------------
301 # Start a testing chunk which compiles `calc' grammar with
302 # BISON-OPTIONS, and performs several tests over the parser.
303 m4_define([AT_CHECK_CALC],
304 [# We use integers to avoid dependencies upon the precision of doubles.
305 AT_SETUP([Calculator $1])
306
307 AT_DATA_CALC_Y([$1])
308
309 # Specify the output files to avoid problems on different file systems.
310 AT_CHECK([bison calc.y -o calc.c m4_patsubst([$1], [--yyerror-verbose])],
311 [0], [], [])
312 AT_CHECK([$CC $CFLAGS $CPPFLAGS calc.c -o calc], 0, [], [])
313
314 # Test the priorities.
315 _AT_CHECK_CALC([$1],
316 [1 + 2 * 3 = 7
317 1 + 2 * -3 = -5
318
319 -1^2 = -1
320 (-1)^2 = 1
321
322 ---1 = -1
323
324 1 - 2 - 3 = -4
325 1 - (2 - 3) = 2
326
327 2^2^3 = 256
328 (2^2)^3 = 64], [$2])
329
330 # Some parse errors.
331 _AT_CHECK_CALC_ERROR([$1], [+1],
332 [1.0:1.1],
333 [, unexpected `'+''])
334 _AT_CHECK_CALC_ERROR([$1], [1//2],
335 [1.2:1.3],
336 [, unexpected `'/'', expecting `NUM' or `'-'' or `'(''])
337 _AT_CHECK_CALC_ERROR([$1], [error],
338 [1.0:1.1],
339 [, unexpected `$undefined.'])
340 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3],
341 [1.6:1.7],
342 [, unexpected `'=''])
343 _AT_CHECK_CALC_ERROR([$1],
344 [
345 +1],
346 [2.0:2.1],
347 [, unexpected `'+''])
348
349 AT_CLEANUP(calc calc.c calc.h calc.output)
350 ])# AT_CHECK_CALC
351
352
353
354
355 # ------------------ #
356 # Test the parsers. #
357 # ------------------ #
358
359 AT_CHECK_CALC()
360
361 AT_CHECK_CALC([--defines])
362 AT_CHECK_CALC([--locations])
363 AT_CHECK_CALC([--name-prefix=calc])
364 AT_CHECK_CALC([--verbose])
365 AT_CHECK_CALC([--yacc])
366 AT_CHECK_CALC([--yyerror-verbose])
367
368 AT_CHECK_CALC([--locations --yyerror-verbose])
369
370 AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
371
372 AT_CHECK_CALC([--debug])
373 AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])