-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp.c
256 lines (227 loc) · 8.02 KB
/
exp.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Implementace překladače imperativního jazyka IFJ22
*
* @file exp.c
* @author Josef Kuchař ([email protected])
* @author Matej Sirovatka ([email protected])
* @author Tomáš Běhal ([email protected])
* @author Šimon Benčík ([email protected])
* @brief Syntax analysis for expressions
*/
#include "exp.h"
#include <stdio.h>
#include "error.h"
#include "parser.h"
#include "stack.h"
#include "token.h"
#include "token_term.h"
#define TABLE_SIZE 19
enum {
L, // <
R, // >
E, // =
X, // Invalid
};
/**
* @brief Parses parenthesis
*
* @param stack instance of stack containing the expression
* @return token_term_t result of the expression
*/
token_term_t* parse_paren(stack_t* stack) {
if (!(type_is_literal(stack->tokens[1]->result) || stack->tokens[1]->result == TOK_VAR)) {
error_exit(ERR_SYN);
}
if (stack->tokens[1]->terminal) {
error_exit(ERR_SYN);
}
if (stack->tokens[0]->value.type != TOK_RPAREN || stack->tokens[2]->result != TOK_LPAREN) {
error_exit(ERR_SYN);
}
token_term_free(stack->tokens[0]);
token_term_free(stack->tokens[2]);
return stack->tokens[1];
}
/**
* @brief Parses arithmetic expression
*
* @param stack instance of stack containing the expression
* @return token_term_t result of the expression
*/
token_term_t* parse_arithmetic(stack_t* stack) {
token_term_t* new = stack->tokens[1];
new->terminal = false;
new->right = stack->tokens[0];
new->left = stack->tokens[2];
new->result = TOK_VAR;
if (!(type_is_literal(stack->tokens[0]->result) || stack->tokens[0]->result == TOK_VAR)) {
error_exit(ERR_SYN);
}
if (!(type_is_literal(stack->tokens[2]->result) || stack->tokens[2]->result == TOK_VAR)) {
error_exit(ERR_SYN);
}
if (stack->tokens[0]->terminal || stack->tokens[2]->terminal) {
error_exit(ERR_SYN);
}
return new;
}
/**
* @brief Parses string concatenation
*
* @param stack instance of stack containing the expression
* @return token_term_t result of the expression
*/
token_term_t* parse_concat(stack_t* stack) {
token_term_t* new = stack->tokens[1];
new->terminal = false;
new->right = stack->tokens[0];
new->left = stack->tokens[2];
new->result = TOK_STR_LIT;
if (stack->tokens[0]->terminal || stack->tokens[2]->terminal) {
error_exit(ERR_SYN);
}
return new;
}
/**
* @brief Parses comparison
*
* @param stack instance of stack containing the expression
* @return token_term_t result of the expression
*/
token_term_t* parse_comparison(stack_t* stack) {
token_term_t* new = stack->tokens[1];
new->terminal = false;
new->right = stack->tokens[0];
new->left = stack->tokens[2];
new->result = TOK_BOOL_LIT;
if (!(type_is_literal(stack->tokens[0]->result) || stack->tokens[0]->result == TOK_VAR)) {
error_exit(ERR_SYN);
}
if (!(type_is_literal(stack->tokens[2]->result) || stack->tokens[2]->result == TOK_VAR)) {
error_exit(ERR_SYN);
}
if (stack->tokens[0]->terminal || stack->tokens[2]->terminal) {
error_exit(ERR_SYN);
}
return new;
}
const int precedence_table[TABLE_SIZE][TABLE_SIZE] = {
// =/!= only for formatting reasons
/** / + - . < <= > >= == != ( ) ID IN FL ST NI $ */
{R, R, R, R, R, R, R, R, R, R, R, L, R, L, L, L, L, L, R}, // *
{R, R, R, R, R, R, R, R, R, R, R, L, R, L, L, L, L, L, R}, // /
{L, L, R, R, R, R, R, R, R, R, R, L, R, L, L, L, L, L, R}, // +
{L, L, R, R, R, R, R, R, R, R, R, L, R, L, L, L, L, L, R}, // -
{L, L, R, R, R, R, R, R, R, R, R, L, R, L, L, L, L, L, R}, // .
{L, L, L, L, L, X, X, X, X, X, X, L, R, L, L, L, L, L, R}, // <
{L, L, L, L, L, X, X, X, X, X, X, L, R, L, L, L, L, L, R}, // <=
{L, L, L, L, L, X, X, X, X, X, X, L, R, L, L, L, L, L, R}, // >
{L, L, L, L, L, X, X, X, X, X, X, L, R, L, L, L, L, L, R}, // >=
{L, L, L, L, L, X, X, X, X, X, X, L, R, L, L, L, L, L, R}, // ==
{L, L, L, L, L, X, X, X, X, X, X, L, R, L, L, L, L, L, R}, // !=
{L, L, L, L, L, L, L, L, L, L, L, L, E, L, L, L, L, L, R}, // (
{R, R, R, R, R, R, R, R, R, R, R, X, R, X, X, X, X, X, R}, // )
{R, R, R, R, R, R, R, R, R, R, R, X, R, X, X, X, X, X, R}, // ID
{R, R, R, R, R, R, R, R, R, R, R, X, R, X, X, X, X, X, R}, // IN
{R, R, R, R, R, R, R, R, R, R, R, X, R, X, X, X, X, X, R}, // FL
{R, R, R, R, R, R, R, R, R, R, R, X, R, X, X, X, X, X, R}, // ST
{R, R, R, R, R, R, R, R, R, R, R, X, R, X, X, X, X, X, R}, // NI
{L, L, L, L, L, L, L, L, L, L, L, L, X, L, L, L, L, L, R}, // $
};
int get_precedence(token_t stack_top, token_t input) {
if (input.type >= TABLE_SIZE || stack_top.type >= TABLE_SIZE) {
return R;
}
return precedence_table[stack_top.type][input.type];
}
token_term_t* parse_expression(stack_t* stack) {
if (stack->len == 3) {
int type = stack->tokens[1]->value.type;
if (stack->tokens[2]->value.type == TOK_LPAREN) {
return parse_paren(stack);
}
switch (type) {
case TOK_PLUS:
case TOK_MINUS:
case TOK_MULTIPLY:
case TOK_DIVIDE:
return parse_arithmetic(stack);
break;
case TOK_LESS:
case TOK_LESS_E:
case TOK_GREATER:
case TOK_GREATER_E:
case TOK_EQUALS:
case TOK_NEQUALS:
return parse_comparison(stack);
break;
case TOK_DOT:
return parse_concat(stack);
break;
default:
error_exit(ERR_SYN);
break;
}
} else if (stack->len == 1) { // Rules for single identifier/single non terminal
if (token_is_literal(&stack->tokens[0]->value) || stack->tokens[0]->value.type == TOK_VAR) {
if (stack->tokens[0]->terminal) {
stack->tokens[0]->terminal = false;
return stack->tokens[0];
} else {
error_not_implemented();
if (token_is_literal(&stack->tokens[0]->value) ||
stack->tokens[0]->value.type == TOK_VAR) {
return token_term_new(token_new(TOK_EXP_END, 0, 0), false);
}
}
}
}
error_exit(ERR_SYN);
return NULL;
}
void rule_exp(parser_t* parser, parser_state_t state) {
(void)state;
stack_t stack = stack_new();
stack_t current_expression = stack_new();
stack_push(&stack, token_term_new(token_new(TOK_DOLLAR, 0, 0), true));
while (true) {
int precedence = get_precedence(stack_top_terminal(&stack)->value, parser->token);
token_term_t* token = NULL;
if (!stack_top(&stack)->terminal && stack.len == 2 &&
(precedence == R || precedence == X)) {
gen_exp(parser->gen, stack_top(&stack), state.in_function);
break;
}
switch (precedence) {
case L:
stack_push_after_terminal(&stack);
stack_push(&stack, token_term_new(parser->token, true));
next_token_keep(parser);
break;
case R:
while ((token = stack_pop(&stack))->value.type != TOK_HANDLE_START) {
if (token->value.type != TOK_HANDLE_START) {
stack_push(¤t_expression, token);
} else {
token_term_free(token);
}
}
token_term_free(token);
stack_push(&stack, parse_expression(¤t_expression));
break;
case E:
stack_push(&stack, token_term_new(parser->token, true));
next_token_keep(parser);
break;
case X:
error_exit(ERR_SYN);
break;
default:
break;
}
stack_empty(¤t_expression);
}
stack_free(¤t_expression);
stack_free(&stack);
return;
}