-
Notifications
You must be signed in to change notification settings - Fork 7
/
LexicalAnalyzer.c
540 lines (421 loc) · 16.5 KB
/
LexicalAnalyzer.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
/**********************************************************************************************
* Homework #2 (Lexical Analyzer)
*
* Programmer: Erwin Holzhauser
*
* Due Date: February 16, 2014, @ 11:59 PM
*
* COP3402-0001, Spring 2014 Instructor: Euripides Montagne
*
* Description: Implementation of a lexical analyzer for the programming language PL/0.
* Capable of reading a PL/0 source program, identifying some errors, and
* producing as output: the source program, and a lexeme table and list for the
* source program.
*
* Input: File containing PL/0 program, with the following constraints:
* (1) Max length of identifiers is 11 characters.
* (2) Max length of numbers is 5 digits in length.
* (3) Comments and invisible characters are ignored.
* (4) Does not have to be grammatically valid PL/0 code.
*
* Output: (1) source_program.txt : Copy of input file.
* (2) lexeme_table.txt : Lexeme table for source program.
* (3) lexeme_list.txt : Lexeme list for source program.
*
**********************************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**CONSTANTS**/
#define MAX_ID_LEN 11
#define MAX_NUM_LEN 5
/**FUNCTION PROTOTYPES**/
//Input: String, s.
//Output: If s is a reserved word, return the numerical token value; otherwise, return -1.
int isReservedWord( char* s );
//Input: String, s.
//Output: If s is a special symbol, return 1; otherwise, return -1.
int isSpecialSymbol( char c );
//Input: Token, s.
//Output: If the token contains a lexical error, print an error message and return 1; otherwise, return 0.
int detectError( char *s );
//Input: Valid token, s.
//Output: Return the numerical token value of s.
int isOtherValid( char *s );
/**MAIN FUNCTION**/
int main( void ){
int i; //Loop counter.
int tempTokCtr = 0; //Count number of tokens.
char buffer[ 10000 ]; //For reading in tokens.
char temp[ 10000 ]; //Back-up buffer.
//Prompt user for source program name.
char sp[50]; //Name of input file.
printf( "What is the file name for your source program?\n" );
scanf( "%s", sp );
//Open file input stream to main input (the program).
FILE *fin = fopen( sp, "r" );
//Open file output stream to intermediary output file.
//In this file, unprocessed tokens will be stored for further processing.
FILE *temp_fout = fopen( "temp_output.txt", "w" );
//Procedure for creating intermediary output.
/** BEGIN PROCEDURE **/
while( fscanf( fin, "%s", buffer ) != EOF ){
int len = (int) strlen( buffer ); //Length of read-in token.
//For the length of the read-in token:
for( i = 0; i < len; i++ ){
//If we've reached the end of the buffer, and it's not a special symbol, print the token.
//If invalid symbols are present, they're ignored and taken care of later.
if( i == len-1 && !isSpecialSymbol( buffer[ i ] ) ){
fprintf( temp_fout, "%s ", buffer );
tempTokCtr++; //Increment the token counter.
}
//If the i-th element of the token is a special symbol.
else if( isSpecialSymbol( buffer[ i ] ) ){
//Brute force examine particular token cases: comment tokems, not equal, less-than-or-equal, ... , null, and odd.
if( len > 1 ){
if( buffer[ i ] == '/' && buffer[ i+1 ] == '*' ){
strncpy( temp, buffer, i );
temp[ i ] = '\0';
fprintf( temp_fout, "%s ", temp );
tempTokCtr++;
fprintf( temp_fout, "/* " );
tempTokCtr++;
strncpy( buffer, &buffer[i]+2, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
else if( buffer[ i ] == '*' && buffer[ i+1 ] == '/' ){
strncpy( temp, buffer, i );
temp[ i ] = '\0';
fprintf( temp_fout, "%s ", temp );
tempTokCtr++;
fprintf( temp_fout, "*/ " );
tempTokCtr++;
strncpy( buffer, &buffer[i]+2, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
else if( buffer[ i ] == '!' && buffer[ i+1 ] == '=' ){
strncpy( temp, buffer, i );
temp[ i ] = '\0';
fprintf( temp_fout, "%s ", temp );
tempTokCtr++;
fprintf( temp_fout, "!= " );
tempTokCtr++;
strncpy( buffer, &buffer[i]+2, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
else if( buffer[ i ] == '<' && buffer[ i+1 ] == '=' ){
strncpy( temp, buffer, i );
temp[ i ] = '\0';
fprintf( temp_fout, "%s ", temp );
tempTokCtr++;
fprintf( temp_fout, "<= " );
tempTokCtr++;
strncpy( buffer, &buffer[i]+2, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
else if( buffer[ i ] == '>' && buffer[ i+1 ] == '=' ){
strncpy( temp, buffer, i );
temp[ i ] = '\0';
fprintf( temp_fout, "%s ", temp );
tempTokCtr++;
fprintf( temp_fout, ">= " );
tempTokCtr++;
strncpy( buffer, &buffer[i]+2, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
else if( buffer[ i ] == ':' && buffer[ i+1 ] == '=' ){
strncpy( temp, buffer, i );
temp[ i ] = '\0';
fprintf( temp_fout, "%s ", temp );
tempTokCtr++;
fprintf( temp_fout, ":= " );
tempTokCtr++;
strncpy( buffer, &buffer[i]+2, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
else if( len > 3 && buffer[ i ] == 'n' && buffer[ i+1 ] == 'u' && buffer[ i+2 ] == 'l' && buffer[ i+3 ] =='l' ){
strncpy( temp, buffer, i );
temp[ i ] = '\0';
fprintf( temp_fout, "%s ", temp );
tempTokCtr++;
fprintf( temp_fout, "null " );
tempTokCtr++;
strncpy( buffer, &buffer[i]+4, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
else if( len > 2 && buffer[ i ] == 'o' && buffer[ i+1 ] == 'd' && buffer[ i+2 ] == 'd' ){
strncpy( temp, buffer, i );
temp[ i ] = '\0';
fprintf( temp_fout, "%s ", temp );
tempTokCtr++;
fprintf( temp_fout, "odd " );
tempTokCtr++;
strncpy( buffer, &buffer[i]+3, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
}
//Special symbol token at the beginning.
if( i == 0 ){
//Print symbol as individual token.
fprintf( temp_fout, "%c ", buffer[i] );
tempTokCtr++; //Increment token counter.
//Shift buffer, and update loop index.
//Similar procedure used for brute force token examination, and further token examination.
strncpy( buffer, &buffer[i]+1, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
//Valid symbol is splitting tokens within the buffer; split, print, and shift the tokens left of the valid symbol, including the symbol.
if( i > 0 ){
strncpy( temp, buffer, i );
temp[ i ] = '\0';
fprintf( temp_fout, "%s ", temp );
tempTokCtr++;
fprintf( temp_fout, "%c ", buffer[i] );
tempTokCtr++;
strncpy( buffer, &buffer[i]+1, len-1 );
buffer[ len - 1 ] = '\0';
len = (int) strlen( buffer );
i = -1;
}
}
}
}
/** END OF PROCEDURE **/
//Resource management.
fclose( temp_fout );
fclose( fin );
//Open file input stream to intermediary output created by the procedure above (unprocessed tokens).
fin = fopen( "temp_output.txt", "r" );
FILE *table = fopen( "lexeme_table.txt", "w" ); //Will hold the lexeme table.
FILE *list = fopen( "lexeme_list.txt", "w" ); //Will hold the lexeme list.
//Print header for lexeme table.
fprintf( table, "Lexeme Table:\n%15s%15s\n", "lexeme", "token type");
//Procedure to examine printed tokens.
/** BEGIN PROCEDURE **/
//Initialize.
int isResWord = -1; //If currently examined token is a reserved word, != -1; otherwise, = -1.
int len = 0; //Length of examined token.
int isComment = 0; //If currently reading in a comment, = 1; otherwise, = 0.
//For the length of tokens.
while( fscanf( fin, "%s ", buffer ) != EOF ){
//fscanf( fin, "%s", buffer ); //Read in token.
len = (int) strlen( buffer ); //Get token string length.
//Recognize beginning of comment block.
if( !strcmp( buffer, "/*" ) ){
isComment = 1;
}
//Recognize end of comment block.
else if( isComment == 1 && !strcmp( buffer, "*/" ) ){
isComment = 0;
}
//Only read in tokens while not examining a comment block.
else if( !isComment ){
//Error detection by token.
if( detectError( buffer ) ){
fclose( fin );
fclose( table );
fclose( list );
exit( 0 );
}
//Is the current token a reserved word? Print to table and list appropriately.
isResWord = isReservedWord( buffer );
if( isResWord != -1 ){
fprintf( table, "%15s%15d\n", buffer, isResWord );
fprintf( list, "%d ", isResWord );
}
//Is the current token an identifier? Print to table and list appropriately.
else if( isalpha( buffer[0] ) ){
fprintf( table, "%15s%15d\n", buffer, 2 );
fprintf( list, "2 %s ", buffer );
}
//Is the current token a number? Print to table and list appropriately.
else if( isdigit( buffer[0] ) ){
fprintf( table, "%15s%15d\n", buffer, 3 );
fprintf( list, "3 %s ", buffer );
}
//Otherwise, our token is some other valid symbol (checked for errors above). Print appropriately.
else{
fprintf( table, "%15s%15d\n", buffer, isOtherValid( buffer ) );
fprintf( list, "%d ", isOtherValid( buffer ) );
}
}
}
//Resource management.
fclose( fin );
fclose( table );
fclose( list );
/** END OF PROCEDURE **/
//Procedure to copy and source program.
/** BEGIN PROCEDURE **/
fin = fopen( sp, "r" ); //File containing source program.
FILE *source = fopen( "source_program.txt", "w" ); //File to copy source program to.
char copy; //Used to copy file, character by character.
//Copy character by character, until end of file.
while( ( copy = fgetc( fin ) ) != EOF )
fputc( copy, source );
//Resource management.
fclose( fin );
fclose( source );
remove( "temp_output.txt" ); //Delete intermediary output.
/** END OF PROCEDURE **/
return 0;
} //End of main.
/**FUNCTION DECLARATIONS**/
int detectError( char *s ){
int i;
int len = (int) strlen( s );
//Check for single colon token.
if( len == 1 && s[0] == ':' ){
printf( "Error: Invalid Symbol: :\n" );
return 1;
}
//Check for invalid symbol.
for( i = 0; i < len; i++ ){
if( !isalnum(s[i]) && !isSpecialSymbol( s[i] ) ){
printf( "Error: Invalid Symbol: %c\n", s[i] );
return 1;
}
}
//Single letter & single number accepted, so if s > 1.
if( len > 1 ){
//Check identifier length.
if( isalpha( s[0] ) ){
for( i = 1; i < len; i++ ){
if( i > MAX_ID_LEN ){
printf( "Error: Variable name: %s is too long.\n", s );
return 1;
}
}
}
if( isdigit( s[0] ) ){
//Check invalid identifier (starts with number).
for( i = 1; i < len; i++ ){
if( isalpha( s[i] ) ){
printf( "Error: Variable name: %s begins with a number.\n", s );
return 1;
}
}
//Check number length.
if( i > MAX_NUM_LEN ){
printf( "Error: Number %s is too long.\n", s );
return 1;
}
}
}
return 0;
} //End of detectError.
int isSpecialSymbol( char c ){
if( c == '+' )
return 1;
else if( c == '-' )
return 1;
else if( c == '*' )
return 1;
else if( c == '/' )
return 1;
else if( c == '(' )
return 1;
else if( c == ')' )
return 1;
else if( c == '=' )
return 1;
else if( c == ',' )
return 1;
else if( c == '.' )
return 1;
else if( c == '<' )
return 1;
else if( c == '>' )
return 1;
else if( c == ';' )
return 1;
else if( c == ':' )
return 1;
return 0;
} //end of isSpecialSymbol.
int isReservedWord( char* s ){
if( !strcmp( s, "const" ) )
return 28;
else if( !strcmp( s, "int" ) )
return 29;
else if( !strcmp( s, "procedure" ) )
return 30;
else if( !strcmp( s, "call" ) )
return 27;
else if( !strcmp( s, "begin" ) )
return 21;
else if( !strcmp( s, "end" ) )
return 22;
else if( !strcmp( s, "if" ) )
return 23;
else if( !strcmp( s, "then" ) )
return 24;
else if( !strcmp( s, "else" ) )
return 33;
else if( !strcmp( s, "while" ) )
return 25;
else if( !strcmp( s, "do" ) )
return 26;
else if( !strcmp( s, "read" ) )
return 32;
else if( !strcmp( s, "write" ) )
return 31;
return -1;
} //End of isReservedWord.
int isOtherValid( char *s ){
if( !strcmp( s, "null" ) )
return 1;
else if( !strcmp( s, "+" ) )
return 4;
else if( !strcmp( s, "-" ) )
return 5;
else if( !strcmp( s, "*" ) )
return 6;
else if( !strcmp( s, "/" ) )
return 7;
else if( !strcmp( s, "<=" ) )
return 12;
else if( !strcmp( s, ">=" ) )
return 14;
else if( !strcmp( s, "(" ) )
return 15;
else if( !strcmp( s, ")" ) )
return 16;
else if( !strcmp( s, "," ) )
return 17;
else if( !strcmp( s, ";" ) )
return 18;
else if( !strcmp( s, "." ) )
return 19;
else if( !strcmp( s, ":=" ) )
return 20;
else if( !strcmp( s, "odd" ) )
return 8;
else if( !strcmp( s, "=" ) )
return 9;
else if( !strcmp( s, "!=" ) )
return 10;
else if( !strcmp( s, "<" ) )
return 11;
else if( !strcmp( s, ">" ) )
return 13;
} //End of isOtherValid.