-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex_analyser.c
124 lines (119 loc) · 1.95 KB
/
lex_analyser.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
#include<stdio.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
void keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0;
char keys[34][10] = {"auto","break","case","char","const","continue","default","do","double","else","enum","extern",
"float","for","goto","if","int","long","register","return","short","signed","sizeof","static","struct","switch",
"typedef","union","unsigned","void","volatile","while","printf","scanf"};
int main()
{
char seps[15]= "\t\n;,(){}[]#\"<>";
char oper[]="/%+-*=!&|^~<>.?";
int j;
j=0;
char ch,str[25];
char fname[50];
FILE *f1;
printf("\nfile name :");
scanf("%s",fname);
f1=fopen(fname,"r");
if(f1==NULL)
{
printf("file not found\n");
exit(0);
}
while((ch=fgetc(f1))!=EOF)
{
for(j=0;j<=14;j++)
{
if(ch==oper[j])
{
printf("%c is an operator\n",ch);
op++;
str[i]='\0';
keyw(str);
}
}
for(j=0;j<=14;j++)
{
if(i==-1)
break;
if(ch==seps[j])
{
if(ch=='#')
{
while(ch!='>')
{
printf("%c",ch);
ch = fgetc(f1);
}
printf("%c is a header file\n",ch);
i = -1;
break;
}
if(ch =='"')
{
do
{
ch = fgetc(f1);
printf("%c ",ch);
}while(ch!='"');
printf("%c is an argument\n",ch);
i = -1;
break;
}
str[i]='\0';
keyw(str);
}
}
if(ch==' ')
{
str[i++]='\0';
keyw(str);
}
if(i != -1)
{
str[i]=ch;
i++;
}
else i=0;
}
printf("keywords: %d\n identifiers: %d\n operators: %d\n Numbers: %d\n",kw,id,op,num);
}
void keyw(char *p)
{
int k,flag=0;
for(k=0;k<34;k++)
{
if(strcmp(keys[k],p)==0)
{
printf("%s is a keyword \n",p);
kw++;
flag=1;
break;
}
}
if(flag==0)
{
if(isdigit(p[0]))
{
printf("%s is a number \n",p);
num++;
}
else
{
if(p[0]!=13&&p[0]!=10)
{
if(p[0]!='\0')
{
printf("%s is an identifier \n",p);
id++;
}
}
}
}
i = -1;
}