/* Infix notation calculator--calc */ %{ #include /* We don't need perfect functions for these tests. */ #undef malloc #undef memcmp #undef realloc #include #if STDC_HEADERS # include # include #else char *strcat(char *dest, const char *src); #endif #include #include extern void perror (const char *s); /* Exercise pre-prologue dependency to %union. */ typedef int value_t; value_t global_result = 0; int global_count = 0; %} %parse-param "value_t *result", "result" %parse-param "int *count", "count" /* Exercise %union. */ %union { value_t ival; }; %{ #if YYPURE # define LOC (*yylloc) # define VAL (*yylval) #else # define LOC (yylloc) # define VAL (yylval) #endif #if YYPURE # if YYLSP_NEEDED # define LEX_FORMALS YYSTYPE *yylval, YYLTYPE *yylloc # define LEX_ARGS yylval, yylloc # define USE_LEX_ARGS (void) yylval; (void) yylloc; # else # define LEX_FORMALS YYSTYPE *yylval # define LEX_ARGS yylval # define USE_LEX_ARGS (void) yylval # endif # define LEX_PRE_FORMALS LEX_FORMALS, # define LEX_PRE_ARGS LEX_ARGS, #else # define LEX_FORMALS void # define LEX_PRE_FORMALS # define LEX_ARGS # define LEX_PRE_ARGS # define USE_LEX_ARGS #endif static int power (int base, int exponent); static void yyerror (const char *s); static int yylex (LEX_FORMALS); static int yygetc (LEX_FORMALS); static void yyungetc (LEX_PRE_FORMALS int c); %} /* Bison Declarations */ %token CALC_EOF 0 "end of input" %token NUM "number" %type exp %nonassoc '=' /* comparison */ %left '-' '+' %left '*' '/' %left NEG /* negation--unary minus */ %right '^' /* exponentiation */ /* Grammar follows */ %% input: line | input line { ++*count; ++global_count; } ; line: '\n' | exp '\n' { *result = global_result = $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 ')' { $$ = 0; } ; %% /* The input. */ FILE *yyin; static void yyerror (const char *s) { #if YYLSP_NEEDED fprintf (stderr, "%d.%d-%d.%d: ", LOC.first_line, LOC.first_column, LOC.last_line, LOC.last_column); #endif fprintf (stderr, "%s\n", s); } #if YYLSP_NEEDED static YYLTYPE last_yylloc; #endif static int yygetc (LEX_FORMALS) { int res = getc (yyin); USE_LEX_ARGS; #if YYLSP_NEEDED last_yylloc = LOC; if (res == '\n') { LOC.last_line++; LOC.last_column = 1; } else LOC.last_column++; #endif return res; } static void yyungetc (LEX_PRE_FORMALS int c) { USE_LEX_ARGS; #if YYLSP_NEEDED /* Wrong when C == `\n'. */ LOC = last_yylloc; #endif ungetc (c, yyin); } static int read_signed_integer (LEX_FORMALS) { int c = yygetc (LEX_ARGS); int sign = 1; int n = 0; USE_LEX_ARGS; if (c == '-') { c = yygetc (LEX_ARGS); sign = -1; } while (isdigit (c)) { n = 10 * n + (c - '0'); c = yygetc (LEX_ARGS); } yyungetc (LEX_PRE_ARGS c); return sign * n; } /*---------------------------------------------------------------. | Lexical analyzer returns an integer on the stack and the token | | NUM, or the ASCII character read if not a number. Skips all | | blanks and tabs, returns 0 for EOF. | `---------------------------------------------------------------*/ static int yylex (LEX_FORMALS) { static int init = 1; int c; if (init) { init = 0; #if YYLSP_NEEDED LOC.last_column = 1; LOC.last_line = 1; #endif } #if YYLSP_NEEDED LOC.first_column = LOC.last_column; LOC.first_line = LOC.last_line; #endif /* Skip white space. */ while ((c = yygetc (LEX_ARGS)) == ' ' || c == '\t') { #if YYLSP_NEEDED LOC.first_column = LOC.last_column; LOC.first_line = LOC.last_line; #endif } /* process numbers */ if (c == '.' || isdigit (c)) { yyungetc (LEX_PRE_ARGS c); VAL.ival = read_signed_integer (LEX_ARGS); return NUM; } /* Return end-of-file. */ if (c == EOF) return CALC_EOF; /* Return single chars. */ return c; } static int power (int base, int exponent) { int res = 1; if (exponent < 0) exit (1); for (/* Niente */; exponent; --exponent) res *= base; return res; } int main (int argc, const char **argv) { value_t result = 0; int count = 0; yyin = NULL; if (argc == 2) yyin = fopen (argv[1], "r"); else yyin = stdin; if (!yyin) { perror (argv[1]); exit (1); } #if YYDEBUG yydebug = 1; #endif yyparse (&result, &count); assert (global_result == result); assert (global_count == count); return 0; }