-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.c
664 lines (474 loc) · 17.6 KB
/
lex.c
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// #include "defines.h"
int isCharacterValid(char character);
int stateAnalyzer (FILE *input, char *output, char *encodedOutput);
int doesCharRequireClosure (char character);
void initializeCharArray (char *characterArray);
int isSensitiveWord (char *inputString);
int getPunctuationIdentifier(char inputCharacter);
char peek(FILE *stream);
void checkNumberAndAddToOutput();
void checkCharacterForComplexAndAddIt();
void addLexeme(char input_characters[30]);
char * getLexeme(int position);
void static flagLexemeAsUsed();
// Variables (global for helping with code-cleanup):
int nextToken;
size_t defaultSize = 50;
char stringContainer[BIG_CHARACTER_ARRAY_LENGTH];
char currentWord[BIG_CHARACTER_ARRAY_LENGTH];
char buffer[DEFAULT_WORD_ARRAY_SIZE];
int stateAnalyzerReturnStatus;
char tempCharArray[512];
char character1;
char character2;
int i = 0;
int closureState;
char tempForGetc;
int tempPunctuationIdentifier;
int state = STATE_WHITESPACE;
// The list of all the valid characters in our language:
const char validCharacters[74] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','v','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9',
'+','-','*','/','<','>','=',':',';',',','{','}'};
// If the parameter "actionRequest" is "2", "lex" will return the Peeked next Token:
int lex(){
// If analysis isn't done yet, analyse. Else, return the next token:
if (analysis_done == 0) {
printf(" \n");
printf("Starting lex. Analysis Done is: %d \n\n", analysis_done);
stateAnalyzerReturnStatus = stateAnalyzer(input, output, encodedOutput);
analysis_done = 1;
printf("State analyzer return status = %d \n\n", stateAnalyzerReturnStatus);
if(stateAnalyzerReturnStatus == STATE_EOF){
// Return the final lexicographical translation:
printf("-----------------------------\n");
printf("Status: File Reading is Over.\n");
printf("-----------------------------\n\n");
printf("Result: The Lex Output is '%s' \n\n", output);
printf("Result: The EncodedOutput is '%s' \n\n", encodedOutput);
// Show the Lexeme List contents:
printf("The Lexeme List contains: \n");
int y;
for( y=0; y<lexeme_counter; y++) {
printf("%s, ", getLexeme(y));
}
printf("\n");
// Here begins the tokenization of the encodedOutput string:
nextToken = atol(strtok(encodedOutput, " "));
// And here we fill the peekToken for the first time:
char * tempTokenizerOutput;
tempTokenizerOutput = strtok(NULL, " ");
// And we fill the next Lexeme:
char * temp_next_lexeme = getLexeme(lexeme_parsing_counter);
strcpy(currentLexeme, temp_next_lexeme);
lexeme_parsing_counter++;
if ( tempTokenizerOutput != NULL ) {
peekToken = atol(tempTokenizerOutput);
} else {
peekToken = STATE_EOF;
}
return ( nextToken );
} else {
printf("Error: Parsing could not be completed because of Errors.");
}
} else if ( analysis_done == 1 ) {
// Note: When you pass the NULL-pointer as the first 'strtok' argument, it continues the parsing
// from the place it was left at the first time. So, from now on, every time we call it, we will
// get the next token of the 'encodedOutput' that is delimited by whitespace:
// First, we move the peekToken to the nextToken to serve it to any function that requires it:
nextToken = peekToken;
if ( nextToken != STATE_EOF ) {
// Then, we fill the now empty 'peekToken' with the up-next Token in the series:
char * tempTokenizerOutput;
tempTokenizerOutput = strtok(NULL, " ");
if( tempTokenizerOutput != NULL ) {
peekToken = atol(tempTokenizerOutput);
} else {
peekToken = STATE_EOF;
}
// Getting the next Lexeme in the list:
char * temp_next_lexeme = getLexeme(lexeme_parsing_counter);
strcpy(currentLexeme, temp_next_lexeme);
lexeme_parsing_counter++;
printf("Next token: %d \n", nextToken);
printf("Peek token: %d \n", peekToken);
printf("Next lexeme: %s \n\n", currentLexeme);
return( nextToken );
} else {
printf("Debug: The TOKENIZER has reached the END of input!\n\n");
peekToken = STATE_EOF;
return( STATE_EOF );
}
}
// The code (probably) never reaches this point:
return(0);
}
int stateAnalyzer (FILE *input, char *output, char *encodedOutput) {
initializeCharArray(currentWord);
while ( state != STATE_ERROR && state != STATE_EOF ){
// Reading the next and peeking at the second next character:
character1 = getc(input);
character2 = peek(input);
// Debug:
printf("State: %d \n", state);
printf("Character 1 = '%c' \n", character1);
printf("Character 2 = '%c' \n\n", character2);
if ( character1 == EOF ) {
state = STATE_EOF;
} else if (state == STATE_WHITESPACE && isspace(character1)) { // State Whitespace: Loop getting whitespace.
state = STATE_WHITESPACE;
} else if (state == STATE_WHITESPACE && isalpha(character1)) { // State Variable: Starts with alphanumeric -> It's a variable
initializeCharArray(currentWord);
i=0;
state = STATE_VARIABLE;
if(i<30) {
currentWord[i] = character1;
};
i++;
} else if (state == STATE_VARIABLE && (isalpha(character1)||isdigit(character1)) ) { // State Variable: Getting letter or number after the first being a letter -> It's still a variable
state = STATE_VARIABLE;
printf("Inside building currentWord: '%s'\n", currentWord);
if(i<30) {
currentWord[i] = character1;
};
i++;
} else if (state == STATE_VARIABLE && isspace(character1)) { // State Variable -> Whitespace: Ending a variable when encountering whitespace
state = STATE_WHITESPACE;
printf("Debug Sensitive Word Identification: Current word contains: %s \n", currentWord);
// Here we add the variable identifier OR the sensitive identifier to our text buffers (encoded and original).
int isItSensitiveWord = isSensitiveWord(currentWord);
// Original:
strncat(output, currentWord, i);
strncat(output, " ", i);
addLexeme(currentWord);
// Encoded:
if( isItSensitiveWord == NOT_SENSITIVE ){
sprintf(encodedOutput + strlen(encodedOutput), "%d ", VARIABLE);
} else {
sprintf(encodedOutput + strlen(encodedOutput), "%d ", isItSensitiveWord);
}
initializeCharArray(currentWord);
i=0;
} else if (state == STATE_VARIABLE && ispunct(character1)) {
printf("Inside currentWord: '%s'\n", currentWord);
state = STATE_WHITESPACE;
// Here we add the variable identifier OR the sensitive identifier to our text buffers (encoded and original).
int isItSensitiveWord = isSensitiveWord(currentWord);
// Original:
strncat(output, currentWord, i);
addLexeme(currentWord);
strncat(output, " ", i);
// Encoded:
if( isItSensitiveWord == NOT_SENSITIVE ){
sprintf(encodedOutput + strlen(encodedOutput), "%d ", VARIABLE);
} else {
sprintf(encodedOutput + strlen(encodedOutput), "%d ", isItSensitiveWord);
}
initializeCharArray(currentWord);
i=0;
checkCharacterForComplexAndAddIt();
} else if (state == STATE_WHITESPACE && isdigit(character1)) { // State Number: Starts with digit -> It's an integer
state = STATE_NUMBER;
if(i<30) {
strncat(currentWord, &character1, 1);
};
i++;
} else if (state == STATE_NUMBER && isdigit(character1)) { // State Number: And we keep getting numbers
state = STATE_NUMBER;
if(i<30) {
strncat(currentWord, &character1, 1);
};
i++;
} else if (state == STATE_NUMBER && isalpha(character1)) { // We are in state Number and we get a letter, so, ERROR.
state = STATE_ERROR;
} else if (state == STATE_NUMBER && isspace(character1)) { // The number has ended with whitespace, so we save it:
checkNumberAndAddToOutput();
} else if (state == STATE_NUMBER && ispunct(character1)) {
checkNumberAndAddToOutput();
// ...and then, we check the punctuation symbol that showed up (if it's simple or complex character):
state = STATE_WHITESPACE;
initializeCharArray(currentWord);
i=0;
checkCharacterForComplexAndAddIt();
// Here begin the composite punctuation identifications:
} else if (state == STATE_WHITESPACE && ispunct(character1)) {
state = STATE_WHITESPACE;
initializeCharArray(currentWord);
i=0;
checkCharacterForComplexAndAddIt();
}
}
if (state == STATE_EOF) {
printf("Parsing ended with STATE_EOF.\n\n");
printf("This is inside output: %s \n\n", output);
printf("This is inside encodedOutput: %s \n\n", encodedOutput);
return(STATE_EOF);
} else {
return(STATE_ERROR);
}
}
int isCharacterValid (char character) {
int i=0;
// First we check if the current character belongs to our valid alphabet:
for(i=0; i<74; i++){
if( character == validCharacters[i] ){
return(1);
}
}
// If no valid equivalent found:
return(0);
}
int doesCharRequireClosure (char character) {
printf("The character to be examined is %c\n\n", character);
if( character == '+' | character == '-' | character == '*' | character == '=' | character == ',' | character == ';' | character == '{'| character == '[' | character == '(' | character == '}' | character == ']' | character == ')') {
return (SIMPLE_CHARACTER);
} else if ( character == '<'| character == '>'| character == ':' | character == '/') {
return (POSSIBLE_COMPLEX_CHAR);
} else {
return (STATE_ERROR);
}
}
void initializeCharArray (char *characterArray) {
int i=0;
for(i=0; i<BIG_CHARACTER_ARRAY_LENGTH; i++){
characterArray[i] = '\0';
};
}
char peek(FILE *stream) {
char c;
c = fgetc(stream);
ungetc(c, stream);
return c;
}
int isSensitiveWord (char *inputString){
// Sensitive words
if( strcmp(inputString, "and") == 0){
return (and_a);
} else if( strcmp(inputString, "var") == 0){
return (var_a);
} else if( strcmp(inputString, "do") == 0){
return (do_a);
} else if( strcmp(inputString, "else") == 0){
return (else_a);
} else if( strcmp(inputString, "procedure") == 0){
return (procedure_a);
} else if( strcmp(inputString, "function") == 0){
return (function_a);
} else if( strcmp(inputString, "print") == 0){
return (print_a);
} else if( strcmp(inputString, "if") == 0){
return (if_a);
} else if( strcmp(inputString, "in") == 0){
return (in_a);
} else if( strcmp(inputString, "inout") == 0){
return (inout_a);
} else if( strcmp(inputString, "not") == 0){
return (not_a);
} else if( strcmp(inputString, "for") == 0){
return (for_a);
} else if( strcmp(inputString, "to") == 0){
return (to_a);
} else if( strcmp(inputString, "step") == 0){
return (step_a);
} else if( strcmp(inputString, "program") == 0){
return (program_a);
} else if( strcmp(inputString, "or") == 0){
return (or_a);
} else if( strcmp(inputString, "return") == 0){
return (return_a);
} else if( strcmp(inputString, "while") == 0){
return (while_a);
} else if( strcmp(inputString, "call") == 0){
return (call_a);
} else {
return(NOT_SENSITIVE);
}
}
int getPunctuationIdentifier(char inputCharacter){
if( inputCharacter == '+' ){
return (plus);
} else if( inputCharacter == '-' ) {
return (minus);
} else if( inputCharacter == '*' ) {
return (multipl);
} else if( inputCharacter == '/' ) {
return (divide);
} else if( inputCharacter == '<' ) {
return (lessthan);
} else if( inputCharacter == '>' ) {
return (morethan);
} else if( inputCharacter == '=' ) {
return (equals);
} else if( inputCharacter == ';' ) {
return (semicolon);
} else if( inputCharacter == ',' ) {
return (comma);
} else if( inputCharacter == '{' ) {
return (curlbrackleft);
} else if( inputCharacter == '}' ) {
return (curlbrackright);
} else if( inputCharacter == '(' ) {
return (parenthleft);
} else if( inputCharacter == ')' ) {
return (parenthright);
} else if( inputCharacter == '[' ) {
return (bracketleft);
} else if( inputCharacter == ']' ) {
return (bracketright);
} else {
return(NOT_SENSITIVE);
}
}
void checkNumberAndAddToOutput() {
// First, we save the number that we have until now (if it's valid):
char* ptr;
// Check for the integer size restriction (between -32767 and 32767):
if ( abs((int)strtol(currentWord, &ptr, 10)) < 32767 ) {
state = STATE_WHITESPACE;
sprintf(output + strlen(output), "%s ", currentWord);
addLexeme(currentWord);
sprintf(encodedOutput + strlen(encodedOutput), "%d ", INTEGER);
initializeCharArray(currentWord);
i=0;
} else {
error("[Lexicographical Analyzer] Integers must be between -32767 and 32767.");
state = STATE_ERROR;
}
}
void checkCharacterForComplexAndAddIt() {
closureState = doesCharRequireClosure(character1);
printf("Does char require closure? Answer: %d\n\n", closureState);
// Discern between a common punctuation character and one that needs closure:
if( closureState == SIMPLE_CHARACTER ){
strncat(output, &character1, 1);
strncat(output, " ", 1);
addLexeme("ENCODED");
tempPunctuationIdentifier = getPunctuationIdentifier(character1);
printf("The punctuation identifier is: %d\n\n", tempPunctuationIdentifier);
sprintf(encodedOutput + strlen(encodedOutput), "%d ", tempPunctuationIdentifier);
} else if ( closureState == POSSIBLE_COMPLEX_CHAR ) {
if ( character1 == '<' && character2 != '=' && character2 != '>') {
strncat(output, "<", 1);
strncat(output, " ", 1);
addLexeme("ENCODED");
tempPunctuationIdentifier = getPunctuationIdentifier('<');
sprintf(encodedOutput + strlen(encodedOutput), "%d ", tempPunctuationIdentifier);
} else if( character1 == '<' && character2 == '=') {
getc(input); // Get the peeked character out of the way for the next loop
strncat(output, "<=", 2);
strncat(output, " ", 1);
addLexeme("ENCODED");
tempPunctuationIdentifier = lessequals;
sprintf(encodedOutput + strlen(encodedOutput), "%d ", tempPunctuationIdentifier);
} else if ( character1 == '<' && character2 == '>') {
getc(input); // Get the peeked character out of the way for the next loop
strncat(output, "<>", 2);
strncat(output, " ", 1);
addLexeme("ENCODED");
tempPunctuationIdentifier = different;
sprintf(encodedOutput + strlen(encodedOutput), "%d ", tempPunctuationIdentifier);
} else if ( character1 == '>' && character2 == '=') {
getc(input); // Get the peeked character out of the way for the next loop
strncat(output, ">=", 2);
strncat(output, " ", 1);
addLexeme("ENCODED");
tempPunctuationIdentifier = moreequals;
sprintf(encodedOutput + strlen(encodedOutput), "%d ", tempPunctuationIdentifier);
} else if ( character1 == '>' && character2 != '=') {
strncat(output, ">", 1);
strncat(output, " ", 1);
addLexeme("ENCODED");
tempPunctuationIdentifier = getPunctuationIdentifier('>');
sprintf(encodedOutput + strlen(encodedOutput), "%d ", tempPunctuationIdentifier);
} else if ( character1 == '/' && character2 == '*') {
printf("We know that a comment section begins.\n\n");
// Here we know that comments open, so we go through the input to find
// their closing point, and ignore everything between the comment symbols:
character1 = getc(input);
character2 = peek(input);
printf("Character1: %c\n", character1);
printf("Character2: %c\n\n", character2);
while ( (character1 != '*') || (character2 != '/') ) {
character1 = getc(input);
character2 = peek(input);
printf("loop.\n");
if( character1 == EOF ){
// This means that the file ends before the comments close
state = STATE_ERROR;
}
}
getc(input);
// Get the peeked character (in our case '/') out of the way
} else if ( character1 == '/' && character2 != '*') {
strncat(output, "/", 1);
strncat(output, " ", 1);
addLexeme("ENCODED");
tempPunctuationIdentifier = getPunctuationIdentifier('/');
sprintf(encodedOutput + strlen(encodedOutput), "%d ", tempPunctuationIdentifier);
} else if ( character1 == ':' && character2 == '=' ) {
getc(input);
strncat(output, ":=", 2);
strncat(output, " ", 1);
addLexeme("ENCODED");
tempPunctuationIdentifier = assign;
sprintf(encodedOutput + strlen(encodedOutput), "%d ", tempPunctuationIdentifier);
} else if ( character1 == '*' && character2 == '/'){
// This error happens if we find a comment-close character before we find a comment-open one:
state = STATE_ERROR;
}
} else if (closureState == STATE_ERROR){
state = STATE_ERROR;
}
}
void addLexeme(char input_characters[30]) {
lexeme_counter++;
// Create the new node to be inserted in our list:
lexeme *new_lexeme = malloc(sizeof(lexeme));
// Add the data to the new lexeme struct:
strcpy(new_lexeme->word, input_characters);
new_lexeme->isItUsed = 0;
new_lexeme->next = NULL;
// If the list is empty, make this new node the head:
if ( lexeme_head == NULL ) {
lexeme_head = new_lexeme;
} else {
lexeme *current = lexeme_head;
while ( current->next != NULL ) {
current = current->next;
}
// We reach this point, when we are at the last node:
current->next = new_lexeme;
}
}
char * getLexeme(int position) {
int i=0;
lexeme *lexemePointer = lexeme_head;
while ( lexemePointer != NULL && i <= position ) {
if (i == position ) {
return (lexemePointer->word);
}
lexemePointer = lexemePointer->next;
i++;
}
return(" ");
}
// This function sets a flag inside the Lexeme struct, every time it is used/saved
// as a Varibale or Function name for use in Intermediate Code Generation:
void flagLexemeAsUsed(){
int i=0;
lexeme *lexemePointer = lexeme_head;
while ( lexemePointer != NULL ) {
lexemePointer = lexemePointer->next;
i++;
if ( i == lexeme_parsing_counter-1 ) {
lexemePointer->isItUsed = 1;
}
}
}