-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.c
195 lines (153 loc) · 3.3 KB
/
parser.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "rdp_calc.h"
// Grammar:
// <expression> ::= <term> {("+" | "-") <term>}
// <term> ::= <factor> {("*" | "/") <factor>}
// <factor> ::= <number> | "(" <expression> ")"
// <number> ::= positive integer
// global variable holding the current Symbol
Node *Symp;
// Some expressions have many syntax errors, do not print them all
// print only the first error encountered, and set error_free to 0
bool error_free;
// Parser function
ASTreeNode_t *parser(lexer_t *tokens)
{
initSymp;
error_free = 1;
ASTreeNode_t *ASTree = expression();
if (ASTree == NULL || (Symp && !isEnd)) {
ast_clear(ASTree);
if (Symp)
unexpect("parser");
return (NULL);
}
return ASTree;
}
int accept(token_t token)
{
if (Sym == token) {
nextSymp;
return 1;
}
return 0;
}
int expect(token_t expected_token)
{
if (!accept(expected_token)) {
// check if this is the first error in the expression
if (!error_free)
return 0;
// if so, process the error
if (Sym == endofexpr || Sym == number)
error("expect: found %s when expecting a '%s'\n", lexeme[Sym], lexeme[expected_token]);
else
error("expect: found '%s' when expecting a '%s'\n", lexeme[Sym], lexeme[expected_token]);
// lock: set error_free to 0
error_free = 0;
return 0;
}
return 1;
}
void unexpect(char *msg)
{
// check if this is the first error in the expression
if (!error_free)
return ;
// if so, process the error
if (msg && *msg)
error("%s: ", msg);
error("unexpected token '%s' ", lexeme[Sym]);
if (Symp->prev)
error("after '%s'\n", lexeme[Symp->prev->tok]);
else
error("in the beginning of the expression\n");
// lock: set error_free to 0
error_free = 0;
}
// <factor> ::= <number> | "(" <expression> ")"
ASTreeNode_t *factor()
{
ASTreeNode_t *tree = NULL;
if (!Symp)
return NULL;
if (Sym == number) {
tree = ast_new(Sym, Val, NULL, NULL);
nextSymp;
return tree;
}
else if (accept(opar)) {
tree = expression();
if (tree == NULL || !expect(cpar)) {
ast_clear(tree);
return NULL;
}
return tree;
}
else
{
unexpect("factor");
return NULL;
}
}
// <term> ::= <factor> {("*" | "/") <factor>}
ASTreeNode_t *term()
{
ASTreeNode_t *tree = NULL;
if (!Symp)
return NULL;
tree = factor();
if (tree == NULL) {
return NULL;
}
while (!isEnd && (Sym == mult || Sym == div_)) {
tree = ast_new(Sym, none, tree, NULL);
nextSymp;
tree->right = factor();
if (tree->right == NULL) {
ast_clear(tree);
return NULL;
}
}
return tree;
}
// <expression> ::= <term> {("+" | "-") <term>}
ASTreeNode_t *expression()
{
ASTreeNode_t *tree = NULL;
if (!Symp)
return NULL;
tree = term();
if (tree == NULL) {
return NULL;
}
while (!isEnd && (Sym == plus || Sym == minus)) {
tree = ast_new(Sym, none, tree, NULL);
nextSymp;
tree->right = term();
if (tree->right == NULL) {
ast_clear(tree);
return NULL;
}
}
return tree;
}
// Constructor function
ASTreeNode_t *ast_new(token_t type, int value, ASTreeNode_t *left, ASTreeNode_t *right)
{
ASTreeNode_t *node;
node = (ASTreeNode_t *)malloc(sizeof(ASTreeNode_t));
node->type = type;
node->value = value;
node->left = left;
node->right = right;
return node;
}
// Destructor function
void ast_clear(ASTreeNode_t *tree)
{
if (tree == NULL)
return ;
ast_clear(tree->left);
ast_clear(tree->right);
free(tree);
}