forked from CS429-S2022/CI-Lab-Student
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.c
More file actions
230 lines (216 loc) · 6.79 KB
/
Copy pathparse.c
File metadata and controls
230 lines (216 loc) · 6.79 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "ci.h"
extern lptr_t this_token, next_token;
extern void init_lexer(void);
extern void advance_lexer(void);
static const char *VALID_FMTS = "dxXbB";
static const int NUM_RESERVED_IDS = 2;
static const struct {
char *id;
token_t t;
} reserved_ids[] = {
{"true", TOK_TRUE},
{"false", TOK_FALSE}
};
bool is_binop(token_t t) {
return t >= TOK_PLUS && t <= TOK_EQ;
}
bool is_unop(token_t t) {
return t >= TOK_UMINUS && t <= TOK_NOT;
}
bool id_is_fmt_spec(char *s) {
return strlen(s) == 1 && strspn(s, VALID_FMTS) == 1;
}
static token_t check_reserved_ids(char *s) {
for (int i = 0; i < NUM_RESERVED_IDS; i++)
if (strcmp(reserved_ids[i].id, s) == 0) return reserved_ids[i].t;
return TOK_INVALID;
}
static node_t *build_leaf(void) {
node_t *ret = calloc(1, sizeof(node_t));
if (! ret) {
logging(LOG_FATAL, "failed to allocate node");
return NULL;
}
ret->node_type = NT_LEAF;
ret->type = NO_TYPE;
ret->tok = this_token->ttype;
switch (this_token->ttype) {
case TOK_NUM:
ret->val.ival = atoi(this_token->repr);
ret->type = INT_TYPE;
break;
case TOK_TRUE:
case TOK_FALSE:
ret->val.bval = this_token->ttype == TOK_TRUE;
ret->type = BOOL_TYPE;
break;
case TOK_STR:
ret->val.sval = (char *) malloc(strlen(this_token->repr) + 1);
if (! ret->val.sval) {
free(ret);
logging(LOG_FATAL, "failed to allocate string");
return NULL;
}
strcpy(ret->val.sval, this_token->repr);
ret->type = STRING_TYPE;
break;
case TOK_FMT_SPEC:
ret->val.fval = this_token->repr[0];
ret->type = FMT_TYPE;
break;
case TOK_ID:
ret->val.sval = (char *) malloc(strlen(this_token->repr) + 1);
if (! ret->val.sval) {
free(ret);
logging(LOG_FATAL, "failed to allocate string");
return NULL;
}
strcpy(ret->val.sval, this_token->repr);
ret->type = ID_TYPE;
break;
default:
logging(LOG_ERROR, "unknown token type");
free(ret); ret = NULL;
break;
}
return ret;
}
static node_t *build_exp(void) {
// check running status
if (terminate || ignore_input) return NULL;
token_t t;
if (this_token->ttype == TOK_NUM) // P5 -> P23
return build_leaf();
if (this_token->ttype == TOK_STR) // P5 -> P??
return build_leaf();
if (this_token->ttype == TOK_ID) { // P24, P25
if ((t = check_reserved_ids(this_token->repr)) != TOK_INVALID) {
this_token->ttype = t;
}
return build_leaf();
} else {
if (this_token->ttype != TOK_LPAREN) {
handle_error(ERR_SYNTAX);
return NULL;
}
node_t *ret = calloc(1, sizeof(node_t));
if (! ret) {
logging(LOG_FATAL, "failed to allocate node");
return NULL;
}
ret->node_type = NT_INTERNAL;
ret->type = NO_TYPE;
advance_lexer();
if (is_unop(this_token->ttype)) { // P9
ret->tok = this_token->ttype;
advance_lexer();
ret->children[0] = build_exp();
if (next_token->ttype != TOK_RPAREN) {
handle_error(ERR_SYNTAX);
return ret;
}
advance_lexer();
}
else {
ret->children[0] = build_exp();
if (is_binop(next_token->ttype)) { // P8
ret->tok = next_token->ttype;
advance_lexer();
advance_lexer();
ret->children[1] = build_exp();
if (next_token->ttype != TOK_RPAREN) {
handle_error(ERR_SYNTAX);
return ret;
}
advance_lexer();
}
else if (next_token->ttype == TOK_QUESTION) { // P7
ret->tok = next_token->ttype;
advance_lexer();
advance_lexer();
ret->children[1] = build_exp();
if (next_token->ttype != TOK_COLON) {
handle_error(ERR_SYNTAX);
return ret;
}
advance_lexer();
advance_lexer();
ret->children[2] = build_exp();
if (next_token->ttype != TOK_RPAREN) {
handle_error(ERR_SYNTAX);
return ret;
}
advance_lexer();
}
else { // P10
if (next_token->ttype != TOK_RPAREN) {
handle_error(ERR_SYNTAX);
return ret;
}
ret->tok = TOK_IDENTITY;
advance_lexer();
}
}
return ret;
}
}
static node_t *build_root(void) {
// check running status
if (terminate || ignore_input) return NULL;
node_t *ret = calloc(1, sizeof(node_t));
if (! ret) {
logging(LOG_FATAL, "failed to allocate node");
return NULL;
}
ret->node_type = NT_ROOT;
ret->type = NO_TYPE;
// check for assignment
if (this_token->ttype == TOK_ID && next_token->ttype == TOK_ASSIGN) {
if (check_reserved_ids(this_token->repr) != TOK_INVALID) {
logging(LOG_ERROR, "variable name is reserved");
return ret;
}
ret->type = ID_TYPE;
ret->children[0] = build_leaf();
advance_lexer();
advance_lexer();
ret->children[1] = build_exp();
if (next_token->ttype != TOK_EOL) {
handle_error(ERR_SYNTAX);
}
return ret;
}
ret->children[0] = build_exp();
if (next_token->ttype == TOK_EOL) // P2
return ret;
else { // P3
if (next_token->ttype != TOK_SEP) {
handle_error(ERR_SYNTAX);
return ret;
}
advance_lexer();
if (next_token->ttype != TOK_ID) {
handle_error(ERR_SYNTAX);
return ret;
}
if (id_is_fmt_spec(next_token->repr))
next_token->ttype = TOK_FMT_SPEC;
if (next_token->ttype != TOK_FMT_SPEC) {
handle_error(ERR_SYNTAX);
return ret;
}
advance_lexer();
ret->children[1] = build_leaf();
if (next_token->ttype != TOK_EOL) {
handle_error(ERR_SYNTAX);
return ret;
}
return ret;
}
handle_error(ERR_SYNTAX);
return ret;
}
node_t *read_and_parse(void) {
init_lexer();
return build_root();
}