-
Notifications
You must be signed in to change notification settings - Fork 8
/
calc.y
62 lines (60 loc) · 1.47 KB
/
calc.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
%{
#include <stdio.h>
#include <string.h>
#include <math.h>
int yylex();
%}
%token NB ADD MULT MINUS DIV EQUAL LBR RBR EXIT ABS OPPOSE
%%
expr_calcs: expr_calc | expr_calcs expr_calc;
expr_calc: expr_num EQUAL
{
printf("%d\n\n", $$);
}
| EXIT {YYACCEPT;}
| error '\n' {yyerrok;}
| error EQUAL {yyerrok;};
expr_num: facteur
| expr_num ADD facteur
{
$$=$1+$3;
}
| expr_num MINUS facteur
{
$$=$1-$3;
};
facteur: terme
| facteur MULT terme
{
$$=$1*$3;
}
| facteur DIV terme
{
$$=$1/$3;
};
terme: NB { $$=$1;} | LBR expr_num RBR {$$=$2;} | ABS expr_num {$$=fabs($2);}| OPPOSE expr_num {$$=-($2);};
%%
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap(void)
{
return 1;
}
int main(void)
{
// if (argc != 2){
// printf("Usage:\t %s FolderToCopy\n", argv[0]);
// return 1;
// }
// yyin=fopen(argv[1],"r");
// if (yyin==NULL){
// printf("Cannont open <%s>.\n", argv[1]);
// return 2;
// }
printf("Hello and welcome into Yacc Calculator. Enter your expression using '=' to have the result (spaces unsupported). Exit by typing 'exit'.\n\n");
yyparse();
printf("\n");
return 0;
}