-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyacc.y
51 lines (43 loc) · 1.22 KB
/
yacc.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
%{
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#define YYSTYPE double
extern int yylex(void); // 不加该行可能报错
void yyerror (char* s); //c语言规范,先声明再实现。函数在后面实现。
%}
%token NUM PLUS MINUS TIMES DIVIDE MOD POWER LEFT RIGHT EOL EXIT
%start input
%%
input :
| input line
;
line : EOL { YYACCEPT; }
| EXIT { YYACCEPT; }
| expr EOL { printf("%lf\n", $1); }
;
expr : expr PLUS term { $$ = $1 + $3; }
| expr MINUS term { $$ = $1 - $3; }
| term
;
term : term TIMES factor { $$ = $1 * $3; }
| term DIVIDE factor { $$ = $1 / $3; }
| term MOD factor { $$ = (long)$1 % (long)$3; }
| factor
;
factor : factor POWER exponent { $$ = pow($1, $3); }
| exponent
exponent : LEFT expr RIGHT { $$ = $2; }
| NUM
;
%%
#include "lex.yy.c"
int main(void)
{
yyparse();
return 0;
}
void yyerror (char* s)
{
printf ("%s\n", s);
}