0% found this document useful (0 votes)
41 views

Program For Lexical Analyser

This program performs lexical analysis on a C program source code file. It defines data structures to store keywords, functions, operators, and separators. It then reads the file token by token, classifying each token based on comparisons with the defined data structures. It prints the line number, token, and token category. Keywords, functions, operators, and separators are directly compared. Identifiers and constants are checked against character properties. It continues until reaching end of file.

Uploaded by

hemantsoman
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Program For Lexical Analyser

This program performs lexical analysis on a C program source code file. It defines data structures to store keywords, functions, operators, and separators. It then reads the file token by token, classifying each token based on comparisons with the defined data structures. It prints the line number, token, and token category. Keywords, functions, operators, and separators are directly compared. Identifiers and constants are checked against character properties. It continues until reaching end of file.

Uploaded by

hemantsoman
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

//Program for Lexical Analyser

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<dos.h>
#define KMAX 17
#define FMAX 14
#define SMAX 9
#define OMAX 19

/*
KMAX - keywords
FMAX - keywords
SMAX - keywords
OMAX - keywords
*/

struct keyword //17


{char name[20];
}k[50]={{"void"},{"char"},{"int"},{"float"}, {"struct"},{"if"},{"else"},
{"while"},{"do"},{"for"},{"switch"},{"break"},{"goto"},{"case"},
{"default"},{"class"},{"return"}};

struct function
{char name[20];
}f[50]={{"main"},{"getch"},{"strcpy"},{"strcat"}, {"strlen"},{"strcmp"},
{"clrscr"},{"printf"},{"scanf"},{"fgetc"},{"fputc"},{"putc"},{"strstr"},
{"getche"}};

struct Operator
{char name[20];
}o[50]={{"+"},{"-"},{"*"},{"^"},{"/"},{"="},{"%"},{"&"}, {"|"},{"&&"},
{"||"},{"!="},{"!"},{"<"}, {">"},{"<="},{">="},{"<<"},{">>"}};

struct Seperator
{char name[20];
}s[50]={{")"},{"("},{"{"},{"}"},{","},{";"},{":"}, {"["},{"]"}};
//----------------------------------------------------------------
int gcount=0,cline=0;
//----------------------------------------------------------------
int isoperator(char ch)
{
if(ch=='+' || ch=='-'|| ch=='*'|| ch=='^'|| ch=='/'|| ch=='='||
ch=='%'|| ch=='&'|| ch=='|'|| ch=='!'|| ch=='<'|| ch=='>')
return(1);
else
return(0);
}
//----------------------------------------------------------------
int isseperator(char ch) //"" do not
{
if(ch==',' || ch==';'|| ch=='}'|| ch=='{'|| ch=='['|| ch==']'||
ch=='('|| ch==')')
return(1);
else
return(0);
}
//-----------------------------------------------------
void IsLineIncrement(char ch)
{
if(ch=='\n')
cline++; //current line
}
//-----------------------------------------------
void lex()
{
char ch;
char ch1,ch2,pch; //comment validation
int len,flag=0,iflag=0,sflag=1,dflag=0,count=0,prflag=1;
int i = 0,j=0;
char token[10];
FILE *fp;
fp = fopen("ip6.c","r");

printf("\n-----------------------------------------------------------");
printf("\nLine No\t\tToken\t\tCategory");
printf("\n-----------------------------------------------------------");

do{
if(!isalnum(ch))
{
do{ // Alphabet & Numerical
ch = fgetc(fp);
token[i] = ch;
i++;
}while(ch!=EOF && !isspace(ch) && !isalnum(ch) && !isoperator(ch)
&& !isseperator(ch) &&ch!='\"');
token[i-1] = '\0';
goto start;
}

do{
ch = fgetc(fp);
token[i] = ch;
i++;
}while(ch!=EOF && !isspace(ch) && !isoperator(ch) && !
isseperator(ch) &&ch!='\"');
token[i-1] = '\0';
start :
//-------------------------------------
if(ch=='"') //String Literal
{
prflag=1;
do{
ch = fgetc(fp);
}while(ch!='"');
ch = fgetc(fp);
}
//--------------------------------Keyword
for(j=0;j<KMAX;j++)
{
if(!strcmp(token,k[j].name))
{
flag=1;
printf("\n%d\t\t%s\t\t%s",cline,token,"Keyword");
}
}
//--------------------------------Function
for(j=0;j<FMAX;j++)
{
if(!strcmp(token,f[j].name))
{
flag=1;
printf("\n%d\t\t%s\t\t%s",cline,token,"Pre. Function");
}
}
//--------------------------------Seperator
for(j=0;j<SMAX;j++)
{
if(!strcmp(token,s[j].name))
{
flag=1;
printf("\n%d\t\t%s\t\t%s",cline,token,"Seperator");
}
}
//--------------------------------Operator
for(j=0;j<OMAX;j++)
{
if(!strcmp(token,o[j].name))
{
switch(ch)
{
case '-' :
case '=' :
case '+' :
if(token[0]==ch)
{
switch(token[0])
{
case '+':
strcpy(token,"++");
break;
case '-':
strcpy(token,"--");
break;
case '=':
strcpy(token,"==");
break;
}
ch=fgetc(fp);
sflag=1;
}
break;
case '/' :
if(token[0]=='/')
{
ch2 = fgetc(fp);
while(ch2!='\n')
ch2 = fgetc(fp);
if(ch2=='\n')
cline++;
ch=' ';
}
break;
case '*' :
if(token[0]=='/')
{
ch2 = fgetc(fp);
IsLineIncrement(ch2);

while(1)
{
ch1 = ch2;
ch2 = fgetc(fp);
IsLineIncrement(ch2);

if(ch1=='*' && ch2=='/')


{
ch=' ';
break;
}
}
}
break;
default :
sflag=1;
break;
}

if(sflag==1)
{
flag=1;
printf("\n%d\t\t%s\t\t%s",cline,token,"Operator");
}
}
}
//--------------------------------Identifier
len = strlen(token);
if((isalpha(token[0]) || token[0]=='_') && flag!=1)
for(j=0;j<len;j++)
{
if(isalnum(token[j]) || token[j]=='_')
iflag=1;
else
{
iflag = 0;
break;
}
}
if(iflag==1)
printf("\n%d\t\t%s\t\t%s",cline,token,"Identifier");
//--------------------------------Constant
len = strlen(token);
for(j=0;j<len;j++)
{
if(isdigit(token[j]) || token[j]=='.')
dflag=1;
else
{
dflag = 0;
break;
}
}
if(dflag==1)
printf("\n%d\t\t%s\t\t%s",cline,token,"Constant");
//--------------------------Increment Var cline [ Current Line ]
IsLineIncrement(ch);
//-----------------------For Next Loop
if(isspace(ch))
{
token[0]='\0';
i=0;
}
else
{
token[0]= ch;
token[1]= '\0';
i=1;
}
flag=0;
dflag=0;
iflag=0;
sflag=0;
}while(ch!=EOF); //Outer od
fclose(fp);
}
//----------------------------------------------------------------
void main()
{
clrscr();
lex();
getch();
}

You might also like