-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcalc.y
49 lines (47 loc) · 1.09 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
%{
#include <stdio.h>
#include <string.h>
int yylex();
%}
%token NB ADD MULT MINUS DIV EQUAL LBR RBR EXIT
%%
expr_calcs: expr_calc | expr_calcs expr_calc;
expr_calc: expr_num EQUAL
{
printf("EQUAL\n");
} | EXIT {YYACCEPT;};
expr_num: facteur
| expr_num ADD facteur
{printf("ADD\n");}
| expr_num MINUS facteur
{printf("MINUS\n");};
facteur: terme
| facteur MULT terme
{printf("MULT\n");}
| facteur DIV terme
{printf("DIV\n");};
terme: NB {printf("Nb = %d \n", $1);} | LBR expr_num RBR {printf("BRACKETS\n");};
%%
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;
// }
yyparse();
printf("\n");
return 0;
}