-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainParser.c
More file actions
313 lines (265 loc) · 9.74 KB
/
MainParser.c
File metadata and controls
313 lines (265 loc) · 9.74 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
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
/*
************************************************************
* COMPILERS COURSE - Algonquin College
* Code version: Fall, 2023
* Author: TO_DO
* Professors: Paulo Sousa
************************************************************
=---------------------------------------=
| COMPILERS - ALGONQUIN COLLEGE (F23) |
=---------------------------------------=
| .... |
| ........::.::::::. |
| .:........::.:^^^~~~: |
| :^^::::::^^^::^!7??7~^. |
| .:^~~^!77777~~7?YY?7??7^. |
| :.^~!??!^::::^^~!?5PY??!~. |
| ~!!7J~.:::^^^^~!!~~?G5J?~ |
| :^~?!~7?Y57^^?PP5YJ!J5Y?: |
| .~!!.:^!7!:.:7JYYJ7~7Y7^ |
| .~77.. . .~^:^^^~7?: |
| .^!^~:::.:^!7?~^~!77J: |
| ^^!Y~^^^^~?YJ77??7JJ^ |
| .^7J?~^~~^~7??7??7JY?~: |
| ::^^~^7?!^~~!7???J?J7~:. |
| ^~~!.^7YPPPP5Y?7J7777~. |
| ..:~..:^!JPP5YJ?!777!^. |
| .~?JJJJJJJJJJYYYYYPPPPPPPPPPPP5PPYY~ |
| :!Y5GGG.___ YYYYYY__._.PPGGGGGG5!. |
| :!Y5G / __| ___ / _(_)__ _ PGP5. |
| :~75 \__ \/ _ \ _| / _` | 5?. |
| 7~7 |___/\___/_| |_\__,_| Y5?. |
| .^~!~.....................P5YY7. |
| .:::::::::::::?JJJJYYYYYYYYYJJJJ7. |
| |
=---------------------------------------=
*/
/*
************************************************************
* File name: MainParser.c
* Compiler: MS Visual Studio 2022
* Course: CST 8152 – Compilers, Lab Section: [011, 012, 013]
* Assignment: A32.
* Date: May 01 2023
* Professor: Paulo Sousa
* Purpose: This file is the main code for Parser (A32)
* Function list: (...).
************************************************************
*/
/*
************************************************************
* IMPORTANT NOTE:
* The #define _CRT_SECURE_NO_WARNINGS should be used in MS Visual Studio projects
* to suppress the warnings about using "unsafe" functions like fopen()
* and standard sting library functions defined in string.h.
* The define does not have any effect in other compilers projects.
*********************************************************
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> /* Constants for calls to exit()*/
#include <string.h>
#include <stdarg.h>
#ifndef COMPILERS_H_
#include "Compilers.h"
#endif
#ifndef READER_H_
#include "Reader.h"
#endif
#ifndef SCANNER_H_
#include "Scanner.h"
#endif
#ifndef PARSER_H_
#include "Parser.h"
#endif
/* Check for ANSI C compliancy */
#define ANSI_C 0
#if defined(__STDC__)
#undef ANSI_C
#define ANSI_C 1
#endif
/*
* -------------------------------------------------------------
* Global vars and External vars
* -------------------------------------------------------------
*/
/* Global objects - variables */
static BufferPointer sourceBuffer; /* pointer to input (source) buffer */
BufferPointer stringLiteralTable; /* This buffer is used as a repository for string literals */
mobile_intg errorNumber; /* Run-time error number = 0 by default (ANSI) */
/* External objects */
extern mobile_intg syntaxErrorNumber /* number of syntax errors reported by the parser */;
extern mobile_intg line; /* source code line number - defined in scanner.c */
extern ParserData psData;
/*
* -------------------------------------------------------------
* Function declarations
* -------------------------------------------------------------
*/
/* Function declarations (prototypes) */
extern mobile_void startParser(sofia_void);
extern mobile_intg startScanner(BufferPointer sc_buf);
static mobile_void printParserError(mobile_string fmt, ...);
static mobile_void displayParser(BufferPointer ptrBuffer);
static mobile_long getParserFileSize(mobile_string fname);
static mobile_void callGarbageCollector(mobile_void);
/*
************************************************************
* Parser Main function
* Parameters:
* - argc / argv = Parameters from command prompt
* Return value:
* - Success operation.
***********************************************************
*/
mobile_intg mainParser(mobile_intg argc, mobile_string* argv) {
numParserErrors = 0; /* Initializes the errors */
FILE* fi; /* input file handle */
mobile_intg loadsize = 0; /*the size of the file loaded in the buffer */
/*check for correct arrguments - source file name */
if (argc <= 1) {
/* __DATE__, __TIME__, __LINE__, __FILE__ are predefined preprocessor macros*/
printParserError("Date: %s Time: %s", __DATE__, __TIME__);
printParserError("Runtime error at line %d in file %s", __LINE__, __FILE__);
printParserError("%s%s%s", argv[0], ": ", "Missing source file name.");
printParserError("%s%s%s", "Usage: ", "parser", " source_file_name");
exit(EXIT_FAILURE);
}
/* create a source code input buffer - multiplicative mode */
sourceBuffer = readerCreate(READER_DEFAULT_SIZE, READER_DEFAULT_INCREMENT, MODE_MULTI);
if (sourceBuffer == NULL) {
printParserError("%s%s%s", argv[0], ": ", "Could not create source buffer");
exit(EXIT_FAILURE);
}
/*open source file */
if ((fi = fopen(argv[2], "r")) == NULL) {
printParserError("%s%s%s%s", argv[0], ": ", "Cannot open file: ", argv[2]);
exit(EXIT_FAILURE);
}
/* load source file into input buffer */
printf("Reading file %s ....Please wait\n", argv[2]);
loadsize = readerLoad(sourceBuffer, fi);
if (loadsize == READER_ERROR)
printParserError("%s%s%s", argv[0], ": ", "Error in loading buffer.");
/* close source file */
fclose(fi);
/*find the size of the file */
if (loadsize == READER_ERROR) {
printf("The input file %s %s\n", argv[2], "is not completely loaded.");
printf("Input file size: %ld\n", getParserFileSize(argv[2]));
}
/* Add SEOF (EOF) to input buffer and display the source buffer */
if ((loadsize != READER_ERROR) && (loadsize != 0)) {
if (readerAddChar(sourceBuffer, READER_TERMINATOR)) {
displayParser(sourceBuffer);
}
}
/* create string Literal Table */
stringLiteralTable = readerCreate(READER_DEFAULT_SIZE, READER_DEFAULT_INCREMENT, MODE_ADDIT);
if (stringLiteralTable == NULL) {
printParserError("%s%s%s", argv[0], ": ", "Could not create string literal buffer");
exit(EXIT_FAILURE);
}
/* Registrer exit function */
atexit(callGarbageCollector);
/* Initialize scanner */
startScanner(sourceBuffer);
/* Start parsing */
printf("\nParsing the source file...\n\n");
startParser();
printf("\nNumber of Parser errors: %d\n", numParserErrors);
/* Prints the statistics */
printBNFData(psData);
return (EXIT_SUCCESS); /* same effect as exit(0) */
}
void startParser() {
lookahead = tokenizer(); // Get the first token
program(); // Start parsing with the program rule
matchToken(SEOF_T, NO_ATTR); // Ensure the end of the file is reached
}
//void matchToken(int tokenCode, int tokenAttribute) {
//if (lookahead.code == tokenCode && lookahead.attribute == tokenAttribute) {
//lookahead = tokenizer(); // Move to the next token
//}
// else {
// printError();
//}
//
void syncErrorHandler(int syncTokenCode) {
// Handle errors by skipping tokens until a specific token is found
while (lookahead.code != syncTokenCode) {
lookahead = tokenizer();
}
lookahead = tokenizer(); // Move past the sync token
}
void printError() {
printf("Syntax error at line %d\n", line);
//errorCount++;
}
/*
************************************************************
* Error printing function with variable number of arguments
* Params: Variable arguments, using formats from C language.
* - Internal vars use list of arguments and types from stdarg.h
* - NOTE: The format is using signature from C Language
************************************************************
*/
mobile_void printParserError(mobile_string fmt, ...) {
va_list ap;
va_start(ap, fmt);
(mobile_void)vfprintf(stderr, fmt, ap);
va_end(ap);
/* Move to new line */
if (strchr(fmt, '\n') == NULL)
fprintf(stderr, "\n");
}
/*
************************************************************
* The function return the size of an open file
* Param:
* - Filename
* Return:
* - Size of the file
************************************************************
*/
mobile_long getParserFileSize(mobile_string fname) {
FILE* input;
mobile_long flength;
input = fopen(fname, "r");
if (input == NULL) {
printParserError("%s%s", "Cannot open file: ", fname);
return 0;
}
fseek(input, 0L, SEEK_END);
flength = ftell(input);
fclose(input);
return flength;
}
/*
************************************************************
* The function display buffer contents
* Param:
* - Parser to be displayed.
************************************************************
*/
mobile_void displayParser(BufferPointer ptrBuffer) {
printf("\nPrinting input buffer parameters:\n\n");
printf("The capacity of the buffer is: %d\n", readerGetSize(ptrBuffer));
printf("The current size of the buffer is: %d\n", readerGetPosWrte(ptrBuffer));
printf("\nPrinting input buffer contents:\n\n");
readerRecover(ptrBuffer);
readerPrint(ptrBuffer);
}
/*
************************************************************
* The function frees all dynamically allocated memory.
* This function is always called despite how the program terminates - normally or abnormally.
************************************************************
*/
mobile_void callGarbageCollector(sofia_void) {
if (syntaxErrorNumber)
printf("\nSyntax errors: %d\n", syntaxErrorNumber);
printf("\nCollecting garbage...\n");
readerFree(sourceBuffer);
readerFree(stringLiteralTable);
}