|
| 1 | +/* |
| 2 | +************************************************************ |
| 3 | +* COMPILERS COURSE - Algonquin College |
| 4 | +* Code version: Summer, 2024 |
| 5 | +* Author: TO_DO |
| 6 | +* Professors: Paulo Sousa |
| 7 | +************************************************************ |
| 8 | +# |
| 9 | +# ECHO "=---------------------------------------=" |
| 10 | +# ECHO "| COMPILERS - ALGONQUIN COLLEGE (S24) |" |
| 11 | +# ECHO "=---------------------------------------=" |
| 12 | +# ECHO " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ” |
| 13 | +# ECHO " @@ @@ ” |
| 14 | +# ECHO " @@ %&@@@@@@@@@@@ @@ ” |
| 15 | +# ECHO " @@ @%% (@@@@@@@@@ @ @@ ” |
| 16 | +# ECHO " @@ @& @ @ @ @ @@ ” |
| 17 | +# ECHO " @@ @ @ % / / @@@@@@ @@ ” |
| 18 | +# ECHO " @@ & @ @ @@ @@ ” |
| 19 | +# ECHO " @@ @/ @*@ @ @ @ @@ ” |
| 20 | +# ECHO " @@ @@@@ @@ @ @ @@ ” |
| 21 | +# ECHO " @@ /@@ @@@ @ @@ ” |
| 22 | +# ECHO " @@ @ / / @@ @ @@ ” |
| 23 | +# ECHO " @@ @ @@ /@/ @@@ @ @@ ” |
| 24 | +# ECHO " @@ @@@@@@@@@@@@@@@ @@ ” |
| 25 | +# ECHO " @@ @@ ” |
| 26 | +# ECHO " @@ S O F I A @@ ” |
| 27 | +# ECHO " @@ @@ ” |
| 28 | +# ECHO " @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ” |
| 29 | +# ECHO " " |
| 30 | +# ECHO "[READER SCRIPT .........................]" |
| 31 | +# ECHO " " |
| 32 | +*/ |
| 33 | + |
| 34 | +/* |
| 35 | +************************************************************ |
| 36 | +* File name: MainScanner.c |
| 37 | +* Compiler: MS Visual Studio 2022 |
| 38 | +* Course: CST 8152 – Compilers, Lab Section: [011, 012] |
| 39 | +* Assignment: A22, A32. |
| 40 | +* Date: May 01 2024 |
| 41 | +* Purpose: This file is the main code for Scanner (A22) |
| 42 | +* Function list: (...). |
| 43 | +************************************************************ |
| 44 | +*/ |
| 45 | + |
| 46 | +/* |
| 47 | + *............................................................................. |
| 48 | + * ADVICE 1: |
| 49 | + * Please check the "TODO" labels to develop your activity. |
| 50 | + * |
| 51 | + * ADVICE 2: Preprocessor directives |
| 52 | + * The #define _CRT_SECURE_NO_WARNINGS should be used in MS Visual Studio projects |
| 53 | + * to suppress the warnings about using "unsafe" functions like fopen() |
| 54 | + * and standard sting library functions defined in string.h. |
| 55 | + * The define directive does not have any effect on other compiler projects |
| 56 | + * (Gcc, VSCode, Codeblocks, etc.). |
| 57 | + *............................................................................. |
| 58 | + */ |
| 59 | + |
| 60 | +#define _CRT_SECURE_NO_WARNINGS |
| 61 | + |
| 62 | +#include <stdio.h> |
| 63 | +#include <stdlib.h> |
| 64 | +#include <string.h> |
| 65 | +#include <stdarg.h> |
| 66 | + |
| 67 | +#ifndef COMPILERS_H_ |
| 68 | +#include "Compilers.h" |
| 69 | +#endif |
| 70 | + |
| 71 | +#ifndef BUFFER_H_ |
| 72 | +#include "Reader.h" |
| 73 | +#endif |
| 74 | + |
| 75 | +#ifndef SCANNER_H_ |
| 76 | +#include "Scanner.h" |
| 77 | +#endif |
| 78 | + |
| 79 | + /*check for ANSI C compliancy */ |
| 80 | +#define ANSI_C 0 |
| 81 | +#if defined(__STDC__) |
| 82 | +#undef ANSI_C |
| 83 | +#define ANSI_C 1 |
| 84 | +#endif |
| 85 | + |
| 86 | +/* |
| 87 | + * ------------------------------------------------------------- |
| 88 | + * Global vars and External vars |
| 89 | + * ------------------------------------------------------------- |
| 90 | + */ |
| 91 | + |
| 92 | + /* Global objects - variables (used in other codes as external) */ |
| 93 | +BufferPointer stringLiteralTable; /* This buffer implements String Literal Table */ |
| 94 | +sofia_intg errorNumber; /* Run-time error number = 0 by default (ANSI) */ |
| 95 | + |
| 96 | +/* External objects */ |
| 97 | +extern sofia_intg line; /* Source code line numbers - defined in scanner.c */ |
| 98 | +extern Token tokenizer(sofia_void); |
| 99 | + |
| 100 | +/* |
| 101 | + * ------------------------------------------------------------- |
| 102 | + * Function declarations |
| 103 | + * ------------------------------------------------------------- |
| 104 | + */ |
| 105 | +sofia_void printScannerError(sofia_string fmt, ...); |
| 106 | +sofia_void displayScanner(BufferPointer ptrBuffer); |
| 107 | +sofia_long getScannerFilesize(sofia_string fname); |
| 108 | +sofia_void printToken(Token t); |
| 109 | + |
| 110 | +/* |
| 111 | +************************************************************ |
| 112 | + * Scanner Main function |
| 113 | + * Parameters: |
| 114 | + * argc / argv = Parameters from command prompt |
| 115 | + * Return value: |
| 116 | + * Success operation. |
| 117 | + *********************************************************** |
| 118 | + */ |
| 119 | + |
| 120 | +sofia_intg mainScanner(sofia_intg argc, sofia_string* argv) { |
| 121 | + |
| 122 | + BufferPointer sourceBuffer; /* Pointer to input (source) buffer */ |
| 123 | + FILE* fileHandler; /* Input file handle */ |
| 124 | + Token currentToken; /* Token produced by the scanner */ |
| 125 | + sofia_intg loadSize = 0; /* The size of the file loaded in the buffer */ |
| 126 | + |
| 127 | + /* Check for correct arrguments - source file name */ |
| 128 | + if (argc <= 2) { |
| 129 | + /* __DATE__, __TIME__, __LINE__, __FILE__ are predefined preprocessor macros*/ |
| 130 | + printScannerError("Date: %s Time: %s", __DATE__, __TIME__); |
| 131 | + printScannerError("Runtime error at line %d in file %s", __LINE__, __FILE__); |
| 132 | + printScannerError("%s%s", argv[0], ": Missing source file name."); |
| 133 | + printScannerError("%s", "Usage: <Option=1> <SourceFile>"); |
| 134 | + exit(EXIT_FAILURE); |
| 135 | + } |
| 136 | + |
| 137 | + /* Shows debug mode */ |
| 138 | + printf("%s%d%s", "[Debug mode: ", DEBUG, "]\n"); |
| 139 | + |
| 140 | + /* Create a source code input buffer - multiplicative mode */ |
| 141 | + sourceBuffer = readerCreate(READER_DEFAULT_SIZE, READER_DEFAULT_INCREMENT, MODE_MULTI); |
| 142 | + if (sourceBuffer == NULL) { |
| 143 | + printScannerError("%s%s", argv[1], ": Could not create source buffer"); |
| 144 | + exit(EXIT_FAILURE); |
| 145 | + } |
| 146 | + |
| 147 | + /* Open source file */ |
| 148 | + if ((fileHandler = fopen(argv[2], "r")) == NULL) { |
| 149 | + printScannerError("%s%s%s", argv[0], ": Cannot open file: ", argv[2]); |
| 150 | + exit(EXIT_FAILURE); |
| 151 | + } |
| 152 | + |
| 153 | + /* Load source file into input buffer */ |
| 154 | + printf("Reading file %s ....Please wait\n", argv[2]); |
| 155 | + loadSize = readerLoad(sourceBuffer, fileHandler); |
| 156 | + if (loadSize == READER_ERROR) |
| 157 | + printScannerError("%s%s", argv[0], ": Error in loading buffer."); |
| 158 | + |
| 159 | + /* Close source file */ |
| 160 | + fclose(fileHandler); |
| 161 | + /* Find the size of the file */ |
| 162 | + if (loadSize == READER_ERROR) { |
| 163 | + printf("The input file %s %s\n", argv[2], "is not completely loaded."); |
| 164 | + printf("Input file size: %ld\n", getScannerFilesize(argv[2])); |
| 165 | + } |
| 166 | + |
| 167 | + /* Compact and display the source buffer and add SEOF to input program buffer */ |
| 168 | + if ((loadSize != READER_ERROR) && (loadSize != 0)) { |
| 169 | + if (readerAddChar(sourceBuffer, READER_TERMINATOR)) { |
| 170 | + displayScanner(sourceBuffer); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /* Create string Literal Table */ |
| 175 | + stringLiteralTable = readerCreate(READER_DEFAULT_SIZE, READER_DEFAULT_INCREMENT, MODE_ADDIT); |
| 176 | + if (stringLiteralTable == NULL) { |
| 177 | + printScannerError("%s%s", argv[0], ": Could not create string literals buffer"); |
| 178 | + exit(EXIT_FAILURE); |
| 179 | + } |
| 180 | + |
| 181 | + /* Testbed for the scanner and add SEOF to input program buffer*/ |
| 182 | + /* Initialize scanner input buffer */ |
| 183 | + if (startScanner(sourceBuffer)) { |
| 184 | + printScannerError("%s%s", argv[0], ": Empty program buffer - scanning canceled"); |
| 185 | + exit(EXIT_FAILURE); |
| 186 | + } |
| 187 | + |
| 188 | + printf("\nScanning source file...\n\n"); |
| 189 | + printf("Token\t\tAttribute\n"); |
| 190 | + printf("----------------------------------\n"); |
| 191 | + do { |
| 192 | + currentToken = tokenizer(); |
| 193 | + printToken(currentToken); |
| 194 | + } while (currentToken.code != SEOF_T); |
| 195 | + |
| 196 | + /* Print String Literal Table if not empty */ |
| 197 | + printf("\nPrinting string table...\n"); |
| 198 | + printf("----------------------------------\n"); |
| 199 | + if (readerGetPosWrte(stringLiteralTable)) { |
| 200 | + readerPrint(stringLiteralTable); |
| 201 | + } |
| 202 | + printf("\n----------------------------------\n"); |
| 203 | + readerRestore(sourceBuffer); |
| 204 | + readerRestore(stringLiteralTable); |
| 205 | + sourceBuffer = stringLiteralTable = NULL; |
| 206 | + printScannerData(scData); |
| 207 | + /* Ass2 evaluation only */ |
| 208 | + if (argv[3] != NULL && *argv[3] == 'l') |
| 209 | + printf("The number of lines is: %d\n", line); |
| 210 | + |
| 211 | + return (EXIT_SUCCESS); |
| 212 | +} |
| 213 | + |
| 214 | +/* |
| 215 | +************************************************************ |
| 216 | + * Error printing function with variable number of arguments |
| 217 | + * Params: Variable arguments, using formats from C language. |
| 218 | + * - Internal vars use list of arguments and types from stdarg.h |
| 219 | + * - NOTE: The format is using signature from C Language |
| 220 | +*********************************************************** |
| 221 | +*/ |
| 222 | + |
| 223 | +sofia_void printScannerError(sofia_string fmt, ...) { |
| 224 | + va_list ap; |
| 225 | + va_start(ap, fmt); |
| 226 | + (void)vfprintf(stderr, fmt, ap); |
| 227 | + va_end(ap); |
| 228 | + /* Move to new line */ |
| 229 | + if (strchr(fmt, '\n') == NULL) |
| 230 | + fprintf(stderr, "\n"); |
| 231 | +} |
| 232 | + |
| 233 | +/* |
| 234 | +************************************************************ |
| 235 | + * The function displays buffer contents |
| 236 | + * Param: |
| 237 | + * - Scanner to be displayed. |
| 238 | + *********************************************************** |
| 239 | + */ |
| 240 | + |
| 241 | +sofia_void displayScanner(BufferPointer ptrBuffer) { |
| 242 | + printf("\nPrinting buffer parameters:\n\n"); |
| 243 | + printf("The capacity of the buffer is: %d\n", readerGetSize(ptrBuffer)); |
| 244 | + printf("The current size of the buffer is: %d\n", readerGetPosWrte(ptrBuffer)); |
| 245 | + printf("\nPrinting buffer contents:\n\n"); |
| 246 | + readerRecover(ptrBuffer); |
| 247 | + readerPrint(ptrBuffer); |
| 248 | +} |
| 249 | + |
| 250 | +/* |
| 251 | + ************************************************************ |
| 252 | + * The function gets size of scanner file |
| 253 | + * Param: |
| 254 | + * - Filename |
| 255 | + * Return: |
| 256 | + * - Size of the file |
| 257 | + *********************************************************** |
| 258 | + */ |
| 259 | + |
| 260 | +sofia_long getScannerFilesize(sofia_string fname) { |
| 261 | + FILE* fileInput; |
| 262 | + sofia_long fileLength; |
| 263 | + fileInput = fopen(fname, "r"); |
| 264 | + if (fileInput == NULL) { |
| 265 | + printScannerError("%s%s", "Cannot open file: ", fname); |
| 266 | + return 0L; |
| 267 | + } |
| 268 | + fseek(fileInput, 0L, SEEK_END); |
| 269 | + fileLength = ftell(fileInput); |
| 270 | + fclose(fileInput); |
| 271 | + return fileLength; |
| 272 | +} |
0 commit comments