-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.y
171 lines (140 loc) · 4.6 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
%{
#include <math.h>
#include <stdbool.h>
// bridges
#include <parser.h>
#include <lexer.h>
void yyerror(yyscan_t scanner, const char *msg) {
fprintf(stderr, "Error: %s\n", msg);
}
%}
%define api.pure
%lex-param { void* scanner }
%parse-param { void* scanner }
%code requires {
#include <symstack.h>
}
%union {
int ival;
double fval;
char sval[100];
node* nodeptr;
void* none;
}
%token TOKEN_LPAREN "("
%token TOKEN_RPAREN ")"
%token TOKEN_PLUS "+"
%token TOKEN_STAR "*"
%token TOKEN_HYPH "-"
%token TOKEN_FSLASH "/"
%token TOKEN_CARET "^"
%token TOKEN_LBRACE "{"
%token TOKEN_RBRACE "}"
%token TOKEN_COMMA ","
%token TOKEN_EQUALS "="
/* %token TOKEN_KEYWORD_FN "fn"
%token TOKEN_KEYWORD_END "end" */
%token <ival> TOKEN_INTEGER "INTEGER"
%token <fval> TOKEN_DECIMAL "DECIMAL"
%token <sval> TOKEN_ID "ID"
%type <none> start
%type <none> input
%type <nodeptr> defs
%type <nodeptr> def
%type <nodeptr> expr
%type <nodeptr> statement
%type <nodeptr> args
// literals
%type <fval> number
%left "+"
%left "*"
%left "-"
%left "/"
%left "^"
%%
start:
input {
if (yychar == YYEOF)
{
printf("input parsed\n");
}
}
input:
%empty
| defs expr {
// print_node($2);
double n = evaluate($2, $1);
printf("result: %g\n", n);
}
defs:
%empty
| defs def {
node *top = $2;
node *prev = $1;
top->e.li.next = prev;
$$ = top;
}
def:
"ID" "{" expr "}" {
$$ = newnode((node) {kDef, {.li = {strdup($1), $3, NULL}}});
}
expr:
number {
nVal n = $1;
$$ = newnode((node) {kNum, {n}});
}
| expr "-" expr {
node *left = $1;
node *right = $3;
$$ = newnode((node) {kDiff, {.binary = {left, right}}});
}
| expr "+" expr {
node *left = $1;
node *right = $3;
$$ = newnode((node) {kSum, {.binary = {left, right}}});
}
| expr "/" expr {
node *left = $1;
node *right = $3;
$$ = newnode((node) {kDiv, {.binary = {left, right}}});
}
| expr "*" expr {
node *left = $1;
node *right = $3;
$$ = newnode((node) {kMult, {.binary = {left, right}}});
}
| expr "^" expr {
node *left = $1;
node *right = $3;
$$ = newnode((node) {kPow, {.binary = {left, right}}});
}
| "(" expr ")" {
$$ = $2;
}
| "ID" {
$$ = newnode((node) {kVar, {.id = {strdup($1)}}});
}
| "ID" "(" args ")" {
$$ = newnode((node) {kCall, {.link = {strdup($1), $3}}});
}
args:
%empty {
$$ = NULL;
}
| statement {
$$ = $1;
}
| args "," statement {
node *top = $3;
node *prev = $1;
top->e.li.next = prev;
$$ = top;
}
statement:
"ID" "=" expr {
// $$ = newnode((node) {kLink, {.link = {strdup($1), $3}}});
$$ = newnode((node) {kArg, {.li = {strdup($1), $3, NULL}}});
}
number:
"INTEGER" { $$ = (double) $1; }
| "DECIMAL" { $$ = $1; }