-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainScanner.c
More file actions
240 lines (206 loc) · 7.26 KB
/
MainScanner.c
File metadata and controls
240 lines (206 loc) · 7.26 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
/*
************************************************************
* COMPILERS COURSE - Algonquin College
* Code version: Summer, 2022
* Author: Harry Brown
* Professors: Paulo Sousa
************************************************************
*/
/*
************************************************************
* File name: mainScanner.c
* Author: Harry Brown
* Course: CST 8152 – Compilers, Lab Section: [011, 012, 013]
* Assignment: A22
* Date: Jul 23, 2022
* Professor: Paulo Sousa
* Purpose: This file is the main code for Scanner (A22)
* Function list: (...).
************************************************************
*/
/*
* 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 compiler projects.
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#ifndef COMPILERS_H_
#include "Compiler.h"
#endif
#ifndef BUFFER_H_
#include "Buffer.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 */
monaco_int errorNumber; /* Run-time error number = 0 by default (ANSI) */
/* External objects */
extern monaco_int line; /* Source code line numbers - defined in scanner.c */
extern Token tokenizer(monaco_nul);
/*
* -------------------------------------------------------------
* Function declarations
* -------------------------------------------------------------
*/
monaco_nul printScannerError(monaco_chr* fmt, ...);
monaco_nul displayScanner(Buffer* ptrBuffer);
monaco_dbl_lng getScannerFilesize(monaco_chr* fname);
monaco_nul printToken(Token t);
/*
************************************************************
* Scanner Main function
* Parameters:
* argc / argv = Parameters from command prompt
* Return value:
* Success operation.
***********************************************************
*/
monaco_int mainScanner(monaco_int argc, monaco_chr** argv) {
BufferPointer sourceBuffer; /* Pointer to input (source) buffer */
FILE* fileHandler; /* Input file handle */
Token currentToken; /* Token produced by the scanner */
monaco_int 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 = bufCreate(BUFFER_DEFAULT_SIZE, BUFFER_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 = bufLoad(sourceBuffer, fileHandler);
if (loadSize == BUFFER_ERROR)
printScannerError("%s%s", argv[0], ": Error in loading buffer.");
/* Close source file */
fclose(fileHandler);
/* Find the size of the file */
if (loadSize == BUFFER_ERROR) {
printf("The input file %s %s\n", argv[2], "is not completely loaded.");
printf("Input file size: %Lf\n", getScannerFilesize(argv[2]));
}
/* Compact and display the source buffer and add SEOF to input program buffer */
if ((loadSize != BUFFER_ERROR) && (loadSize != 0)) {
if (bufAddChar(sourceBuffer, BUFFER_EOF)) {
displayScanner(sourceBuffer);
}
}
/* Create string Literal Table */
stringLiteralTable = bufCreate(BUFFER_DEFAULT_SIZE, BUFFER_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 (bufGetPosWrte(stringLiteralTable)) bufPrint(stringLiteralTable);
printf("\n----------------------------------\n");
bufDestroy(sourceBuffer);
bufDestroy(stringLiteralTable);
sourceBuffer = stringLiteralTable = NULL;
/* 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
***********************************************************
*/
monaco_nul printScannerError(monaco_chr* 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.
***********************************************************
*/
monaco_nul displayScanner(Buffer* ptrBuffer) {
printf("\nPrinting buffer parameters:\n\n");
printf("The capacity of the buffer is: %d\n", bufGetSize(ptrBuffer));
printf("The current size of the buffer is: %d\n", bufGetPosWrte(ptrBuffer));
printf("\nPrinting buffer contents:\n\n");
bufRecover(ptrBuffer);
bufPrint(ptrBuffer);
}
/*
************************************************************
* The function gets size of scanner file
* Param:
* - Filename
* Return:
* - Size of the file
***********************************************************
*/
monaco_dbl_lng getScannerFilesize(monaco_chr* fname) {
FILE* fileInput;
monaco_dbl_lng 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;
}