42 lines
1.3 KiB
Plaintext
42 lines
1.3 KiB
Plaintext
%{
|
|
#include <stdio.h> /* For printf, etc. */
|
|
#include <math.h> /* For pow, used in the grammar. */
|
|
#include "calc.h" /* Contains definition of 'symrec'. */
|
|
int yylex (void);
|
|
void yyerror (char const *);
|
|
%}
|
|
%define api.value.type union /* Generate YYSTYPE from these types: */
|
|
%token <double> NUM /* Double precision number. */
|
|
%token <symrec*> VAR FUN /* Symbol table pointer: variable/function. */
|
|
%nterm <double> exp
|
|
%precedence '='
|
|
%left '-' '+'
|
|
%left '*' '/'
|
|
%precedence NEG /* negation--unary minus */
|
|
%right '^' /* exponentiation */
|
|
%% /* The grammar follows. */
|
|
input:
|
|
%empty
|
|
| input line
|
|
;
|
|
line:
|
|
'\n'
|
|
| exp '\n' { printf ("%.10g\n", $1); }
|
|
| error '\n' { yyerrok; }
|
|
;
|
|
exp:
|
|
NUM
|
|
| VAR { $$ = $1->value.var; }
|
|
| VAR '=' exp { $$ = $3; $1->value.var = $3; }
|
|
| FUN '(' exp ')' { $$ = $1->value.fun ($3); }
|
|
| exp '+' exp { $$ = $1 + $3; }
|
|
| exp '-' exp { $$ = $1 - $3; }
|
|
| exp '*' exp { $$ = $1 * $3; }
|
|
| exp '/' exp { $$ = $1 / $3; }
|
|
| '-' exp %prec NEG { $$ = -$2; }
|
|
| exp '^' exp { $$ = pow ($1, $3); }
|
|
| '(' exp ')' { $$ = $2; }
|
|
;
|
|
/* End of grammar. */
|
|
%%
|