-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
611 lines (607 loc) · 16.8 KB
/
main.c
File metadata and controls
611 lines (607 loc) · 16.8 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
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
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
FILE *f;
char str[10000000],ch;
void NLOC();
void codeMenu();
void write();
void appendCode();
void showCode();
void tokenMenu();
void specificToken(int a);
void countToken();
void keywordCount();
void operatorCount();
void identifierCount();
struct Token //structure to store token count information.
{
int total,oper,keyW;
int cons,ident,sym;
}T={0};
struct constant //structure to store constants count information.
{
int integers,decimals,strings,chr;
}con={0};
struct operator //structure to store operators
{
int arithmetic,relational,logical,bitwise,assignment,misc;
}ops={0};
int main()
{
do
{
system("cls");
printf("\n\t\t\t\t\t\t\t\t\tWelcome\n");
printf("\t\t\t\t\t\t\t\t LEXICAL ANALYSIS\n");
printf("1.Code Input\n2.NLOC\n");
printf("3.Token Count\n4.Complexity Analysis\n5.Exit\n");
printf("Enter your choice: ");
ch= getch();
switch (ch)
{
case '1':
codeMenu();
break;
case '2':
NLOC();
break;
case '3':
//under development!!
tokenMenu();
break;
case '4':
printf("under development!!");
break;
case '5':
printf("\nThank you!!");
exit(0);
break;
default:
printf("\nEnter Proper choice!!");
break;
}
printf("\nWant to continue?[y/n]");
ch= getch();
} while (ch!='n');
}
//menu driven function for code modification
void codeMenu()
{
do{
system("cls");
printf("\n\t\t\t\t\t\t\t\tCode Input\n");
printf("1.Write Code\n2.Append Code\n");
printf("3.Show Code\n4.Exit to main menu\n5.Exit\n");
printf("Enter your choice: ");
ch=getch();
switch (ch)
{
case '1':
write();
break;
case '2':
appendCode();
break;
case '3':
showCode();
break;
case '4':
main();
break;
case '5':
printf("\n Thank you!\n");
exit(0);
break;
default:
printf("\nInvalid choice entered!!\n");
break;
}
again:
printf("\n*press[y]to continue\n*press[n]to exit\n*press[b]to go back to main menu\n>_");
ch=getch();
if(ch!='y'&&ch!='b'&&ch!='n') {printf("Invalid choice!!"); goto again;}
else if(ch=='n') {printf("\nThank you");exit(0);}
}while(ch!='b');
main();
}
//menu driven function for token count
void tokenMenu()
{
int x;
do{
system("cls");
countToken();
printf("\n\t\t\t\t\t\t\t\tTOKEN COUNT\n");
printf("1.Total No of Tokens\n2.Classified Token count\n");
printf("3.Specific Token count\n4.Back To Main menu\n5.Exit\n");
printf("Enter your choice: ");
ch=getch();
switch (ch)
{
case '1':
printf("Total No of Token: %d\n",T.total);
break;
case '2':
system("cls");
printf("Operators : %d\n",T.oper);
printf("Symbols : %d\n",T.sym);
printf("Constants : %d\n",T.cons);
printf("KeyWords : %d\n",T.keyW);
printf("Identifier : %d\n",T.ident);
break;
case '3':
system("cls");
try:
printf("1.KeyWords\n2.Identifier\n3.Operators\n4.Symbols\n5.Constants\nEnter Your choice:");
scanf("%d",&x);
if(x<=5)
{
system("cls");
specificToken(x);
}
else
{
system("cls");
printf("\nInvalid choice!!Try again\n");
goto try;
}
break;
case '4':
main();
break;
case '5':
exit(0);
break;
default:
printf("Enter a Valid choice!! ");
break;
}
again:
printf("\n*press[y]to continue\n*press[n]to exit\n*press[b]to go back to main menu\n>_");
ch=getch();
if(ch!='y'&&ch!='b'&&ch!='n') {printf("Invalid choice!!"); goto again;}
else if(ch=='n') {printf("\nThank you");exit(0);}
}while(ch!='b');
main();
}
//func to calculate Tokens
void countToken()
{
memset(&T, 0, sizeof T);
memset(&con, 0, sizeof con);// using memset(memory set) predifined function to set all struc variables to 0
f= fopen("code.txt ", "r");
fseek(f,0,SEEK_END);
int const fileSize = ftell(f);int i=0;
char s[fileSize];
fseek(f,0,SEEK_SET);
s[i]=getc(f);
while(s[i]!= EOF) //while to get all char from file
{
++i;
s[i]=getc(f);
}
//for loop to count special characters (symbols)
for(int i=0;i<=strlen(s);i++)
{
if (s[i] == ',' || s[i] == ';' || s[i] == '(' || s[i] == ')' ||
s[i] == '[' || s[i] == ']' || s[i] == '{' || s[i] == '}' || s[i] == '#')
{
//printf("\n%c",s[i]); -> just for debugging
++T.sym;
}
}
//3 loops to count constant values of 3 types
/*1.for loop to count constant integers
for (i = 0; i < strlen(s);i++)
{
for(int j = i; j <strlen(s);j++)
{
if (isdigit(s[j])!=0&&s[i+1]!='.')
{
//printf("\n%c\n", s[j]); //-> just for debugging
if(isdigit(s[j+1])!=0)
continue;
else if(s[j+1]!='.')
{
++con.integers;
for(int k=j+1; k <strlen(s);k++)
{
if(isdigit(s[k])!=0)
continue;
else
i=k;
}
break;
}
}
}
} */
//2.for loop to count constant decimals
for (i = 0; i < strlen(s);i++)
{
for(int j = i; j <strlen(s);j++)
{
if (isdigit(s[j])!=0)
{
//printf("\n%c\n", s[j]); //-> just for debugging
if(isdigit(s[j+1])!=0)
continue;
else if(s[j+1]=='.')
continue;
else
{
++con.decimals;
i=j+1;
break;
}
}
}
}
//3.for loop to count constant strings
for (i = 0; i < strlen(s);i++)
{
if (s[i]=='\"')
{
for(int j = i+1; j <strlen(s);j++)
{
//printf("\n%c\n", s[j]);//just for debugging
if(s[j]!='\"')
{continue;}
else
{
//printf("\n%c\n", s[j+1]);
++con.strings;
i=j;
break;
}
}
}
else
continue;
}
//4.for loop to count constant characters
for(int i = 0; i < strlen(s);i++)
{
if(s[i]=='\''&&(s[i+2]=='\''||s[i+3]=='\''))
{
++con.chr;
if(s[i+2]=='\'')
i+=2;
else if(s[i+3]=='\'')
i+=3;
}
else
continue;
}
keywordCount();// calling function function to count keywords
operatorCount();
identifierCount();
T.oper= ops.arithmetic+ops.assignment+ops.bitwise+ops.logical+ops.relational+ops.misc;
T.cons= con.strings+con.chr+con.decimals;
T.total=T.cons+T.ident+T.keyW+T.oper+T.sym;
fclose(f);
}
//function to count operators
void operatorCount()
{
memset(&ops, 0, sizeof ops);
f= fopen("code.txt ", "r");
fseek(f,0,SEEK_END);
int const fileSize = ftell(f);int i=0;
char s[fileSize];
fseek(f,0,SEEK_SET);
s[i]=getc(f);
while(s[i]!= EOF) //while to get all char from file
{
++i;
s[i]=getc(f);
}
//for loop to count operators
for(int i = 0; i <strlen(s);i++)
{
//to count logical operators
if((s[i]=='&'&&s[i+1]=='&')||(s[i]=='|'&&s[i+1]=='|')||s[i]=='!')
{
++ops.logical;
if(s[i+1]=='&'||s[i+1]=='|')
++i;
}
//to count bitwise operators
if((s[i]=='|'&&isalnum(s[i+1])!=0)||(s[i]=='&'&&isalnum(s[i+1])!=0)||(s[i]=='^'&&isalnum(s[i+1])!=0)
||(s[i]=='~'&&isalnum(s[i+1])!=0)||(s[i]=='<'&&s[i+1]=='<')||(s[i]=='>'&&s[i+1]=='>'))
{
++ops.bitwise;
++i;
}
//to count misc operators
if((s[i]=='?'&&s[i+2]==':')||(s[i]=='*'&&s[i+1]=='*'))
{
++ops.misc;
if(s[i+2]==':')
i+=2;
else
++i;
}
//to count assignment operators
if((s[i]=='='&&s[i+1]!='=')||(s[i]=='+'&&s[i+1]=='=')||(s[i]=='-'&&s[i+1]=='='))
{
++ops.assignment;
if(s[i+1]=='=')
++i;
}
//to count relational operators
if((s[i]=='='&&s[i+1]=='=')||(s[i]=='!'&&s[i+1]=='=')||(s[i]=='<'&&s[i+1]=='=')||(s[i]=='>'&&s[i+1]=='='))
{
++ops.relational;
++i;
}
if((s[i]=='<'&&isalnum(s[i+1])!=0&&s[i-8]!='#')||(s[i]=='>'&&isalnum(s[i+1])!=0))
//s[i-8]!=# condition is exclude headerfile statements
++ops.relational;
//to count arithmetic
if((s[i]=='+'&&(s[i+1]=='+'||isalnum(s[i+1])!=0))||(s[i]=='-'&&(s[i+1]=='-'||isalnum(s[i+1])!=0))
||(s[i]=='*'&&isalnum(s[i+1])!=0))
{
++ops.arithmetic;
if(s[i+1]=='+'||s[i+1]=='-')
++i;
}
if(s[i]=='/'||s[i]=='%')
++ops.arithmetic;
}
}
//function to count keywords
void keywordCount()
{
f= fopen("code.txt ", "r");
fseek(f,0,SEEK_END);
int const fileSize = ftell(f);int i=0;
char s[fileSize]; char s2[10000];
fseek(f,0,SEEK_SET);
while(s[i]!= EOF) //while to get all char from file
{
++i;
s[i]=getc(f);
}
for(int i= 0; i <strlen(s);i++)
{
char s2[10000]={"\0"};
for(int j=i; j <strlen(s);j++)// for parsing string
{
if(isalpha(s[j])!=0&&(s[j]!=' '))
{
s2[j-i]=s[j];
}
else
{
i=j;
break;
}
}
if(s!=NULL){
if(strcmp(s2,"auto")==0||strcmp(s2,"break")==0||strcmp(s2,"case")==0
||strcmp(s2,"char")==0||strcmp(s2,"const")==0||strcmp(s2,"continue")==0||strcmp(s2,"default")==0)
T.keyW++;
else if(strcmp(s2,"do")==0||strcmp(s2,"double")==0||strcmp(s2,"else")==0||strcmp(s2,"enum")==0
||strcmp(s2,"extern")==0||strcmp(s2,"float")==0||strcmp(s2,"for")==0||strcmp(s2,"goto")==0)
T.keyW++;
else if(strcmp(s2,"if")==0||strcmp(s2,"int")==0||strcmp(s2,"long")==0||strcmp(s2,"register")==0
||strcmp(s2,"return")==0||strcmp(s2,"short")==0||strcmp(s2,"signed")==0||strcmp(s2,"sizeof")==0)
T.keyW++;
else if(strcmp(s2,"static")==0||strcmp(s2,"struct")==0||strcmp(s2,"switch")==0||strcmp(s2,"typedef")==0
||strcmp(s2,"union")==0||strcmp(s2,"unsigned")==0||strcmp(s2,"void")==0||strcmp(s2,"volatile")==0||strcmp(s2,"while")==0)
T.keyW++;
}
}
}
//function to count identifiers
void identifierCount()
{
f= fopen("code.txt ", "r");
fseek(f,0,SEEK_END);
int const fileSize = ftell(f);int i=0;
char s[fileSize]; char s2[10000];
fseek(f,0,SEEK_SET);
while(s[i]!= EOF) //while to get all char from file
{
++i;
s[i]=getc(f);
}
for(int i= 0; i <strlen(s);i++)
{
char s2[10000]={"\0"};
for(int j=i; j <strlen(s);j++)// for parsing string
{
if(isalpha(s[j])!=0&&(s[j]!=' '))
{
s2[j-i]=s[j];
}
else
{
i=j;
break;
}
}
//to count variable and function names
if(strcmp(s2,"int")==0||strcmp(s2,"float")==0||strcmp(s2,"char")==0||strcmp(s2,"double")==0)
{
if(isalpha(s[i+1])!=0||s[i+1]=='_'||(s[i+1]='*'&&isalpha(s[i+2])!=0))
{
for(int j=i+1;j<strlen(s);j++)
{
if(isalnum(s[j])!=0||s[j]=='_'||s[j]=='['||s[j]=='*')
{
continue;
}
else if(s[j]==',')
{
++T.ident;
continue;
}
else if(s[j]==']')
{
++T.ident;
if(s[j+1]==',')
++j;
else if(s[j+1]==';')
{++j; break; }
continue;
}
else if(ispunct(s[j])!=0)
{
++T.ident;
break;
}
}
}
}
if(strcmp(s2,"struct")==0||strcmp(s2,"union")==0)
{
if(isalpha(s[i+1])!=0||s[i+1]=='_')
{
for(int j=i+1;j<strlen(s);j++)
{
if(isalnum(s[j])!=0||s[j]=='_')
{
continue;
}
else
{
T.ident++;
break;
}
}
}
}
}
}
//function to show specific token count
void specificToken(int a)
{
if(a==1)
printf("\nNo. of keywords in code:%d\n",T.keyW);
else if(a==2)
printf("\nNo. of Identifiers in code:%d\n",T.ident);
else if(a==3)
{
printf("\nNo. of Operators in code:%d\n",T.oper);
printf("\tNo. of Arithmetic operators in code :%d\n",ops.arithmetic);
printf("\tNo. of Relational operators in code :%d\n",ops.relational);
printf("\tNo. of Assignment operators in code :%d\n",ops.assignment);
printf("\tNo. of Logical operators in code :%d\n",ops.logical);
printf("\tNo. of Bitwise operators in code :%d\n",ops.bitwise);
printf("\tNo. of Misc operators in code :%d\n",ops.misc);
}
else if(a==4)
printf("\nNo. of Special Symbols in code:%d\n",T.sym);
else if(a==5)
{
printf("\nNo. of Constants in code:%d :-\n",T.cons);
//printf("\tNo. of Integer Constants in code :%d\n",con.integers);
printf("\tNo. of Numerical constants in code:%d\n",con.decimals);
printf("\tNo. of String constants in code :%d\n",con.strings);
printf("\tNo. of Character constants in code:%d\n",con.chr);
}
else
printf("\nInvalid choice!!\n");
}
//function to write
void write()
{
int i=0;
printf("\nEnter Your Code(type and enter _ to end input):\n");
f= fopen("code.txt ","w");
if(f == NULL)
printf("file not exist!!");
while(1)
{
str[i]=getchar();
if(str[i]==95)
{
break;
}
i++;
}
for(int j=0;str[j]!=95;j++)
fprintf(f,"%c",str[j]);
if(f!=NULL)
printf("Code saved to code.txt successfuly!!");
fclose(f);
}
//modify
void appendCode()
{
showCode();
int i=0;
char s[10000]="\0";
f= fopen("code.txt ","a+");
fseek(f,0,SEEK_END);
again:
printf("\nWant to append in same line or next line[s/n]?");
ch=getch();
if(ch=='n')
fprintf(f,"%s","\n");
else if(ch!='s'&&ch!='n')
{
printf("\nInvalid choice!! try again");
goto again;
}
printf("\nEnter Your Code(type and enter _ to end input):\n");
if(f == NULL)
printf("file not exist!!");
while(1)
{
s[i]=getchar();
if(s[i]==95)
break;
++i;
}
for(int j=0;s[j]!=95;j++)
fprintf(f,"%c",s[j]);
if(f!=NULL)
printf("\nCode saved to code.txt successfuly!!");
fclose(f);
}
//show code
void showCode()
{
printf("\nThe Code that was saved last time:\n ");
f= fopen("code.txt ", "r");
fseek(f,0,SEEK_END);
const int fileSize = ftell(f);int i=0;
char s[fileSize];
fseek(f,0,SEEK_SET);
s[i]=getc(f);
while(s[i]!= EOF)
{
++i;
s[i]=getc(f);
}
printf("\n");
puts(s);
fclose(f);
}
void NLOC()
{
int count = 0;
f=fopen("code.txt","r");
char chr;
chr= getc(f);
while (chr != EOF)
{
if (chr == '\n')
{
count++;
}
chr = getc(f);
}
if (chr!='\n')
{
count++;
}
printf("Number of lines in code (NL0C) is : %d",count);
fclose(f);
}