
%{

typedef struct {
	char	name[32];
	union {
		int    i;
		double d;
		char	*str;
		double *array;
		
%}


%union {
double     val;  /* For returning numbers.                   */
char 	  *str; 
key      *keys;
}

%token <val>  NUM        /* Simple double precision number   */
%type  <val>  exp
%token        BLOCKSTART "K_"
%token <str>  NAME
%token <keys> keylist

%right '='
%left '-' '+'
%left '*' '/'
%left NEG     /* Negation--unary minus */
%right '^'    /* Exponentiation        */

/* Grammar follows */

%%
	
input:    block
		: input block
;

block:  BLOCKSTART NAME keylist { 



input:   /* empty */
        | input line
;

line:
          '\n'
        | exp '\n'   { printf ("\t%.10g\n", $1); }
        | error '\n' { yyerrok;                  }
;

exp:      NUM                { $$ = $1;                         }
        | VAR                { $$ = $1->value.var;              }
        | VAR '=' exp        { $$ = $3; $1->value.var = $3;     }
        | FNCT '(' exp ')'   { $$ = (*($1->value.fnctptr))($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 */
%%
