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 extern void perror (const char *s);
42 /* BISON Declarations */
46 %left NEG /* negation--unary minus */
47 %right '^' /* exponentiation */
51 input: /* empty string */
56 | exp '\n' { printf ("%d", $1); }
60 | exp '+' exp { $$ = $1 + $3; }
61 | exp '-' exp { $$ = $1 - $3; }
62 | exp '*' exp { $$ = $1 * $3; }
63 | exp '/' exp { $$ = $1 / $3; }
64 | '-' exp %prec NEG { $$ = -$2; }
65 | exp '^' exp { $$ = power ($1, $3); }
66 | '(' exp ')' { $$ = $2; }
73 yyerror (const char *s)
75 fprintf (stderr, "%s\n", s);
79 read_signed_integer (FILE *stream)
81 int c = getc (stream);
93 n = 10 * n + (c - '0');
102 /*---------------------------------------------------------------.
103 | Lexical analyzer returns an integer on the stack and the token |
104 | NUM, or the ASCII character read if not a number. Skips all |
105 | blanks and tabs, returns 0 for EOF. |
106 `---------------------------------------------------------------*/
113 /* Skip white space. */
114 while ((c = getc (yyin)) == ' ' || c == '\t')
116 /* process numbers */
117 if (c == '.' || isdigit (c))
120 yylval = read_signed_integer (yyin);
123 /* Return end-of-file. */
126 /* Return single chars. */
131 power (int base, int exponent)
136 for (/* Niente */; exponent; --exponent)
142 main (int argn, const char **argv)
145 yyin = fopen (argv[1], "r");
168 AT_DEFINE([AT_DATA_CALC_Y],
169 [_AT_DATA_CALC_Y($[1], $[2], $[3])])
172 # _AT_CHECK_CALC(INPUT, OUTPUT, [STDERR])
173 # ---------------------------------------
174 # Run `calc' on INPUT, and expect OUTPUT and STDERR.
175 AT_DEFINE([_AT_CHECK_CALC],
176 [AT_CHECK([echo "$1" | calc], 0, [$2], [$3])])
179 # AT_CHECK_CALC(TITLE, [BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
180 # ---------------------------------------------------------------
181 # Start a testing chunk named TITLE which compiles `calc' grammar with
182 # BISON-OPTIONS, and performs several tests over the parser.
183 AT_DEFINE([AT_CHECK_CALC],
184 [# We use integers to avoid dependencies upon the precision of doubles.
189 # Specify the output files to avoid problems on different file systems.
190 AT_CHECK([bison calc.y -o calc.c $2], 0, [], [])
191 AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], [])
193 # Test the priorities.
194 _AT_CHECK_CALC([1 + 2 * 3], [7], [$3])
195 _AT_CHECK_CALC([1 + 2 * -3], [-5], [$3])
197 _AT_CHECK_CALC([-1^2], [-1], [$3])
198 _AT_CHECK_CALC([(-1)^2], [1], [$3])
200 _AT_CHECK_CALC([---1], [-1], [$3])
202 _AT_CHECK_CALC([1 - 2 - 3], [-4], [$3])
203 _AT_CHECK_CALC([1 - (2 - 3)], [2], [$3])
205 _AT_CHECK_CALC([2^2^3], [256], [$3])
206 _AT_CHECK_CALC([(2^2)^3], [64], [$3])
208 AT_CLEANUP(calc calc.c)
217 AT_CHECK_CALC([Simple calculator])
219 AT_CHECK_CALC([Simple Yacc compatible calculator],
222 AT_CHECK_CALC([Simple calculator whose tokens are numbered from 3],
225 AT_CHECK_CALC([Simple debugging calculator],
228 AT_CHECK_CALC([Simple Yacc compatible debugging calculator],
229 [--debug --yacc], ignore)