10 ## ---------------------------------------------------- ##
11 ## Compile the grammar described in the documentation. ##
12 ## ---------------------------------------------------- ##
15 # ------------------------- #
16 # Helping Autotest macros. #
17 # ------------------------- #
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],
28 [[/* Infix notation calculator--calc */
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);
43 /* BISON Declarations */
47 %left NEG /* negation--unary minus */
48 %right '^' /* exponentiation */
52 input: /* empty string */
57 | exp '\n' { printf ("%d", $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; }
74 yyerror (const char *s)
76 fprintf (stderr, "%s\n", s);
80 read_signed_integer (FILE *stream)
82 int c = getc (stream);
94 n = 10 * n + (c - '0');
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 `---------------------------------------------------------------*/
114 /* Skip white space. */
115 while ((c = getc (yyin)) == ' ' || c == '\t')
117 /* process numbers */
118 if (c == '.' || isdigit (c))
121 yylval = read_signed_integer (yyin);
124 /* Return end-of-file. */
127 /* Return single chars. */
132 power (int base, int exponent)
137 for (/* Niente */; exponent; --exponent)
143 main (int argn, const char **argv)
146 yyin = fopen (argv[1], "r");
169 AT_DEFINE([AT_DATA_CALC_Y],
170 [_AT_DATA_CALC_Y($[1], $[2], $[3])])
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])])
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])
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, [], [])
194 # Test the priorities.
195 _AT_CHECK_CALC([1 + 2 * 3], [7], [$2])
196 _AT_CHECK_CALC([1 + 2 * -3], [-5], [$2])
198 _AT_CHECK_CALC([-1^2], [-1], [$2])
199 _AT_CHECK_CALC([(-1)^2], [1], [$2])
201 _AT_CHECK_CALC([---1], [-1], [$2])
203 _AT_CHECK_CALC([1 - 2 - 3], [-4], [$2])
204 _AT_CHECK_CALC([1 - (2 - 3)], [2], [$2])
206 _AT_CHECK_CALC([2^2^3], [256], [$2])
207 _AT_CHECK_CALC([(2^2)^3], [64], [$2])
209 AT_CLEANUP(calc calc.c calc.h calc.output)
219 # This one is very suspicious. The test fails, but it might be normal.
220 AT_CHECK_CALC([--raw])
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])
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)