10 ## ---------------------------------------------------- ##
11 ## Compile the grammar described in the documentation. ##
12 ## ---------------------------------------------------- ##
15 # ------------------------- #
16 # Helping Autotest macros. #
17 # ------------------------- #
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.
26 AT_DEFINE([_AT_DATA_CALC_Y],
29 [errprint([$0: Invalid arguments: $@
32 [[/* Infix notation calculator--calc */
40 static int power (int base, int exponent);
41 static void yyerror (const char *s);
42 static int yylex (void);
43 extern void perror (const char *s);
46 /* BISON Declarations */
49 %nonassoc '=' /* comparison */
52 %left NEG /* negation--unary minus */
53 %right '^' /* exponentiation */
72 printf ("calc: error: %d != %d\n", $1, $3);
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; }
88 yyerror (const char *s)
91 fprintf (stderr, "%d.%d:%d.%d: ",
92 yylloc.first_line, yylloc.first_column,
93 yylloc.last_line, yylloc.last_column);
95 fprintf (stderr, "%s\n", s);
101 int res = getc (yyin);
106 yylloc.last_column = 0;
109 yylloc.last_column++;
119 /* Wrong when C == `\n'. */
120 yylloc.last_column--;
126 read_signed_integer (void)
140 n = 10 * n + (c - '0');
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 `---------------------------------------------------------------*/
163 yylloc.first_column = yylloc.last_column;
164 yylloc.first_line = yylloc.last_line;
167 /* Skip white space. */
168 while ((c = yygetc ()) == ' ' || c == '\t')
171 yylloc.first_column = yylloc.last_column;
172 yylloc.first_line = yylloc.last_line;
176 /* process numbers */
177 if (c == '.' || isdigit (c))
180 yylval = read_signed_integer ();
184 /* Return end-of-file. */
188 /* Return single chars. */
193 power (int base, int exponent)
198 for (/* Niente */; exponent; --exponent)
204 main (int argn, const char **argv)
207 yyin = fopen (argv[1], "r");
221 yylloc.last_column = 0;
222 yylloc.last_line = 1;
231 # AT_DATA_CALC_Y([BISON-OPTIONS])
232 # -------------------------------
234 AT_DEFINE([AT_DATA_CALC_Y],
235 [_AT_DATA_CALC_Y($[1], $[2], $[3],
236 [ifelse(regexp([$1], [--yyerror-verbose]),
238 [[#define YYERROR_VERBOSE]])])])
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.
248 AT_DEFINE([_AT_CHECK_CALC],
249 [ifelse(regexp([$1], [--debug]),
251 [AT_CHECK([echo "$2" | calc],
253 [AT_CHECK([echo "$2" | calc 2>&1 >/dev/null | grep 'parse error' >&2],
257 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT,
258 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
259 # ------------------------------------------------------------
260 # Run `calc' on INPUT, and expect STDERR.
261 AT_DEFINE([_AT_CHECK_CALC_ERROR],
262 [AT_CHECK([echo "$2" | calc 2>&1 >/dev/null | grep 'parse error' >&2], 0,
264 [ifelse(regexp([$1], [--location]),
265 [-1], [], [$3: ])[]dnl
267 ifelse(regexp([$1], [--yyerror-verbose]),
273 # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
274 # --------------------------------------------------------
275 # Start a testing chunk which compiles `calc' grammar with
276 # BISON-OPTIONS, and performs several tests over the parser.
277 AT_DEFINE([AT_CHECK_CALC],
278 [# We use integers to avoid dependencies upon the precision of doubles.
279 AT_SETUP([Calculator $1])
283 # Specify the output files to avoid problems on different file systems.
284 AT_CHECK([bison calc.y -o calc.c patsubst([$1], [--yyerror-verbose])],
286 AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], [])
288 # Test the priorities.
305 _AT_CHECK_CALC_ERROR([$1], [+1],
307 [, unexpected `'+''])
308 _AT_CHECK_CALC_ERROR([$1], [1//2],
310 [, unexpected `'/'', expecting `NUM' or `'-'' or `'(''])
311 _AT_CHECK_CALC_ERROR([$1], [error],
313 [, unexpected `$undefined.'])
314 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3],
316 [, unexpected `'=''])
317 _AT_CHECK_CALC_ERROR([$1],
321 [, unexpected `'+''])
323 AT_CLEANUP(calc calc.c calc.h calc.output)
329 # ------------------ #
330 # Test the parsers. #
331 # ------------------ #
334 # This one is very suspicious. The test fails, but it might be normal.
335 AT_CHECK_CALC([--raw])
337 AT_CHECK_CALC([--defines])
338 AT_CHECK_CALC([--locations])
339 AT_CHECK_CALC([--name-prefix=calc])
340 AT_CHECK_CALC([--verbose])
341 AT_CHECK_CALC([--yacc])
342 AT_CHECK_CALC([--yyerror-verbose])
344 AT_CHECK_CALC([--locations --yyerror-verbose])
346 AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
348 AT_CHECK_CALC([--debug])
349 AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])