]> git.saurik.com Git - bison.git/blame_incremental - tests/calc.m4
Also test parse error messages, including with YYERROR_VERBOSE.
[bison.git] / tests / calc.m4
... / ...
CommitLineData
1# -*- Autoconf -*-
2
3cat <<EOF
4
5Simple Calculator.
6
7EOF
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.
26AT_DEFINE([_AT_DATA_CALC_Y],
27[ifelse([$1$2$3],
28 $[1]$[2]$[3], [],
29 [errprint([$0: Invalid arguments: $@
30])m4exit(1)])dnl
31AT_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
40static int power (int base, int exponent);
41static int read_signed_integer (FILE *stream);
42static void yyerror (const char *s);
43static int yylex (void);
44extern void perror (const char *s);
45%}
46
47/* BISON Declarations */
48%token NUM
49
50%nonassoc '=' /* comparison */
51%left '-' '+'
52%left '*' '/'
53%left NEG /* negation--unary minus */
54%right '^' /* exponentiation */
55
56/* Grammar follows */
57%%
58input:
59 /* empty string */
60| input line
61;
62
63line:
64 '\n'
65| exp '\n'
66;
67
68exp:
69 NUM { $$ = $1; }
70| exp '=' exp
71 {
72 if ($1 != $3)
73 printf ("calc: error: %d != %d\n", $1, $3);
74 $$ = $1 == $3;
75 }
76| exp '+' exp { $$ = $1 + $3; }
77| exp '-' exp { $$ = $1 - $3; }
78| exp '*' exp { $$ = $1 * $3; }
79| exp '/' exp { $$ = $1 / $3; }
80| '-' exp %prec NEG { $$ = -$2; }
81| exp '^' exp { $$ = power ($1, $3); }
82| '(' exp ')' { $$ = $2; }
83;
84%%
85/* The input. */
86FILE *yyin;
87
88static void
89yyerror (const char *s)
90{
91 fprintf (stderr, "%s\n", s);
92}
93
94static int
95read_signed_integer (FILE *stream)
96{
97 int c = getc (stream);
98 int sign = 1;
99 int n = 0;
100
101 if (c == '-')
102 {
103 c = getc (stream);
104 sign = -1;
105 }
106
107 while (isdigit (c))
108 {
109 n = 10 * n + (c - '0');
110 c = getc (stream);
111 }
112
113 ungetc (c, stream);
114
115 return sign * n;
116}
117
118/*---------------------------------------------------------------.
119| Lexical analyzer returns an integer on the stack and the token |
120| NUM, or the ASCII character read if not a number. Skips all |
121| blanks and tabs, returns 0 for EOF. |
122`---------------------------------------------------------------*/
123
124static int
125yylex (void)
126{
127 int c;
128
129 /* Skip white space. */
130 while ((c = getc (yyin)) == ' ' || c == '\t')
131 ;
132 /* process numbers */
133 if (c == '.' || isdigit (c))
134 {
135 ungetc (c, yyin);
136 yylval = read_signed_integer (yyin);
137 return NUM;
138 }
139 /* Return end-of-file. */
140 if (c == EOF)
141 return 0;
142 /* Return single chars. */
143 return c;
144}
145
146static int
147power (int base, int exponent)
148{
149 int res = 1;
150 if (exponent < 0)
151 exit (1);
152 for (/* Niente */; exponent; --exponent)
153 res *= base;
154 return res;
155}
156
157int
158main (int argn, const char **argv)
159{
160 if (argn == 2)
161 yyin = fopen (argv[1], "r");
162 else
163 yyin = stdin;
164
165 if (!stdin)
166 {
167 perror (argv[1]);
168 exit (1);
169 }
170
171#if YYDEBUG
172 yydebug = 1;
173#endif
174 yyparse ();
175 return 0;
176}
177]])
178])# _AT_DATA_CALC_Y
179
180
181# AT_DATA_CALC_Y([BISON-OPTIONS])
182# -------------------------------
183# Produce `calc.y'.
184AT_DEFINE([AT_DATA_CALC_Y],
185[_AT_DATA_CALC_Y($[1], $[2], $[3],
186 [ifelse(regexp([$1], [--yyerror-verbose]),
187 [-1], [],
188 [[#define YYERROR_VERBOSE]])])])
189
190
191
192# _AT_CHECK_CALC(BISON-OPTIONS, INPUT)
193# ------------------------------------
194# Run `calc' on INPUT and expect no STDOUT nor STDERR.
195# If `--debug' is passed to bison, discard all the debugging traces
196# preserving only the `parse errors'. Note that since there should be
197# none, the `grep' will fail with exit status 1.
198AT_DEFINE([_AT_CHECK_CALC],
199[ifelse(regexp([$1], [--debug]),
200 [-1],
201 [AT_CHECK([echo "$2" | calc],
202 [0], [], [])],
203 [AT_CHECK([echo "$2" | calc 2>&1 >/dev/null | grep 'parse error' >&2],
204 [1], [], [])])])
205
206
207# _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT, [IF-YYERROR-VERBOSE])
208# ----------------------------------------------------------------
209# Run `calc' on INPUT, and expect STDERR.
210AT_DEFINE([_AT_CHECK_CALC_ERROR],
211[AT_CHECK([echo "$2" | calc 2>&1 >/dev/null | grep 'parse error' >&2], 0,
212 [],
213 [parse error[]ifelse(regexp([$1], [--yyerror-verbose]),
214 [-1], [], [$3])
215])])
216
217
218# AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
219# --------------------------------------------------------
220# Start a testing chunk which compiles `calc' grammar with
221# BISON-OPTIONS, and performs several tests over the parser.
222AT_DEFINE([AT_CHECK_CALC],
223[# We use integers to avoid dependencies upon the precision of doubles.
224AT_SETUP([Calculator $1])
225
226AT_DATA_CALC_Y([$1])
227
228# Specify the output files to avoid problems on different file systems.
229AT_CHECK([bison calc.y -o calc.c patsubst([$1], [--yyerror-verbose])],
230 [0], [], [])
231AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], [])
232
233# Test the priorities.
234_AT_CHECK_CALC([$1],
235[1 + 2 * 3 = 7
2361 + 2 * -3 = -5
237
238-1^2 = -1
239(-1)^2 = 1
240
241---1 = -1
242
2431 - 2 - 3 = -4
2441 - (2 - 3) = 2
245
2462^2^3 = 256
247(2^2)^3 = 64], [$2])
248
249# Some parse errors.
250_AT_CHECK_CALC_ERROR([$1], [+1],
251 [, unexpected `'+''])
252_AT_CHECK_CALC_ERROR([$1], [1//2],
253 [, unexpected `'/'', expecting `NUM' or `'-'' or `'(''])
254_AT_CHECK_CALC_ERROR([$1], [error],
255 [, unexpected `$undefined.'])
256_AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3],
257 [, unexpected `'=''])
258
259AT_CLEANUP(calc calc.c calc.h calc.output)
260])# AT_CHECK_CALC
261
262
263
264
265# ------------------ #
266# Test the parsers. #
267# ------------------ #
268
269AT_CHECK_CALC()
270# This one is very suspicious. The test fails, but it might be normal.
271AT_CHECK_CALC([--raw])
272
273AT_CHECK_CALC([--defines])
274AT_CHECK_CALC([--name-prefix=calc])
275AT_CHECK_CALC([--verbose])
276AT_CHECK_CALC([--yacc])
277AT_CHECK_CALC([--yyerror-verbose])
278AT_CHECK_CALC([--defines --name-prefix=calc --verbose --yacc --yyerror-verbose])
279
280# When --debug, a lot of data is sent to STDERR, we can't test it.
281AT_CHECK_CALC([--debug])
282AT_CHECK_CALC([--debug --defines --name-prefix=calc --verbose --yacc --yyerror-verbose])