-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcalc.no_reduction.y
45 lines (40 loc) · 921 Bytes
/
calc.no_reduction.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
%{
#include <stdio.h>
#include <string.h>
#include <math.h>
int yylex();
%}
%token NB ADD MULT MINUS DIV EQUAL LBR RBR EXIT OPPOSE
%%
exprs: expr | exprs expr;
expr:
calc EQUAL {printf("%d\n\n", $$);} |
EXIT {YYACCEPT;} |
error '\n' {yyerrok;} |
error EQUAL {yyerrok;};
calc:
term |
calc ADD term {$$=$1+$3;} |
calc MINUS term {$$=$1-$3;} |
calc MULT term {$$=$1*$3;} |
calc DIV term {$$=$1/$3;};
term:
|
LBR calc RBR {$$=$2;} |
NB {$$=$1;};
%%
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap(void)
{
return 1;
}
int main(void)
{
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;
}