0% found this document useful (0 votes)
42 views8 pages

WAP To Parse C Source and Identify Identifiers, Functions and Keywords

This C++ program parses a C source code file to identify keywords, identifiers, and functions. It reads in the source code file, tokenizes it, and compares the tokens to lists of keywords, data types, and function identifiers to categorize each token. It then outputs the unique keywords, identifiers, and functions it found in the file.

Uploaded by

s1a2l3o4n5i6
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views8 pages

WAP To Parse C Source and Identify Identifiers, Functions and Keywords

This C++ program parses a C source code file to identify keywords, identifiers, and functions. It reads in the source code file, tokenizes it, and compares the tokens to lists of keywords, data types, and function identifiers to categorize each token. It then outputs the unique keywords, identifiers, and functions it found in the file.

Uploaded by

s1a2l3o4n5i6
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

WAP to parse C source and identify

identifiers,functions and keywords


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<fstream.h>
void main()
{
clrscr();
char
kword[42][10]={"auto","break","case","char","class","const","continue","default","delete","do","do
uble","else","enum","extern","float","for","friend","if","inline","int","long","new","operator","privat
e","public","protected","register","return","short","signed","sizeof","static","struct","switch","this",
"typedef","union","unsigned","void","volatile","while"};
char ch,s[1000],x[500][20],key[100][10],in[50][20],fun[20][15];
char dt[5][7]={"int","float","char","long","double"};
int i=0,j=0,k,f,m,c,dtc,n=0,p=0,l=0,kf=0,inf=0,ff=0,dtf=0,rf=0;
ifstream fil;
fil.open("p1.cpp");
while((fil.eof())!=1)
{
s[i]=fil.get();
i++;
}
s[i]='\0';
fil.close();
for(i=0;s[i]!='\0';i++)

DAVIET,SHEENAM MITTAL,144/10

{
if(s[i]==' ' || s[i]=='\t' || s[i]=='\r')
continue;
f=0;
for(k=0;(s[i]>=65 && s[i]<=90) || (s[i]>=97 && s[i]<=122) || (s[i]>=48 &&
s[i]<=57);k++)
{
x[j][k]=s[i];
i++;
f=1;
}
if(!(s[i]==' ' || s[i]=='\t' || s[i]=='\r' || s[i]=='\0'))
{

if(f==1)
{
x[j][k]='\0';
j++;
}
x[j][0]=s[i];
x[j][1]='\0';

}
else
{
x[j][k]='\0';
}
j++;
if(s[i]=='\0')
break;

DAVIET,SHEENAM MITTAL,144/10

cout<<"\n\n";
for(i=0;i<j;i++)
{

for(m=0;m<42;m++)
{
c=strcmp(x[i],kword[m]);
if(c==0)
{
strcpy(key[n],x[i]);
n++;
rf=1;
break;
}
else
rf=0;
}
for(m=0;m<5;m++)
{
c=strcmp(x[i],dt[m]);
if(c==0)
{
do
{
i++;
dtf=0;
for(dtc=0;dtc<5;dtc++)

DAVIET,SHEENAM MITTAL,144/10

{
if(!(strcmp(x[i],dt[dtc])))
dtf=1;
}
if(dtf==1)
continue;
if(x[i][0]==';')
break;
else if(x[i][0]==',')
continue;
else if(x[i][0]=='=')
{
i++;
continue;
}
else if(x[i][0]=='[')
{
i=i+2;
continue;
}
else if(x[i+1][0]=='(')
break;
else if(x[i][0]==')')
break;
else
{
strcpy(in[p],x[i]);

DAVIET,SHEENAM MITTAL,144/10

p++;
}
}while(1);
}
}
if((x[i+1][0]=='(') && (rf==0))
{
strcpy(fun[l],x[i]);
l++;
}
}
cout<<"Keywords : ";
for(i=0;i<n;i++)
{
kf=0;
for(j=i+1;j<n;j++)
{
if(!(strcmp(key[i],key[j])))
{
kf=1;
break;
}
}
if(kf==0)
cout<<key[i]<<"\t";
}
cout<<"\n\nIdentifiers : ";

DAVIET,SHEENAM MITTAL,144/10

for(i=0;i<p;i++)
{
inf=0;
for(j=i+1;j<p;j++)
{
if(!(strcmp(in[i],in[j])))
{
inf=1;
break;
}
}
if(inf==0)
cout<<in[i]<<"\t";
}
cout<<"\n\nFunctions : ";
for(i=0;i<l;i++)
{
ff=0;
for(j=i+1;j<l;j++)
{
if(!(strcmp(fun[i],fun[j])))
{
ff=1;
break;
}
}
if(ff==0)

DAVIET,SHEENAM MITTAL,144/10

cout<<fun[i]<<"\t";
}
getch();
}

DAVIET,SHEENAM MITTAL,144/10

OUTPUT:

DAVIET,SHEENAM MITTAL,144/10

You might also like