]> git.saurik.com Git - bison.git/blob - tests/calc.m4
Test also `--verbose', `--defines' and `--name-prefix'. Testing
[bison.git] / tests / calc.m4
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)
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 [AT_DATA([calc.y],
28 [[/* Infix notation calculator--calc */
29
30 %{
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #define YYSTYPE int
35
36 static int power (int base, int exponent);
37 static int read_signed_integer (FILE *stream);
38 static void yyerror (const char *s);
39 static int yylex (void);
40 extern void perror (const char *s);
41 %}
42
43 /* BISON Declarations */
44 %token NUM
45 %left '-' '+'
46 %left '*' '/'
47 %left NEG /* negation--unary minus */
48 %right '^' /* exponentiation */
49
50 /* Grammar follows */
51 %%
52 input: /* empty string */
53 | input line
54 ;
55
56 line: '\n'
57 | exp '\n' { printf ("%d", $1); }
58 ;
59
60 exp: NUM { $$ = $1; }
61 | exp '+' exp { $$ = $1 + $3; }
62 | exp '-' exp { $$ = $1 - $3; }
63 | exp '*' exp { $$ = $1 * $3; }
64 | exp '/' exp { $$ = $1 / $3; }
65 | '-' exp %prec NEG { $$ = -$2; }
66 | exp '^' exp { $$ = power ($1, $3); }
67 | '(' exp ')' { $$ = $2; }
68 ;
69 %%
70 /* The input. */
71 FILE *yyin;
72
73 static void
74 yyerror (const char *s)
75 {
76 fprintf (stderr, "%s\n", s);
77 }
78
79 static int
80 read_signed_integer (FILE *stream)
81 {
82 int c = getc (stream);
83 int sign = 1;
84 int n = 0;
85
86 if (c == '-')
87 {
88 c = getc (stream);
89 sign = -1;
90 }
91
92 while (isdigit (c))
93 {
94 n = 10 * n + (c - '0');
95 c = getc (stream);
96 }
97
98 ungetc (c, stream);
99
100 return sign * n;
101 }
102
103 /*---------------------------------------------------------------.
104 | Lexical analyzer returns an integer on the stack and the token |
105 | NUM, or the ASCII character read if not a number. Skips all |
106 | blanks and tabs, returns 0 for EOF. |
107 `---------------------------------------------------------------*/
108
109 static int
110 yylex (void)
111 {
112 int c;
113
114 /* Skip white space. */
115 while ((c = getc (yyin)) == ' ' || c == '\t')
116 ;
117 /* process numbers */
118 if (c == '.' || isdigit (c))
119 {
120 ungetc (c, yyin);
121 yylval = read_signed_integer (yyin);
122 return NUM;
123 }
124 /* Return end-of-file. */
125 if (c == EOF)
126 return 0;
127 /* Return single chars. */
128 return c;
129 }
130
131 static int
132 power (int base, int exponent)
133 {
134 int res = 1;
135 if (exponent < 0)
136 exit (1);
137 for (/* Niente */; exponent; --exponent)
138 res *= base;
139 return res;
140 }
141
142 int
143 main (int argn, const char **argv)
144 {
145 if (argn == 2)
146 yyin = fopen (argv[1], "r");
147 else
148 yyin = stdin;
149
150 if (!stdin)
151 {
152 perror (argv[1]);
153 exit (1);
154 }
155
156 #if YYDEBUG
157 yydebug = 1;
158 #endif
159 yyparse ();
160 return 0;
161 }
162 ]])
163 ])# _AT_DATA_CALC_Y
164
165
166 # AT_DATA_CALC_Y
167 # --------------
168 # Produce `calc.y'.
169 AT_DEFINE([AT_DATA_CALC_Y],
170 [_AT_DATA_CALC_Y($[1], $[2], $[3])])
171
172
173 # _AT_CHECK_CALC(INPUT, OUTPUT, [STDERR])
174 # ---------------------------------------
175 # Run `calc' on INPUT, and expect OUTPUT and STDERR.
176 AT_DEFINE([_AT_CHECK_CALC],
177 [AT_CHECK([echo "$1" | calc], 0, [$2], [$3])])
178
179
180 # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
181 # --------------------------------------------------------
182 # Start a testing chunk which compiles `calc' grammar with
183 # BISON-OPTIONS, and performs several tests over the parser.
184 AT_DEFINE([AT_CHECK_CALC],
185 [# We use integers to avoid dependencies upon the precision of doubles.
186 AT_SETUP([Calculator $1])
187
188 AT_DATA_CALC_Y
189
190 # Specify the output files to avoid problems on different file systems.
191 AT_CHECK([bison calc.y -o calc.c $1], 0, [], [])
192 AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], [])
193
194 # Test the priorities.
195 _AT_CHECK_CALC([1 + 2 * 3], [7], [$2])
196 _AT_CHECK_CALC([1 + 2 * -3], [-5], [$2])
197
198 _AT_CHECK_CALC([-1^2], [-1], [$2])
199 _AT_CHECK_CALC([(-1)^2], [1], [$2])
200
201 _AT_CHECK_CALC([---1], [-1], [$2])
202
203 _AT_CHECK_CALC([1 - 2 - 3], [-4], [$2])
204 _AT_CHECK_CALC([1 - (2 - 3)], [2], [$2])
205
206 _AT_CHECK_CALC([2^2^3], [256], [$2])
207 _AT_CHECK_CALC([(2^2)^3], [64], [$2])
208
209 AT_CLEANUP(calc calc.c calc.h calc.output)
210 ])# AT_CHECK_CALC
211
212
213 # -------------- #
214 # Actual tests. #
215 # -------------- #
216
217
218 AT_CHECK_CALC()
219 # This one is very suspicious. The test fails, but it might be normal.
220 AT_CHECK_CALC([--raw])
221
222 AT_CHECK_CALC([--defines])
223 AT_CHECK_CALC([--name-prefix=calc])
224 AT_CHECK_CALC([--verbose])
225 AT_CHECK_CALC([--yacc])
226 AT_CHECK_CALC([--defines --name-prefix=calc --verbose --yacc])
227
228 # When --debug, a lot of data is sent to STDERR, we can't test it.
229 AT_CHECK_CALC([--debug], ignore)
230 AT_CHECK_CALC([--debug --defines --name-prefix=calc --verbose --yacc], ignore)