-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainScanner.c
More file actions
272 lines (239 loc) · 9.06 KB
/
Copy pathMainScanner.c
File metadata and controls
272 lines (239 loc) · 9.06 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
/*
************************************************************
* COMPILERS COURSE - Algonquin College
* Code version: Summer, 2024
* Author: TODO
* Professors: Paulo Sousa
************************************************************
#
# ECHO "=---------------------------------------="
# ECHO "| COMPILERS - ALGONQUIN COLLEGE (S24) |"
# ECHO "=---------------------------------------="
# ECHO " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ”
# ECHO " @@ @@ ”
# ECHO " @@ %&@@@@@@@@@@@ @@ ”
# ECHO " @@ @%% (@@@@@@@@@ @ @@ ”
# ECHO " @@ @& @ @ @ @ @@ ”
# ECHO " @@ @ @ % / / @@@@@@ @@ ”
# ECHO " @@ & @ @ @@ @@ ”
# ECHO " @@ @/ @*@ @ @ @ @@ ”
# ECHO " @@ @@@@ @@ @ @ @@ ”
# ECHO " @@ /@@ @@@ @ @@ ”
# ECHO " @@ @ / / @@ @ @@ ”
# ECHO " @@ @ @@ /@/ @@@ @ @@ ”
# ECHO " @@ @@@@@@@@@@@@@@@ @@ ”
# ECHO " @@ @@ ”
# ECHO " @@ S O F I A @@ ”
# ECHO " @@ @@ ”
# ECHO " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ”
# ECHO " "
# ECHO "[READER SCRIPT .........................]"
# ECHO " "
*/
/*
************************************************************
* File name: MainScanner.c
* Compiler: MS Visual Studio 2022
* Course: CST 8152 – Compilers, Lab Section: [011, 012]
* Assignment: A22, A32.
* Date: May 01 2024
* Purpose: This file is the main code for Scanner (A22)
* Function list: (...).
************************************************************
*/
/*
*.............................................................................
* ADVICE 1:
* Please check the "TODO" labels to develop your activity.
*
* ADVICE 2: Preprocessor directives
* 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 directive does not have any effect on other compiler projects
* (Gcc, VSCode, Codeblocks, etc.).
*.............................................................................
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#ifndef COMPILERS_H_
#include "Compilers.h"
#endif
#ifndef BUFFER_H_
#include "Reader.h"
#endif
#ifndef SCANNER_H_
#include "Scanner.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 (used in other codes as external) */
BufferPointer stringLiteralTable; /* This buffer implements String Literal Table */
mobile_intg errorNumber; /* Run-time error number = 0 by default (ANSI) */
/* External objects */
extern mobile_intg line; /* Source code line numbers - defined in scanner.c */
extern Token tokenizer(sofia_void);
/*
* -------------------------------------------------------------
* Function declarations
* -------------------------------------------------------------
*/
mobile_void printScannerError(mobile_string fmt, ...);
mobile_void displayScanner(BufferPointer ptrBuffer);
mobile_long getScannerFilesize(mobile_string fname);
mobile_void printToken(Token t);
/*
************************************************************
* Scanner Main function
* Parameters:
* argc / argv = Parameters from command prompt
* Return value:
* Success operation.
***********************************************************
*/
mobile_intg mainScanner(mobile_intg argc, mobile_string* argv) {
BufferPointer sourceBuffer; /* Pointer to input (source) buffer */
FILE* fileHandler; /* Input file handle */
Token currentToken; /* Token produced by the scanner */
mobile_intg loadSize = 0; /* The size of the file loaded in the buffer */
/* Check for correct arrguments - source file name */
if (argc <= 2) {
/* __DATE__, __TIME__, __LINE__, __FILE__ are predefined preprocessor macros*/
printScannerError("Date: %s Time: %s", __DATE__, __TIME__);
printScannerError("Runtime error at line %d in file %s", __LINE__, __FILE__);
printScannerError("%s%s", argv[0], ": Missing source file name.");
printScannerError("%s", "Usage: <Option=1> <SourceFile>");
exit(EXIT_FAILURE);
}
/* Shows debug mode */
printf("%s%d%s", "[Debug mode: ", DEBUG, "]\n");
/* Create a source code input buffer - multiplicative mode */
sourceBuffer = readerCreate(READER_DEFAULT_SIZE, READER_DEFAULT_INCREMENT, MODE_MULTI);
if (sourceBuffer == NULL) {
printScannerError("%s%s", argv[1], ": Could not create source buffer");
exit(EXIT_FAILURE);
}
/* Open source file */
if ((fileHandler = fopen(argv[2], "r")) == NULL) {
printScannerError("%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, fileHandler);
if (loadSize == READER_ERROR)
printScannerError("%s%s", argv[0], ": Error in loading buffer.");
/* Close source file */
fclose(fileHandler);
/* 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", getScannerFilesize(argv[2]));
}
/* Compact and display the source buffer and add SEOF to input program buffer */
if ((loadSize != READER_ERROR) && (loadSize != 0)) {
if (readerAddChar(sourceBuffer, READER_TERMINATOR)) {
displayScanner(sourceBuffer);
}
}
/* Create string Literal Table */
stringLiteralTable = readerCreate(READER_DEFAULT_SIZE, READER_DEFAULT_INCREMENT, MODE_ADDIT);
if (stringLiteralTable == NULL) {
printScannerError("%s%s", argv[0], ": Could not create string literals buffer");
exit(EXIT_FAILURE);
}
/* Testbed for the scanner and add SEOF to input program buffer*/
/* Initialize scanner input buffer */
if (startScanner(sourceBuffer)) {
printScannerError("%s%s", argv[0], ": Empty program buffer - scanning canceled");
exit(EXIT_FAILURE);
}
printf("\nScanning source file...\n\n");
printf("Token\t\tAttribute\n");
printf("----------------------------------\n");
do {
currentToken = tokenizer();
printToken(currentToken);
} while (currentToken.code != SEOF_T);
/* Print String Literal Table if not empty */
printf("\nPrinting string table...\n");
printf("----------------------------------\n");
if (readerGetPosWrte(stringLiteralTable)) {
readerPrint(stringLiteralTable);
}
printf("\n----------------------------------\n");
readerRestore(sourceBuffer);
readerRestore(stringLiteralTable);
sourceBuffer = stringLiteralTable = NULL;
printScannerData(scData);
/* Ass2 evaluation only */
if (argv[3] != NULL && *argv[3] == 'l')
printf("The number of lines is: %d\n", line);
return (EXIT_SUCCESS);
}
/*
************************************************************
* 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 printScannerError(mobile_string fmt, ...) {
va_list ap;
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
/* Move to new line */
if (strchr(fmt, '\n') == NULL)
fprintf(stderr, "\n");
}
/*
************************************************************
* The function displays buffer contents
* Param:
* - Scanner to be displayed.
***********************************************************
*/
mobile_void displayScanner(BufferPointer ptrBuffer) {
printf("\nPrinting 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 buffer contents:\n\n");
readerRecover(ptrBuffer);
readerPrint(ptrBuffer);
}
/*
************************************************************
* The function gets size of scanner file
* Param:
* - Filename
* Return:
* - Size of the file
***********************************************************
*/
mobile_long getScannerFilesize(mobile_string fname) {
FILE* fileInput;
mobile_long fileLength;
fileInput = fopen(fname, "r");
if (fileInput == NULL) {
printScannerError("%s%s", "Cannot open file: ", fname);
return 0L;
}
fseek(fileInput, 0L, SEEK_END);
fileLength = ftell(fileInput);
fclose(fileInput);
return fileLength;
}