0% found this document useful (0 votes)
102 views13 pages

Praticals of Compiler Design

I am Kunal Bangar pursuing Bachelor of Engineering in Computer Science and Engineering in Swami Vivekanand College of Engineering.

Uploaded by

Kunal Bangar
Copyright
© © All Rights Reserved
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)
102 views13 pages

Praticals of Compiler Design

I am Kunal Bangar pursuing Bachelor of Engineering in Computer Science and Engineering in Swami Vivekanand College of Engineering.

Uploaded by

Kunal Bangar
Copyright
© © All Rights Reserved
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/ 13

Experiment-1

Q1.Write a C program to implement Lexical Analyser for “if”


statement.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>

int keyword_library(char temp[]);

int main()
{
char ch, temp[40], arithmetic_operator[] = "=+%*/-";
FILE *file_pointer;
int count, x = 0;
file_pointer =
fopen("C:\\Users\\tusoni\\Desktop\\demo.txt","r");
if(file_pointer == NULL)
{
printf("The file could not be opened.\n");
exit(0);
}
while((ch = fgetc(file_pointer)) != EOF)
{
count = 0;
while(count <= 5)
{
if(ch == arithmetic_operator[count])
{
printf("\nOperator:\t%c", ch);
}
count = count + 1;
}
if(isalnum(ch))
{
temp[x++] = ch;
}
else if((ch == '\n' || ch == ' ') && (x != 0))
{
temp[x] = '\0';
x = 0;
if(keyword library(temp) == 1)
{
printf("\n Keyword:\t% s", temp);
}
else
{
printf("\n Identifier:\t% s", temp);
}
}
}
fclose (file pointer);
getch ();
return 0;
}
int keyword library(char temp[])
{
int count = 0, flag = 0;
char keywords[32][12] = {"return", "continue", "extern", "static",
"long", "signed", "switch", "char", "else", "unsigned", "if", "struct",
"union", "goto", "while", "float", "enum", "sizeof", "double",
"volatile", "const", "case", "for", "break", "void", "register", "int",
"do", "default", "short", "typedef", "auto"};
while (count <= 31)
{
if (strcmp(keywords[count], temp) == 0)
{
flag = 1;
break;
}
count = count + 1;
}
return (flag);
}

Output:
Experiment-2
Q2.Write a program to implement of Lexical Analyzer for
Arithmetic Expression.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void remove duplicate();
void final();
int Isiden (char ch);
int Isop (char ch);
int Isdel (char ch);
int Iskey (char * str);
void remove duplicate();
char op [8]={'+','-','*','/','=','<','>','%'};
char del [8]={'}','{',';','(',')','[',']',','};
char *key[]={"int","void","main","char","float"};
//char *operato []={"+","-","/","*","<",">","=","%","<=",">=","++"};
int
idi=0,idj=0,k,opi=0,opj=0,deli=0,uqdi=0,uqidi=0,uqoperi=0,kdi=0,liti
=0,ci=0; int uqdeli [20],uqopi [20],uqideni [20],l=0,j;
char uqdel [20],uqiden [20][20],uqop [20][20],keyword [20][20];
char
iden[20][20],oper[20][20],delem[20],litral[20][20],lit[20],constant[20
][20];
void lex analysis(char *str)
{
int i=0;
while (str[i]!='\0')
{
if (Isiden (str [i]))
{
while (Isiden (str [i]))
{
iden [idi] [idj++]=str [i++];
}
iden [idi][idj]='\0';
idi++;idj=0;
}
else
if (str [i]=='"')
{
lit [l++]=str [i];
for (j=i+1;str[j]!='"'; j++)
{
lit [l++]=str [j];
}
lit[l++]=str [j];lit[l]='\0';
strcpy (litral [liti++],lit);
i=j+1;
}
else
if (Isop(str[i]))
{
while (Isop (str [i]))
{
oper [opi][opj++]=str [i++];
}
oper [opi] [opj]='\0';
opi++;opj=0;
}
else
if (Isdel (str[i]))
{
while (Isdel (str [i]))
{
delem [deli++]=str[i++];
}
}
else
{
i++;
}
}
remove duplicate();
final ();
}
int Isiden (char ch)
{
if (isalpha(ch)||ch=='_'||isdigit(ch)||ch=='.')
return 1;
else
return 0;
}

int Isop (char ch)


{
int f=0,i;
for (i=0;i<8&&!f;i++)
{
if (ch==op[i])
f=1;
}
return f;
}
int Isdel (char ch)
{
int f=0,i;
for (i=0;i<8&&!f; i++)
{
if (ch==del [i])
f=1;
}
return f;
}

int Iskey (char * str)


{
int i,f=0;
for(i=0;i<5;i++)
{
if(!strcmp (key[i],str))
f=1;
}
return f;
}
void remove duplicate()
{
int i,j;
for (i=0;i<20;i++)
{
Uqdeli [i]=0;
uqopi [i]=0;
uqideni [i]=0;
}
for (i=1;i<deli+1;i++)
{
if (uqdeli [i-1]==0)
{
uqdel [uqdi++]=delem [i-1];
for (j=i;j<deli; j++)
{
if(delem [i-1]==delem [j])
uqdeli [j]=1;
}
}
}
for (i=1;i<idi+1;i++)
{
if (uqideni[i-1]==0)
{
strcpy (uqiden [uqidi++],iden[i-1]);
for (j=i;j<idi;j++)
{
if (!strcmp (iden [i-1], iden [j]))
uqideni [j]=1;
}
}
}
for (i=1;i<opi+1;i++)
{
if (uqopi [i-1]==0)
{
strcpy (uqop [uqoperi++],oper [i-1]);
for (j=i;j<opi;j++)
{
if (!strcmp (oper [i-1], oper [j]))
uqopi [j]=1;
}
}
}
}
void final()
{
int i=0;
idi=0;
for (i=0;i<uqidi; i++)
{
if (Iskey (uqiden [i]))
strcpy (keyword [kdi++], uqiden [i]);
else
if (isdigit (uqiden [i][0]))
strcpy (constant[ci++],uqiden [i]);
else
strcpy (iden [idi++], uqiden [i]);
}
printf ("\n\t Delemeter are : \n");
for (i=0;i<uqdi; i++)
printf ("\t %c\n",uqdel [i]);

printf ("\n\t Operators are : \n");


for (i=0;i<uqoperi; i++)
{
printf ("\t");
puts (uqop [i]);
}
printf ("\n\t Identifiers are : \n");
for (i=0;i<idi; i++)
{
printf ("\t");
puts (iden[i]);
}
printf ("\n\t Keywords are : \n");
for (i=0;i<kdi; i++)
{
printf ("\t");
puts (keyword [i]);
}
printf ("\n\t Constants are :\n");
for (i=0;i<ci; i++)
{
printf ("\t");
puts (constant [i]);
}

printf ("\n\t Literals are :\n");


for (i=0;i<liti; i++)
{
printf ("\t");
puts (litral [i]);
}
}
void main()
{
char str [50];
//clrscr ();
printf ("\n Enter the string : ");
scanf ("%[^\n]c",str);
lex analysis (str);
//getch ();
}
Output:

You might also like