-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
124 lines (101 loc) · 2.32 KB
/
parser.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
%{
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
int yylex();
void yyerror(char*s);
%}
%union{
int num;
char sym;
float flo;
long lo;
}
%token ELSE FOR EQ FLOAT LPAREN RPAREN SIN COS TAN SQRT EOL NUMBER PLUS DIV MULT SUB IF ADD LT MT LTEQ MTEQ
%type<num> NUMBER
%type<num> loop Ifelse
%type<lo> Factor Sqr Exp Term
%type<flo> Trig FLOAT
%type<num> Cond
%%
First:
| program First;
program:
Exp EOL {printf("%d\n",$1);}
| Sqr EOL {printf("%d\n", $1); }
| Trig EOL {printf("%f\n", $1); }
| Ifelse EOL {if($1 == 0){
printf(" ");}
else{
printf("%d\n",$1); }
}
| loop EOL {printf("%d\n", $1); }
| EOL;
Exp: Factor
| Exp PLUS Factor { $$ = $1 + $3; }
| Exp SUB Factor { $$ = $1 - $3; }
|
;
Factor: Term
| Factor MULT Term { $$ = $1 * $3; }
| Factor DIV Term { $$ = $1 / $3; }
| Factor MULT Sqr { $$ = $1 * $3; }
| Factor DIV Sqr { $$ = $1 * $3; }
| Sqr {$$ = $1; }
;
Term: NUMBER;
Sqr:
SQRT LPAREN Term RPAREN { $$ = sqrt($3); }
| SQRT LPAREN Exp RPAREN {$$ = sqrt($3); }
| SQRT LPAREN Factor RPAREN {$$ = sqrt($3); };
Trig:
SIN LPAREN FLOAT RPAREN { $$ = sin($3); }
| COS LPAREN FLOAT RPAREN { $$ = cos($3); }
| TAN LPAREN FLOAT RPAREN { $$ = tan($3); }
| SIN LPAREN FLOAT RPAREN Exp { $$ = sin($3) + $5; }
| COS LPAREN FLOAT RPAREN Exp { $$ = cos($3) + $5; }
| TAN LPAREN FLOAT RPAREN Exp { $$ = tan($3) + $5; }
| Exp SIN LPAREN FLOAT RPAREN {$$ = sin($4) + $1; }
| Exp COS LPAREN FLOAT RPAREN {$$ = $1 + cos($4); }
| Exp TAN LPAREN FLOAT RPAREN {$$ = $1 + tan($4); };
Ifelse:
IF LPAREN Cond RPAREN Exp ELSE Exp { if($3){
printf("IF ACCEPTED\n");
($$ = $5);}
else{
($$ = $7); };
}
| IF LPAREN Cond RPAREN Exp{ if($3){
($$ = $5);}
else{
printf("NOPE");};
};
loop:
FOR LPAREN NUMBER RPAREN Exp PLUS Exp {
for(int i = 0; i < $3; i++){
$5 = $5 + $7; };
($$ = $5);
};
Cond:
NUMBER EQ NUMBER { if($1 == $3){
($$ = 1);}
else{
($$ = 0); };
}
| NUMBER LTEQ NUMBER { if($1 <= $3){
($$ = 1);}
else{
($$ = 0);};
}
| NUMBER MTEQ NUMBER { if($1 >= $3){
($$ = 1);}
else{
($$ = 0);};
};
%%
int main(void){
yyparse();
}
void yyerror(char*s){
printf("ERROR: %s\n",s);
}