-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.h
391 lines (350 loc) · 13.5 KB
/
parser.h
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* @file parser.h
* @author Jakub Všetečka <xvsete00@vutbr.cz>
* @brief Parser header file.
* @version 0.1
* @date 2023-11-24
*
* @copyright Copyright (c) 2023
* Project: IFJ compiler
*
*/
#ifndef PARSER_H
#define PARSER_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "debug.h"
#include "scanner.h"
#include "error.h"
#include "stack.h"
#include "semantic.h"
#include "generator.h"
#include "symtable.h"
#define RED "\x1B[31m"
#define GREEN "\x1B[32m"
#define YELLOW "\x1B[33m"
#define BLUE "\x1B[34m"
#define MAGENTA "\x1B[35m"
#define CYAN "\x1B[36m"
#define RESET "\x1B[0m"
/**
* @brief Initializes needed structures and runs the parser.
*/
void run_parser();
/**
* @brief Asks scanner for next token.
*
* @param token pointer to where the new token will be saved
*/
void get_token(Token *token);
/**
* @brief Compares token type with expected type. Calls semantic rule if needed.
*
* @param token pointer to token
* @param items pointer to sym_items for semantic analysis
* @param type expected token type
* @param sem_rule semantic rule
*/
bool cmp_type(Token *token, sym_items *items, Token_type type, Control_state sem_rule);
/**
* @brief Adds built-in functions to symtable.
*
* @param items pointer to sym_items
*/
void add_builtin_functions(sym_items *items);
/**
* @brief Executes semantic rule alongside with generator if needed.
*
* @param token pointer to token
* @param items pointer to sym_items for context propagation
* @param sem_rule semantic rule to apply
*/
int run_control(Token *token, sym_items *items, Control_state sem_rule);
// RECURSIVE DESCENT PARSER
/**
* @brief Parses the START nonterminal in the defined grammar.
* START signifies the top-level rule of the grammar, structured as:
* START -> STMT_LIST eof
* This function manages the initial parsing step, ensuring that the entire input
* conforms to the STMT_LIST followed by an end-of-file (eof) token.
*
* @param token A pointer to the current token, marking the beginning of the parsing process.
* @param items A pointer to sym_items, used for contextual information and state management during parsing.
* @return Returns true if the entire input sequence correctly matches the START rule, false otherwise.
*/
bool START(Token *token, sym_items *items);
/**
* @brief Parses STMT_LIST nonterminal in the defined grammar.
* STMT_LIST -> STMT STMT_LIST | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool STMT_LIST(Token *token, sym_items *items);
/**
* @brief Parses LOCAL_STMT_LIST nonterminal in the defined grammar.
* LOCAL_STMT_LIST -> LOCAL_STMT LOCAL_STMT_LIST | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool LOCAL_STMT_LIST(Token *token, sym_items *items);
/**
* @brief Parses STMT nonterminal in the defined grammar.
* STMT -> VAR_LET | LOAD_ID | DEF_FUNC | FUNC_STMT | IF_STMT | WHILE_STMT | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool STMT(Token *token, sym_items *items);
/**
* @brief Parses LOCAL_STMT nonterminal in the defined grammar.
* LOCAL_STMT -> VAR_LET | LOAD_ID | FUNC_STMT | IF_STMT | WHILE_STMT | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool LOCAL_STMT(Token *token, sym_items *items);
/**
* @brief Parses VAR_LET nonterminal in the defined grammar.
* VAR_LET -> VAR_LET -> VAR_SCOPE id TYPE_AND_ASSIGN
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool VAR_LET(Token *token, sym_items *items);
/**
* @brief Parses VAR_SCOPE nonterminal in the defined grammar.
* VAR_SCOPE -> let | var
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool VAR_SCOPE(Token *token, sym_items *items);
/**
* @brief Parses TYPE_AND_ASSIGN nonterminal in the defined grammar.
* TYPE_AND_ASSIGN -> : D_TYPE R_FLEX | = EXP
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @param sem_rule Semantic rule to apply.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool TYPE_AND_ASIGN(Token *token, sym_items *items);
/**
* @brief Parses D_TYPE nonterminal in the defined grammar.
* D_TYPE -> Int | Int? | Double | Double? | String | String? | Bool | Bool?
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @param sem_rule Semantic rule to apply.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool D_TYPE(Token *token, sym_items *items, Control_state sem_rule);
/**
* @brief Parses R_FLEX nonterminal in the defined grammar.
* R_FLEX -> = EXP | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool R_FLEX(Token *token, sym_items *items);
/**
* @brief Parses DEF_FUNC nonterminal in the defined grammar.
* DEF_FUNC -> func id ( P_LIST ) RET_TYPE { FUNC_STMT_LIST }
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool DEF_FUNC(Token *token, sym_items *items);
/**
* @brief Parses P_LIST nonterminal in the defined grammar.
* P_LIST -> PARAM | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool P_LIST(Token *token, sym_items *items);
/**
* @brief Parses PARAM nonterminal in the defined grammar.
* PARAM -> id id : D_TYPE SEP
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool PARAM(Token *token, sym_items *items);
/**
* @brief Parses SEP nonterminal in the defined grammar.
* SEP -> , PARAM | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for context propagation during parsing.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool P_SEP(Token *token, sym_items *items);
/**
* @brief Parses RET_TYPE nonterminal in the defined grammar.
* RET_TYPE -> -> D_TYPE | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool RET_TYPE(Token *token, sym_items *items);
/**
* @brief Parses FUNC_STMT_LIST nonterminal in the defined grammar.
* FUNC_STMT_LIST -> FUNC_STMT FUNC_STMT_LIST | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool FUNC_STMT_LIST(Token *token, sym_items *items);
/**
* @brief Parses FUNC_STMT nonterminal in the defined grammar.
* FUNC_STMT -> VAR_LET | LOAD_ID | FUNC_WHILE | FUNC_IF | RET | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool FUNC_STMT(Token *token, sym_items *items);
/**
* @brief Parses RET nonterminal in the defined grammar.
* RET -> return EXP
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool RET(Token *token, sym_items *items);
/**
* @brief Parses FUNC_WHILE nonterminal in the defined grammar.
* FUNC_WHILE -> while EXP { FUNC_STMT_LIST }
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool FUNC_WHILE(Token *token, sym_items *items);
/**
* @brief Parses FUNC_IF nonterminal in the defined grammar.
* FUNC_IF -> if IF_COND { FUNC_STMT_LIST } FUNC_ELSE_CLAUSE
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool FUNC_IF(Token *token, sym_items *items);
/**
* @brief Parses FUNC_ELSE_IF nonterminal in the defined grammar.
* FUNC_ELSE_IF -> if IF_COND { FUNC_STMT_LIST } FUNC_ELSE_CLAUSE
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool FUNC_ELSE_IF(Token *token, sym_items *items);
/**
* @brief Parses FUNC_ELSE_CLAUSE nonterminal in the defined grammar.
* FUNC_ELSE_CLAUSE -> else FUNC_AFTER_ELSE | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool FUNC_ELSE_CLAUSE(Token *token, sym_items *items);
/**
* @brief Parses FUNC_AFTER_ELSE nonterminal in the defined grammar.
* FUNC_AFTER_ELSE -> { FUNC_STMT_LIST } | FUNC_IF
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool FUNC_AFTER_ELSE(Token *token, sym_items *items);
/**
* @brief Parses IF_STMT nonterminal in the defined grammar.
* IF_STMT -> if EXP { LOCAL_STMT_LIST } ELSE_CLAUSE
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool IF_STMT(Token *token, sym_items *items);
/**
* @brief Parses ELSE_IF_STMT nonterminal in the defined grammar.
* ELSE_IF_STMT -> if EXP { LOCAL_STMT_LIST } ELSE_CLAUSE
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool ELSE_IF_STMT(Token *token, sym_items *items);
/**
* @brief Parses IF_COND nonterminal in the defined grammar.
* IF_COND -> EXP | let id
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool IF_COND(Token *token, sym_items *items);
/**
* @brief Parses ELSE_CLAUSE nonterminal in the defined grammar.
* ELSE_CLAUSE -> else AFTER_ELSE | ε
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool ELSE_CLAUSE(Token *token, sym_items *items);
/**
* @brief Parses AFTER_ELSE nonterminal in the defined grammar.
* AFTER_ELSE -> { LOCAL_STMT_LIST } | IF_STMT
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool AFTER_ELSE(Token *token, sym_items *items);
/**
* @brief Parses WHILE_STMT nonterminal in the defined grammar.
* WHILE_STMT -> while EXP { LOCAL_STMT_LIST }
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool WHILE_STMT(Token *token, sym_items *items);
/**
* @brief Parses LOAD_ID nonterminal in the defined grammar.
* LOAD_ID -> id = EXP | func_id
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool LOAD_ID(Token *token, sym_items *items);
/**
* @brief Parses EXP nonterminal in the defined grammar.
* PSA is used for parsing.
*
* @param token A pointer to the current token in the parsing process.
* @param items A pointer to sym_items, used for semantic analysis.
* @param sem_rule Semantic rule to apply.
* @return Returns true if parsing succeeds, false otherwise.
*/
bool EXP(Token *token, sym_items *items, Control_state sem_rule);
#endif // PARSER_H