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 */
42 char *strcat(char *dest, const char *src);
47 static int power (int base, int exponent);
48 static void yyerror (const char *s);
49 static int yylex (void);
50 static int yygetc (void);
51 static void yyungetc (int c);
53 extern void perror (const char *s);
56 /* BISON Declarations */
59 %nonassoc '=' /* comparison */
62 %left NEG /* negation--unary minus */
63 %right '^' /* exponentiation */
82 printf ("calc: error: %d != %d\n", $1, $3);
85 | exp '+' exp { $$ = $1 + $3; }
86 | exp '-' exp { $$ = $1 - $3; }
87 | exp '*' exp { $$ = $1 * $3; }
88 | exp '/' exp { $$ = $1 / $3; }
89 | '-' exp %prec NEG { $$ = -$2; }
90 | exp '^' exp { $$ = power ($1, $3); }
91 | '(' exp ')' { $$ = $2; }
98 yyerror (const char *s)
101 fprintf (stderr, "%d.%d:%d.%d: ",
102 yylloc.first_line, yylloc.first_column,
103 yylloc.last_line, yylloc.last_column);
105 fprintf (stderr, "%s\n", s);
111 int res = getc (yyin);
116 yylloc.last_column = 0;
119 yylloc.last_column++;
129 /* Wrong when C == `\n'. */
130 yylloc.last_column--;
136 read_signed_integer (void)
150 n = 10 * n + (c - '0');
161 /*---------------------------------------------------------------.
162 | Lexical analyzer returns an integer on the stack and the token |
163 | NUM, or the ASCII character read if not a number. Skips all |
164 | blanks and tabs, returns 0 for EOF. |
165 `---------------------------------------------------------------*/
173 yylloc.first_column = yylloc.last_column;
174 yylloc.first_line = yylloc.last_line;
177 /* Skip white space. */
178 while ((c = yygetc ()) == ' ' || c == '\t')
181 yylloc.first_column = yylloc.last_column;
182 yylloc.first_line = yylloc.last_line;
186 /* process numbers */
187 if (c == '.' || isdigit (c))
190 yylval = read_signed_integer ();
194 /* Return end-of-file. */
198 /* Return single chars. */
203 power (int base, int exponent)
208 for (/* Niente */; exponent; --exponent)
214 main (int argn, const char **argv)
217 yyin = fopen (argv[1], "r");
231 yylloc.last_column = 0;
232 yylloc.last_line = 1;
241 # AT_DATA_CALC_Y([BISON-OPTIONS])
242 # -------------------------------
244 AT_DEFINE([AT_DATA_CALC_Y],
245 [_AT_DATA_CALC_Y($[1], $[2], $[3],
246 [ifelse(regexp([$1], [--yyerror-verbose]),
248 [[#define YYERROR_VERBOSE]])])])
252 # _AT_CHECK_CALC(BISON-OPTIONS, INPUT)
253 # ------------------------------------
254 # Run `calc' on INPUT and expect no STDOUT nor STDERR.
255 # If `--debug' is passed to bison, discard all the debugging traces
256 # preserving only the `parse errors'. Note that since there should be
257 # none, the `grep' will fail with exit status 1.
258 AT_DEFINE([_AT_CHECK_CALC],
259 [ifelse(regexp([$1], [--debug]),
261 [AT_CHECK([echo "$2" | calc],
263 [AT_CHECK([echo "$2" | calc 2>&1 >/dev/null | grep 'parse error' >&2],
267 # _AT_CHECK_CALC_ERROR(BISON-OPTIONS, INPUT,
268 # [ERROR-LOCATION], [IF-YYERROR-VERBOSE])
269 # ------------------------------------------------------------
270 # Run `calc' on INPUT, and expect STDERR.
271 AT_DEFINE([_AT_CHECK_CALC_ERROR],
272 [AT_CHECK([echo "$2" | calc 2>&1 >/dev/null | grep 'parse error' >&2], 0,
274 [ifelse(regexp([$1], [--location]),
275 [-1], [], [$3: ])[]dnl
277 ifelse(regexp([$1], [--yyerror-verbose]),
283 # AT_CHECK_CALC([BISON-OPTIONS], [PARSER-EXPECTED-STDERR])
284 # --------------------------------------------------------
285 # Start a testing chunk which compiles `calc' grammar with
286 # BISON-OPTIONS, and performs several tests over the parser.
287 AT_DEFINE([AT_CHECK_CALC],
288 [# We use integers to avoid dependencies upon the precision of doubles.
289 AT_SETUP([Calculator $1])
293 # Specify the output files to avoid problems on different file systems.
294 AT_CHECK([bison calc.y -o calc.c patsubst([$1], [--yyerror-verbose])],
296 AT_CHECK([$CC $CFLAGS calc.c -o calc], 0, [], [])
298 # Test the priorities.
315 _AT_CHECK_CALC_ERROR([$1], [+1],
317 [, unexpected `'+''])
318 _AT_CHECK_CALC_ERROR([$1], [1//2],
320 [, unexpected `'/'', expecting `NUM' or `'-'' or `'(''])
321 _AT_CHECK_CALC_ERROR([$1], [error],
323 [, unexpected `$undefined.'])
324 _AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3],
326 [, unexpected `'=''])
327 _AT_CHECK_CALC_ERROR([$1],
331 [, unexpected `'+''])
333 AT_CLEANUP(calc calc.c calc.h calc.output)
339 # ------------------ #
340 # Test the parsers. #
341 # ------------------ #
345 AT_CHECK_CALC([--defines])
346 AT_CHECK_CALC([--locations])
347 AT_CHECK_CALC([--name-prefix=calc])
348 AT_CHECK_CALC([--verbose])
349 AT_CHECK_CALC([--yacc])
350 AT_CHECK_CALC([--yyerror-verbose])
352 AT_CHECK_CALC([--locations --yyerror-verbose])
354 AT_CHECK_CALC([--defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])
356 AT_CHECK_CALC([--debug])
357 AT_CHECK_CALC([--debug --defines --locations --name-prefix=calc --verbose --yacc --yyerror-verbose])