]> git.saurik.com Git - bison.git/blame - tests/calc.m4
* src/reader.c (parse_expect_decl): Use `skip_white_space' and
[bison.git] / tests / calc.m4
CommitLineData
0d533154
AD
1# -*- Autoconf -*-
2
3cat <<EOF
4
5Simple Calculator.
6
7EOF
8
db5b3a89 9
0d533154
AD
10## ---------------------------------------------------- ##
11## Compile the grammar described in the documentation. ##
12## ---------------------------------------------------- ##
13
0d533154 14
db5b3a89
AD
15# ------------------------- #
16# Helping Autotest macros. #
17# ------------------------- #
18
19
d6c2cba0
AD
20# _AT_DATA_CALC_Y($1, $2, $3, [CPP-DIRECTIVES])
21# ---------------------------------------------
db5b3a89
AD
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.
26AT_DEFINE([_AT_DATA_CALC_Y],
d6c2cba0
AD
27[ifelse([$1$2$3],
28 $[1]$[2]$[3], [],
29 [errprint([$0: Invalid arguments: $@
30])m4exit(1)])dnl
31AT_DATA([calc.y],
0d533154
AD
32[[/* Infix notation calculator--calc */
33
34%{
35#include <stdio.h>
36#include <stdlib.h>
37#include <ctype.h>
d6c2cba0 38]$4[
0d533154
AD
39
40static int power (int base, int exponent);
ceed8467 41static void yyerror (const char *s);
5a35a6cb 42static int yylex (void);
0d533154
AD
43extern void perror (const char *s);
44%}
45
46/* BISON Declarations */
47%token NUM
d6c2cba0
AD
48
49%nonassoc '=' /* comparison */
0d533154
AD
50%left '-' '+'
51%left '*' '/'
52%left NEG /* negation--unary minus */
53%right '^' /* exponentiation */
54
55/* Grammar follows */
56%%
d6c2cba0
AD
57input:
58 /* empty string */
59| input line
0d533154
AD
60;
61
d6c2cba0
AD
62line:
63 '\n'
64| exp '\n'
0d533154
AD
65;
66
d6c2cba0
AD
67exp:
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; }
0d533154
AD
82;
83%%
ceed8467
AD
84/* The input. */
85FILE *yyin;
05a1d24b 86
ceed8467 87static void
0d533154
AD
88yyerror (const char *s)
89{
89cab50d
AD
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
0d533154
AD
95 fprintf (stderr, "%s\n", s);
96}
97
98static int
89cab50d
AD
99yygetc ()
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
115static void
116yyungetc (int c)
117{
118#ifdef YYLSP_NEEDED
119 /* Wrong when C == `\n'. */
120 yylloc.last_column--;
121#endif
122 ungetc (c, yyin);
123}
124
125static int
126read_signed_integer (void)
0d533154 127{
89cab50d 128 int c = yygetc ();
0d533154
AD
129 int sign = 1;
130 int n = 0;
131
132 if (c == '-')
133 {
89cab50d 134 c = yygetc ();
0d533154
AD
135 sign = -1;
136 }
137
138 while (isdigit (c))
139 {
140 n = 10 * n + (c - '0');
89cab50d 141 c = yygetc ();
0d533154
AD
142 }
143
89cab50d 144 yyungetc (c);
0d533154
AD
145
146 return sign * n;
147}
148
89cab50d
AD
149
150
0d533154
AD
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
5a35a6cb
AD
157static int
158yylex (void)
0d533154
AD
159{
160 int c;
161
89cab50d
AD
162#ifdef YYLSP_NEEDED
163 yylloc.first_column = yylloc.last_column;
164 yylloc.first_line = yylloc.last_line;
165#endif
166
0d533154 167 /* Skip white space. */
89cab50d
AD
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
0d533154
AD
176 /* process numbers */
177 if (c == '.' || isdigit (c))
178 {
89cab50d
AD
179 yyungetc (c);
180 yylval = read_signed_integer ();
0d533154
AD
181 return NUM;
182 }
89cab50d 183
0d533154
AD
184 /* Return end-of-file. */
185 if (c == EOF)
186 return 0;
89cab50d 187
0d533154
AD
188 /* Return single chars. */
189 return c;
190}
191
192static int
193power (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}
ceed8467
AD
202
203int
204main (int argn, const char **argv)
205{
206 if (argn == 2)
207 yyin = fopen (argv[1], "r");
208 else
89cab50d 209 yyin = stdin;
ceed8467
AD
210
211 if (!stdin)
212 {
213 perror (argv[1]);
214 exit (1);
215 }
db5b3a89
AD
216
217#if YYDEBUG
218 yydebug = 1;
89cab50d
AD
219#endif
220#ifdef YYLSP_NEEDED
221 yylloc.last_column = 0;
222 yylloc.last_line = 1;
db5b3a89 223#endif
ceed8467
AD
224 yyparse ();
225 return 0;
226}
0d533154 227]])
db5b3a89 228])# _AT_DATA_CALC_Y
0d533154 229
0d533154 230
d6c2cba0
AD
231# AT_DATA_CALC_Y([BISON-OPTIONS])
232# -------------------------------
db5b3a89
AD
233# Produce `calc.y'.
234AT_DEFINE([AT_DATA_CALC_Y],
d6c2cba0
AD
235[_AT_DATA_CALC_Y($[1], $[2], $[3],
236 [ifelse(regexp([$1], [--yyerror-verbose]),
237 [-1], [],
238 [[#define YYERROR_VERBOSE]])])])
239
db5b3a89
AD
240
241
d6c2cba0
AD
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.
db5b3a89 248AT_DEFINE([_AT_CHECK_CALC],
d6c2cba0
AD
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
89cab50d
AD
257# _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT,
258# [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
259# ------------------------------------------------------------
d6c2cba0
AD
260# Run `calc' on INPUT, and expect STDERR.
261AT_DEFINE([_AT_CHECK_CALC_ERROR],
262[AT_CHECK([echo "$2" | calc 2>&1 >/dev/null | grep 'parse error' >&2], 0,
263 [],
89cab50d
AD
264[ifelse(regexp([$1], [--location]),
265 [-1], [], [$3: ])[]dnl
266parse error[]dnl
267ifelse(regexp([$1], [--yyerror-verbose]),
268 [-1], [], [$4])[]dnl
269
d6c2cba0 270])])
db5b3a89
AD
271
272
5a35a6cb
AD
273# AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
274# --------------------------------------------------------
275# Start a testing chunk which compiles `calc' grammar with
db5b3a89 276# BISON-OPTIONS, and performs several tests over the parser.
0d533154 277AT_DEFINE([AT_CHECK_CALC],
db5b3a89 278[# We use integers to avoid dependencies upon the precision of doubles.
5a35a6cb 279AT_SETUP([Calculator $1])
db5b3a89 280
d6c2cba0 281AT_DATA_CALC_Y([$1])
db5b3a89
AD
282
283# Specify the output files to avoid problems on different file systems.
d6c2cba0
AD
284AT_CHECK([bison calc.y -o calc.c patsubst([$1], [--yyerror-verbose])],
285 [0], [], [])
db5b3a89 286AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], [])
0d533154
AD
287
288# Test the priorities.
d6c2cba0
AD
289_AT_CHECK_CALC([$1],
290[1 + 2 * 3 = 7
2911 + 2 * -3 = -5
292
293-1^2 = -1
294(-1)^2 = 1
0d533154 295
d6c2cba0 296---1 = -1
0d533154 297
d6c2cba0
AD
2981 - 2 - 3 = -4
2991 - (2 - 3) = 2
0d533154 300
d6c2cba0
AD
3012^2^3 = 256
302(2^2)^3 = 64], [$2])
0d533154 303
d6c2cba0
AD
304# Some parse errors.
305_AT_CHECK_CALC_ERROR([$1], [+1],
89cab50d 306 [1.0:1.1],
d6c2cba0
AD
307 [, unexpected `'+''])
308_AT_CHECK_CALC_ERROR([$1], [1//2],
89cab50d 309 [1.2:1.3],
d6c2cba0
AD
310 [, unexpected `'/'', expecting `NUM' or `'-'' or `'(''])
311_AT_CHECK_CALC_ERROR([$1], [error],
89cab50d 312 [1.0:1.1],
d6c2cba0
AD
313 [, unexpected `$undefined.'])
314_AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3],
89cab50d 315 [1.6:1.7],
d6c2cba0 316 [, unexpected `'=''])
89cab50d
AD
317_AT_CHECK_CALC_ERROR([$1],
318 [
319+1],
320 [2.0:2.1],
321 [, unexpected `'+''])
0d533154 322
5a35a6cb 323AT_CLEANUP(calc calc.c calc.h calc.output)
db5b3a89
AD
324])# AT_CHECK_CALC
325
326
db5b3a89
AD
327
328
d6c2cba0
AD
329# ------------------ #
330# Test the parsers. #
331# ------------------ #
332
5a35a6cb
AD
333AT_CHECK_CALC()
334# This one is very suspicious. The test fails, but it might be normal.
335AT_CHECK_CALC([--raw])
db5b3a89 336
5a35a6cb 337AT_CHECK_CALC([--defines])
89cab50d 338AT_CHECK_CALC([--locations])
5a35a6cb
AD
339AT_CHECK_CALC([--name-prefix=calc])
340AT_CHECK_CALC([--verbose])
341AT_CHECK_CALC([--yacc])
d6c2cba0 342AT_CHECK_CALC([--yyerror-verbose])
db5b3a89 343
89cab50d
AD
344AT_CHECK_CALC([--locations --yyerror-verbose])
345
346AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
347
d6c2cba0 348AT_CHECK_CALC([--debug])
89cab50d 349AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])