This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2.Lexical_Analyzer_using_c.c
339 lines (299 loc) · 7.57 KB
/
2.Lexical_Analyzer_using_c.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// default keywords, symbols and operators of 'c'.
const char keywords [32][10] = { "int", "char", "short", "float", "auto", "double", "struct", "break", "if", "else",
"long", "switch", "case", "enum", "register", "typedef","extern", "return", "union",
"const", "unsigned", "continue", "for", "signed", "void", "default", "goto", "sizeof",
"volatile", "do", "static", "while" };
int main()
{
// Declaring variables
FILE *ptr;
char ch, buffer[30] = "\0";
int flag_dot_count = 1, lines_count = 0;
// Function prototyping
int is_keyword ( const char buffer [] );
int is_identifier ( const char buffer [] );
int is_operator ( const char ch );
int is_special_symbol ( const char ch );
// Assigning file address to file pointer
ptr = fopen ("code.c", "w");
// Checking for any possible errors due to file operation
if( ptr == NULL )
{
puts("Error occured while creating ( or ) opeaning the file...");
puts("Press enter to continue...");
fflush(stdin);
getchar();
exit(0);
}
// displaying prompt before writing into the file
system("cls");
fflush(stdin);
puts("Enter the code : \n");
// writing data into file
while( (ch = getchar()) != EOF )
putc(ch,ptr);
// reassigning file pointer as read mode
fclose(ptr);
ptr = fopen("code.c", "r");
// Checking for any possible errors due to file operation
if( ptr == NULL )
{
puts("\nError occured while reading the file...");
puts("Press enter to continue...");
fflush(stdin);
getchar();
exit(0);
}
// Prompt before displaying the tokens.
puts("\n--------------------------------------------");
puts("\t\tTokens");
puts("\n--------------------------------------------\n");
// token generator logic
for ( int i = 0; (ch = getc(ptr)) != EOF; i = 0, buffer[0] = '\0' )
{
// check only for identifiers and keywords
if(isalpha(ch) || ch == '_')
{
buffer[i++] = ch;
while( (ch = getc(ptr)) != EOF )
{
if( isalnum(ch) || ch == '_' )
buffer[i++] = ch;
else
break;
}
buffer[i] = '\0';
if( is_keyword ( buffer ) )
printf("%-10s is an keyword.\n",buffer);
else if( is_identifier( buffer ) )
printf("%-10s is an identifier.\n",buffer);
else
printf("%-10s is an unknown token.\n",buffer);
ungetc(ch,ptr);
}
// check only for numericals ( like intergers, floating point numbers )
else if ( isdigit(ch) )
{
buffer[i++] = ch;
while( (ch = getc(ptr) ) != EOF )
{
if( isdigit(ch) || ( ch == '.' && flag_dot_count ) )
{
if( ch == '.' )
flag_dot_count = 0;
buffer[i++] = ch;
}
else
break;
}
if( flag_dot_count )
printf("%-10d is an numerical.\n", atoi(buffer));
else
printf("%-10lf is an numerical.\n", atof(buffer));
ungetc(ch,ptr);
}
// check only for string or character literal
else if( ch == '\'' || ch == '\"' )
{
// checking for string literal
if( ch == '\"' )
{
int pervious_index = -1, esq_flag = 0, end_flag = 1;
buffer[i++] = ch;
while( (ch = getc(ptr)) != EOF )
{
buffer[i++] = ch;
// checking for escape sequences in sting literal
if ( esq_flag && i == pervious_index+1 )
{
if ( ch == '\\' // Backslash
|| ch == 'a' // Alarm or Beep
|| ch == 'b' // Backspace
|| ch == 'f' // Form Feed
|| ch == 'n' // New Line
|| ch == 'r' // Carriage Return
|| ch == 't' // Tab (Horizontal)
|| ch == 'v' // Vertical Tab
|| ch == '\'' // Single Quote
|| ch == '\"' // Double Quote
|| ch == '0' ) // Null
{
esq_flag = 0;
}
}
// checking for presence of escape sequences
if( ch == '\\' )
{
esq_flag = 1;
pervious_index = i;
}
// checking for the proper end of string literal
if( ch == '\"' && buffer[i-2] != '\\' )
{
end_flag = 0;
break;
}
}
// if ch == EOF then we must replace the last index with '\0' to avoid the new line in output
if( end_flag )
buffer[i-1] = '\0';
else
buffer[i] = '\0';
// printing status of the token
if(esq_flag || end_flag)
printf("%-10s is an invalid string literal",buffer);
else
printf("%-10s is an string literal.\n",buffer);
}
// checking for character literal
else
{
buffer[i++] = ch;
while( (ch = getc(ptr)) != EOF )
{
buffer[i++] = ch;
if( ch == '\'' )
break;
}
buffer[i] = '\0';
if( strlen(buffer) == 3 ||
( strlen(buffer) == 4 && buffer[1] == '\\'
&& ( buffer[2] == '\\' // Backslash
|| buffer[2] == 'a' // Alarm or Beep
|| buffer[2] == 'b' // Backspace
|| buffer[2] == 'f' // Form Feed
|| buffer[2] == 'n' // New Line
|| buffer[2] == 'r' // Carriage Return
|| buffer[2] == 't' // Tab (Horizontal)
|| buffer[2] == 'v' // Vertical Tab
|| buffer[2] == '\'' // Single Quote
|| buffer[2] == '\"' // Double Quote
|| buffer[2] == '0' ) ) )
printf("%-10s is an charcter literal.\n",buffer);
else
printf("%-10s is an invalid charcter literal.\n",buffer);
}
}
// check for comments
else if ( ch == '/' )
{
// storing in buffer and reading the next element in the file
buffer[i++] = ch;
ch = getc(ptr);
// making sure that it's a comment
if( ch != '/' && ch != '*' )
{
printf("%-10c is an operator.\n",buffer[0]);
ungetc(ch,ptr);
}
// for single-line comment
else if ( ch == '/' )
{
while( (ch = getc(ptr)) != EOF )
{
if( ch == '\n' )
{
lines_count++;
break;
}
}
}
// for multi-line comment
else if ( ch == '*' )
{
buffer[i++] = ch;
while( (ch = getc(ptr)) != EOF )
{
buffer[i++] = ch;
if ( ch == '\n' )
lines_count++;
else if ( ch == '/' && buffer[i-1] == '*' && i-1 > 1 )
break;
}
buffer[i-1] = '\0';
// checking for possible invalid comments
if( ch == EOF )
printf("%-10s is an invalid multi-line comment\n",buffer);
}
}
// check only for special symbols and operator
else if( ch != ' ' && ch != '\n' && ch != '\t' )
{
if( is_operator(ch) )
printf("%-10c is an operator.\n", ch);
else if ( is_special_symbol(ch) )
printf("%-10c is a special symbol.\n", ch );
}
// check for the delimitors
else
{
// counting the number of lines
if( ch == '\n' )
lines_count++;
continue;
}
}
printf("\nNumber of lines : %d", lines_count);
fclose(ptr);
return 0;
}
int is_keyword ( const char buffer [] )
{
for( int i = 0; i < 32; ++i )
if( strcmp( buffer, keywords[i] ) == 0 )
return 1;
return 0;
}
int is_identifier ( const char buffer [] )
{
if( buffer[0] == '_' || isalpha(buffer[0]) )
{
if( strlen(buffer) == 1 )
return 1;
int i,flag = 1;
for( i = 1; i < strlen(buffer); ++i )
flag = ( isalnum(buffer[i]) ) ? 0 : 1;
return (flag) ? 0 : 1;
}
return 0;
}
int is_operator ( const char ch )
{
if ( ch == '+'
|| ch == '-'
|| ch == '*'
|| ch == '/'
|| ch == '%'
|| ch == '!'
|| ch == '&'
|| ch == '<'
|| ch == '>'
|| ch == '='
|| ch == '|' )
return 1;
return 0;
}
int is_special_symbol ( const char ch )
{
if ( ch == ','
|| ch == '.'
|| ch == '('
|| ch == ')'
|| ch == ';'
|| ch == '$'
|| ch == ':'
|| ch == '#'
|| ch == '['
|| ch == ']'
|| ch == '{'
|| ch == '}'
|| ch == '~'
|| ch == '-'
|| ch == '?' )
return 1;
return 0;
}