-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.y
95 lines (77 loc) · 1.63 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
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
extern int yylex();
void yyerror(char*);
typedef union {
double dval;
char str[32];
} un;
#define YYSTYPE un
YYSTYPE yylval;
char* symbols[256];
double values[256];
int top = 0;
void add_symbol(const char* symbol, double value);
double get_symbol(const char* symbol);
%}
%union {
double dval;
char str[32];
}
%token <dval> token_number
%token <str> token_symbol
%type <dval> expr
%type <dval> tt
%type <dval> ff
%%
statement : token_symbol '=' expr '\n' { add_symbol($1, $3); }
| expr '\n' { printf("result: %g\n", $1); }
;
expr : expr '+' tt { $$ = $1 + $3; }
| expr '-' tt { $$ = $1 - $3; }
| tt { $$ = $1; }
;
tt : tt '*' ff { $$ = $1 * $3; }
| tt '/' ff { $$ = $1 / $3; }
| ff { $$ = $1; }
;
ff : '(' expr ')' { $$ = $2; }
| '-' ff { $$ = -$2; }
| token_number { $$ = $1; }
| token_symbol { $$ = get_symbol($1); }
;
%%
void add_symbol(const char* symbol, double value) {
for (int i = 0; i < top; ++i) {
if (!strcmp(symbols[i], symbol)) {
values[i] = value;
return;
}
}
symbols[top] = (char*)malloc(sizeof(char));
strcpy(symbols[top], symbol);
values[top] = value;
top++;
}
double get_symbol(const char* symbol) {
for (int i = 0; i < top; ++i) {
if (!strcmp(symbols[i], symbol)) {
return values[i];
}
}
yyerror("symbol not exist");
return 0.0;
}
void yyerror(char *str) {
extern char* yytext;
if (!strcmp(yytext, "\n")) return;
printf("Error: %s\n", str);
}
int main() {
while (1) {
yyparse();
}
return 0;
}