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