Professional Documents
Culture Documents
Lab Report
CSE332 : COMPILER DESIGN LAB
SUBMITTED TO
SUBMITTED BY
ASHIK-E-RABBANI
ID: 161-15-7093
SEC: J
int main()
{ Sample Input: arr[5]
P a g e 2 | 13
Problem 4: Write a program to take input for(i = 0; s1[i] != '\0'; ++i);
two strings and join(concate) them and for(j = 0; s2[j] != '\0'; ++j, ++i)
display the final string. {
s1[i] = s2[j];
Solution: }
P a g e 3 | 13
printf("Enter first string\n"); strcpy(temp, first);
gets(first); strcpy(first, second);
strcpy(second, temp);
printf("Enter second string\n");
gets(second); printf("After Swapping\n");
printf("First string: %s\n", first);
printf("\nBefore Swapping\n"); printf("Second string: %s\n", second);
printf("First string: %s\n", first);
printf("Second string: %s\n\n", return 0;
second); }
P a g e 4 | 13
Problem 9: Write a program to take input }
else
string and replace any specific character {
with a special character. j = 0;
}
Solution: }
#include <stdio.h> if (j == l)
{
int main() //printf("%s found at position ",str2);
{
char str1[80], str2[80],str3[80]; for(position=0;position<strlen(str3);posi
int l, i, j,position,m; tion++)
{
printf("Write something : "); // printf(” %d ",i – j + 1+position);
gets(str1); str1[i – j+position] = str3[position];
}
printf("Replace For ");
gets(str2); //for(m=0;m<strlen(str2);m++)
printf("Special Charecter : "); printf("\n\n After change %s \n”,str1 );
gets(str3); }
else
for (l = 0; str2[l] != "\0"; l++); {
printf("nai to”);
for (i = 0, j = 0; str1[i] != "\0" && }
str2[j] != "\0"; i++)
{ return 0;
if (str1[i] == str2[j]) }
{
j++;
P a g e 5 | 13
return 0; }
void logical_operator_check()
{
fclose(fp);
char ch, string_input[15], }
operators[] = "&&||<>";
FILE *fp; void others_check()
char tr[20]; {
int i,j=0; char ch, string_input[15], symbols[]
= "(){}[]";
fp = fopen("input.txt","r"); FILE *fp;
char tr[20];
if(fp == NULL){ int i,j=0;
printf("error while opening the
file\n"); fp = fopen("input.txt","r");
exit(0);
} if(fp == NULL){
printf("\nLogical Operators : "); printf("error while opening the
while((ch = fgetc(fp)) != EOF){ file\n");
for(i = 0; i < 6; ++i){ exit(0);
if(ch == operators[i]) }
printf("%c ", ch); printf("\nOthers : ");
while((ch = fgetc(fp)) != EOF){
} for(i = 0; i < 6; ++i){
} if(ch == symbols[i])
printf("\n"); printf("%c ", ch);
}
}
fclose(fp); printf("\n");
}
void numerical_check()
{ fclose(fp);
}
char ch, string_input[15],
operators[] void identifier_check()
={'0','1','2','3','4','5','6','7','8','9' {
}; char ch, string_input[15];
FILE *fp; FILE *fp;
char operators[]
int i,j=0; ={'0','1','2','3','4','5','6','7','8','9'
};
fp = fopen("input.txt","r"); int i,j=0;
} if(isalnum(ch)){
P a g e 7 | 13
string_input[j++] = ch;
} return flag;
else if((ch == ' ' || ch == }
'\n') && (j != 0)){
string_input[j] = void keyword_check()
'\0'; {
j = 0;
char ch, string_input[15],
operators[] = "+-*/%=";
if(isKeyword(string_input) == 1) FILE *fp;
{ char tr[20];
int i,j=0;
}
printf(" Token Identification using C
else \n By Ashik-E-Rabbani \n 161-15-
printf("%s ", 7093\n\n");
string_input);
} fp = fopen("input.txt","r");
} if(fp == NULL){
printf("error while opening the
printf("\n"); file\n");
exit(0);
}
fclose(fp);
} printf("\nKeywords : ");
while((ch = fgetc(fp)) != EOF){
int isKeyword(char string_input[]){
char keywords[32][10] = if(isalnum(ch)){
{"auto","break","case","char","const","co string_input[j++] = ch;
ntinue","default", }
else if((ch == ' ' || ch ==
"do","double","else","enum","extern","flo '\n') && (j != 0)){
at","for","goto", string_input[j] =
'\0';
"if","int","long","register","return","sh j = 0;
ort","signed",
"sizeof","static","struct","switch","type if(isKeyword(string_input) == 1)
def","union", printf("%s ",
string_input);
"unsigned","void","volatile","while"};
int i, flag = 0; }
P a g e 8 | 13
int i;
int l,error_state=0, i,m, int ctr;
num,j,position,m,lengthVariable=5,varPos= int top=-1;//top of
0,valPos=0;; postfix stack
int topOper=-1;//top of operator stack
int main() int operate(int a,int b,char oper)
{ {
int res=0;
scanf("%d",&num); switch(oper)
lengthVariable=num+1; {
//setVariableLength(); case '+':res=a+b;break;
char str[20]; case '-':res=a-b;break;
case '*':res=a*b;break;
int i, varPos=0,valPos=0; case '/':res=a/b;break;
}
for(i=0;i<lengthVariable;i++) return res;
{ }
gets(str);
for(i=0;i<1;i++) topsymb=operatorStack[topOper--];
{ stack[++top]=topsymb;
doWork(); }
} operatorStack[++topOper]=expr[ctr];
}
return 0; ctr++;
} }
while(topOper>=0)//add remaining
operators to stack
int precedence(char a,char b)
{//returns true if precedence of operator
a is more or equal to than that of b stack[++top]=operatorStack[topOper--];
if(((a=='+')||(a=='-
'))&&((b=='*')||(b=='/'))) //printf("The Resulting Postfix
return 0; expression for the given infix
else expression\n%s\n",stack);
return 1; }
} int doWork()
P a g e 9 | 13
{ //for(m=0;m<strlen(str2);m++)
printf("give expression : "); //printf("\n\n Math Expression is : %s
gets(expr); \n",expr );
for(m=0;m<lengthVariable;m++)
{ }
else
str2[0]=var[m]; {
str3[0]=val[m]; error_state++;
replacor();
} printf("\nCompilation Error ! Compilation
Error !! Compilation Error !!!\n");
if(error_state == 0) }
{
printf("\n\n %s = ",expr ); }
maina();
} int maina()
else {
return 0; postfixConvert();//function to
} convert in postfix form
char oper;
void replacor() int operand1,operand2;
{ ctr=0;
for (l = 0; str2[l] != '\0'; l++); int result[2];//stack to keep storing
results
for (i = 0, j = 0; expr[i] != '\0' && int rTop=-1;//top of result stack
str2[j] != '\0'; i++)
{ while(stack[ctr]!='\0')
if (expr[i] == str2[j]) {
{ oper=stack[ctr];
j++; if(oper>='0'&&oper<='9')
} result[++rTop]=(int)(oper-
else '0');//add numbers
{ else
j = 0; {//if an operator is encountered than pop
} twice and push the result of operation to
} the stack
if (j == l) operand1=result[rTop--];
{ operand2=result[rTop--];
//printf("%s found at position ",str2);
result[++rTop]=operate(operand2,operand1,
for(position=0;position<strlen(str3);posi oper);
tion++) }
{ ctr++;
// printf(" %d ",i - j + 1+position); }
expr[i - j +position] = str3[position]; printf("%d\n",result[0]);
} getch();
}
P a g e 11 | 13
p=ptr- for(k=0;k<n;k++)
pro[l].right_of_nonTerm[j]; {
strcat(pro[i].fol,pro[o].first); pro[i].first[strlen(pro[i].first)]=pro[k]
} .right_of_nonTerm[t][0];
else else if(
if(pro[l].right_of_nonTerm[j][p+1]=='\0') pro[k].right_of_nonTerm[t][0]>=65 &&
{ pro[k].right_of_nonTerm[t][0]<=90 )
{
temp=pro[l].left_of_non_term[0];
temp=pro[k].right_of_nonTerm[t][0];
if(pro[l].right_of_nonTerm[j][p]==temp) if(temp=='S')
continue;
if(temp=='S') pro[i].first[strlen(pro[i].first)]='#';
findter();
strcat(pro[i].fol,"$"); }
findfol(); }
} break;
else }
}
pro[i].fol[strlen(pro[i].fol)]=pro[l].rig }
ht_of_nonTerm[j][p+1];
} void findfol()
} {
} int k,t,p1,o1,chk;
} char *ptr1;
for(k=0;k<n;k++)
printf("\n\n\n"); {
for(i=0;i<n;i++) chk=0;
{ for(t=0;t<pro[k].n;t++)
printf("\nFOLLOW (%s) -> { {
",pro[i].left_of_non_term);
for(j=0;j<strlen(pro[i].fol);j++) ptr1=strchr(pro[k].right_of_nonTerm[t],te
{ mp);
for(l=j-1;l>=0;l--) if( ptr1 )
{
if(pro[i].fol[l]==pro[i].fol[j]) p1=ptr1-
break; pro[k].right_of_nonTerm[t];
if(l==-1)
if(pro[k].right_of_nonTerm[t][p1+1]>=65
printf("%c",pro[i].fol[j]); && pro[k].right_of_nonTerm[t][p1+1]<=90)
} {
printf(" }"); for(o1=0;o1<n;o1++)
}
printf("\n"); if(pro[o1].left_of_non_term[0]==pro[k].ri
//getch(); ght_of_nonTerm[t][p1+1])
} {
P a g e 12 | 13
} }
else }
if(pro[k].right_of_nonTerm[t][p1+1]=='\0' if(chk>0)
) break;
{ }
}
temp=pro[k].left_of_non_term[0];
if(pro[l].right_of_nonTerm[j][p]==temp)
continue;
if(temp=='S')
strcat(pro[i].fol,"$");
findfol();
chk++;
}
else
{
pro[i].fol[strlen(pro[i].fol)]=pro[k].rig
ht_of_nonTerm[t][p1+1];
chk++;
}
/* The Work might end here but not The Respect to you Teacher */
P a g e 13 | 13