+]])
+
+AT_DATA_GRAMMAR([calc.y],
+[[/* Infix notation calculator--calc */
+]$4
+AT_SKEL_CC_IF(
+[%define global_tokens_and_yystype])[
+%code requires
+{
+]AT_LOCATION_TYPE_IF([[
+# include <iostream>
+ struct Point
+ {
+ int l;
+ int c;
+ };
+
+ struct Span
+ {
+ Point first;
+ Point last;
+ };
+
+# define YYLLOC_DEFAULT(Current, Rhs, N) \
+ do \
+ if (N) \
+ { \
+ (Current).first = YYRHSLOC (Rhs, 1).first; \
+ (Current).last = YYRHSLOC (Rhs, N).last; \
+ } \
+ else \
+ { \
+ (Current).first = (Current).last = YYRHSLOC (Rhs, 0).last; \
+ } \
+ while (false)
+
+]])[
+ /* 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;
+ extern semantic_value global_result;
+ extern int global_count;
+}
+
+%code
+{
+#include <assert.h>
+#include <string.h>
+#define USE(Var)
+
+FILE *input;
+static int power (int base, int exponent);
+
+]AT_SKEL_CC_IF(,
+[static void yyerror (AT_YYERROR_ARG_LOC_IF([AT_YYLTYPE *llocp, ])
+ AT_PARAM_IF([semantic_value *result, int *count, ])
+ const char *s
+ );])[
+]AT_YYLEX_DECLARE_EXTERN[
+}
+
+]AT_SKEL_CC_IF([AT_LOCATION_IF([AT_LOCATION_TYPE_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 ();
+}
+])])])[
+
+/* Bison Declarations */
+%token CALC_EOF 0 "end of input"
+%token <ival> NUM "number"
+%type <ival> exp
+
+%nonassoc '=' /* comparison */
+%left '-' '+'
+%left '*' '/'
+%precedence 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; }
+;
+%%