forked from zhengqm/DPPL-Proj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.y
86 lines (74 loc) · 1.74 KB
/
float.y
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Float_Functions.h"
extern int least_precision;
int yylex();
#define NVARS 100
char *vars[NVARS]; double vals[NVARS]; int IsvalsDouble[NVARS]; int Isvals64[NVARS];
float var[NVARS];
Float Var[NVARS];
int nvars=0;
int IsTermDouble = 0;
int IsMuldivDouble = 0;
int IsExprDouble = 0;
int line_num = 2;
%}
%union { Float dval; int ivar; char *sval; }
%token <sval> FLOAT
%token <ivar> INT;
%token <ivar> VARIABLE
%token <ivar> FLOAT32;
%token <ivar> FLOAT64;
%token <sval> PRECISION;
%type <dval> expr
%type <dval> muldiv
%type <dval> term
%%
wholeprogram
: PRECISION INT '\n' {printf("Wanted Precision: %d\n", $2); least_precision = $2;} program
;
program
: {printf("\nProcessing Line %d Now...\n", line_num); line_num++;} statement program { }
|
;
statement
: expr '\n' { }
| VARIABLE '=' expr '\n' { Var[$1] = $3; }
;
expr
: expr '+' muldiv { $$ = add($1, $3); printf("this is +\n"); printinfo($$);}//
| expr '-' muldiv { $$ = sub($1, $3); printf("this is -\n"); printinfo($$);}
| muldiv { $$ = $1; }
;
muldiv
: muldiv '*' term { $$ = multiply($1, $3); printf("this is *\n"); printinfo($$);}
| muldiv '/' term { }
| term { $$ = $1; }
;
term
: '(' expr ')' { $$ = $2; }
| VARIABLE { $$ = Var[$1]; printinfo($$); }
| FLOAT ':' INT { $$ = new_float(atof($1), $3); printinfo($$); }
| FLOAT { $$ = new_float(atof($1), 7); printinfo($$); }
;
%%
int varindex(char *varname)
{
int i;
for (i=0; i<nvars; i++)
if (strcmp(varname,vars[i])==0)
return i;
vars[nvars] = strdup(varname);
return nvars++;
//| FLOAT { $$ = new_float(atof($1), 7); printf("bbbbb\n");printinfo($$); }
}
int yyerror(char *s) {
printf("%s\n", s);
}
int main(void)
{
yyparse();
return 0;
}