+]])
+
+AT_DATA_GRAMMAR([calc.y],
+[[/* Infix notation calculator--calc */
+]$4
+AT_SKEL_CC_IF(
+[%define global_tokens_and_yystype])[
+%code requires {
+/* Exercise pre-prologue dependency to %union. */
+typedef int semantic_value;
+}
+
+/* Exercise %union. */
+%union
+{
+ semantic_value ival;
+};
+
+%code provides {
+#include <stdio.h>
+/* The input. */
+extern FILE *input;]AT_SKEL_CC_IF([[
+#ifndef YYLTYPE
+# define YYLTYPE ]AT_NAME_PREFIX[::location
+#endif
+#define first_line begin.line
+#define first_column begin.column
+#define last_line end.line
+#define last_column end.column]])[
+}
+
+%code {
+#include <stdlib.h>
+#include <string.h>
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#else
+# undef alarm
+# define alarm(seconds) /* empty */
+#endif
+#define USE(Var)
+
+FILE *input;
+static semantic_value global_result = 0;
+static int global_count = 0;
+static int power (int base, int exponent);
+]AT_SKEL_CC_IF(,
+[/* yyerror receives the location if:
+ - %location & %pure & %glr
+ - %location & %pure & %yacc & %parse-param. */
+static void yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
+ AT_PARAM_IF([semantic_value *result, int *count, ])
+ const char *s
+ );])[
+int yylex (]AT_LEX_FORMALS[);
+}
+
+]AT_SKEL_CC_IF(
+[/* The lalr1.cc skeleton, for backward compatibility, defines
+ a constructor for position that initializes the filename. The
+ glr.cc skeleton does not (and in fact cannot: location/position
+ are stored in a union, from which objects with constructors are
+ excluded in C++. */
+%initial-action {
+ @$.initialize (0);
+}
+])[
+
+/* Bison Declarations */
+%token CALC_EOF 0 "end of input"
+%token <ival> NUM "number"
+%type <ival> exp
+
+%nonassoc '=' /* comparison */
+%left '-' '+'
+%left '*' '/'
+%left NEG /* negation--unary minus */
+%right '^' /* exponentiation */
+
+/* Grammar follows */
+%%
+input:
+ line
+| input line { ]AT_PARAM_IF([++*count; ++global_count;])[ }
+;
+
+line:
+ '\n'
+| exp '\n' { ]AT_PARAM_IF([*result = global_result = $1], [USE ($1)])[; }
+;
+
+exp:
+ NUM { $$ = $1; }
+| exp '=' exp
+ {
+ if ($1 != $3)
+ fprintf (stderr, "calc: error: %d != %d\n", $1, $3);
+ $$ = $1;
+ }
+| exp '+' exp { $$ = $1 + $3; }
+| exp '-' exp { $$ = $1 - $3; }
+| exp '*' exp { $$ = $1 * $3; }
+| exp '/' exp { $$ = $1 / $3; }
+| '-' exp %prec NEG { $$ = -$2; }
+| exp '^' exp { $$ = power ($1, $3); }
+| '(' exp ')' { $$ = $2; }
+| '(' error ')' { $$ = 1111; yyerrok; }
+| '!' { $$ = 0; YYERROR; }
+| '-' error { $$ = 0; YYERROR; }
+;
+%%
+
+]AT_SKEL_CC_IF(
+[/* A C++ error reporting function. */
+void
+AT_NAME_PREFIX::parser::error (const location& l, const std::string& m)
+{
+ (void) l;
+ std::cerr << AT_LOCATION_IF([l << ": " << ])m << std::endl;
+}
+
+int
+yyparse (AT_PARAM_IF([semantic_value *result, int *count]))
+{
+ AT_NAME_PREFIX::parser parser[]AT_PARAM_IF([ (result, count)]);
+#if YYDEBUG
+ parser.set_debug_level (1);
+#endif
+ return parser.parse ();
+}
+],
+[static void
+yyerror (AT_YYERROR_ARG_LOC_IF([YYLTYPE *llocp, ])
+ AT_PARAM_IF([semantic_value *result, int *count, ])
+ const char *s)
+{
+AT_PARAM_IF([(void) result; (void) count;])
+AT_YYERROR_SEES_LOC_IF([
+ fprintf (stderr, "%d.%d",
+ AT_LOC.first_line, AT_LOC.first_column);
+ if (AT_LOC.first_line != AT_LOC.last_line)
+ fprintf (stderr, "-%d.%d",
+ AT_LOC.last_line, AT_LOC.last_column - 1);
+ else if (AT_LOC.first_column != AT_LOC.last_column - 1)
+ fprintf (stderr, "-%d",
+ AT_LOC.last_column - 1);
+ fprintf (stderr, ": ");])
+ fprintf (stderr, "%s\n", s);
+}])[
+
+]AT_DEFINES_IF(, [AT_CALC_LEX])[