aboutsummaryrefslogtreecommitdiff
path: root/sem1/osc/mm11/regn/regn.y
blob: d0f67ebd2e0389b69d4f32b4700c2df884ba1066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
%{
#include <stdio.h>
#include <math.h>
#include "symtab.h"
#include <string.h>
%}

%union {
	char           *string;
	double          dval;
}

%token <string> VAR
%token <dval> TAL
%token LOG EXP SQRT VAR_BEGIN

%left '-' '+'
%right LOG EXP SQRT
%left '*' '/'
%right UMINUS

%type <dval> expression

%%

statement_list: statement '\n'
	  | statement_list statement '\n' ;

statement:  expression					{printf("= %f\n",$1);}
	  | VAR_BEGIN VAR '=' expression	{symnode_t *n = sym_lookup($2);
										 if( !n ) {n = sym_insert($2); }
										 n->value = $4;};

expression: expression '+' expression   {$$ = $1 + $3;}
	  | expression '-' expression   {$$ = $1 - $3;}
	  | expression '*' expression   {$$ = $1 * $3;}
	  | expression '/' expression   {if ($3 == 0.0) 
                                           yyerror("divide dy zero");
                                         else $$ = $1 / $3;}
	  | '-' expression %prec UMINUS {$$ =  - $2;}
	  | '(' expression ')'          {$$= $2;}
	  | LOG expression				{$$ = log($2);}
	  | EXP expression				{$$ = exp($2);}
	  | SQRT expression				{$$ = sqrt($2);}
          | TAL                         {$$ = $1;}
	  | VAR							{symnode_t *n = sym_lookup($1);
									 if( !n ) { yyerror("Var not found"); } else { $$ = n->value;} };
%%

int main()
{ 
  yyparse(); 
}