You are on page 1of 13

2018

Lab Report
CSE332 : COMPILER DESIGN LAB

SUBMITTED TO

MAHBUBA MALIHA MOURIN

SUBMITTED BY

ASHIK-E-RABBANI

ID: 161-15-7093

SEC: J

DAFFODIL INTERNATIONAL UNIVERSITY | 4/2, Sobhanbag, Mirpur Road, Dhanmondi, Dhaka-1207 P a g e 1 | 13


Problem 1: Write a program to take input scanf("%[^\n]%*c", in_str);
printf("Your Entered %s", in_str);
string and display it. return 0;
}
Solution:
#include <stdio.h>
int main() Sample Input: ashik
{
char in_str[20]; Sample Output: ashik

Problem 2: Write a program to take input }


string from a file(.txt)and display it in to = fopen("copy.txt", "w");
another file. if (to == NULL)
{
Solution: perror("copy.txt doesn't exist.");
exit(1);
#include <stdio.h> }
#include <stdlib.h> do
{
int main() c = getc(from);
{ putc(c, to);
char c; }
FILE *from, *to; while(c != EOF);
fclose(to);
from = fopen("ash_read.txt", "r"); fclose(from);
if (from == NULL) exit(0);
{ }
perror("ash_read doesn't exist.");
exit(1);

Problem 3: Write a program to take input


string display the length of the string.
printf("Size of your string is :
Solution: %d",sizeof(arr)/4);
}
#include<stdio.h>
#define array_size 10

int main()
{ Sample Input: arr[5]

int arr[array_size]; Sample Output: 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: }

#include <stdio.h> s1[i] = '\0';


int main() printf("\n Concat is : %s", s1);
{
char s1[100], s2[100], i, j; return 0;
}
printf("String1 ");
scanf("%s", s1); Sample Input: ash ik
printf("String2: "); Sample Output: ashik
scanf("%s", s2);

Problem 5: Write a program to take two {


j++;
input strings (one main string and one sub }
string) and find the second one in first one else
{
and display the position where the string is j = 0;
in. }
}
Solution:
if (j == l)
#include <stdio.h> {
printf("%s found at position
int main() ",str2);
{
char str1[80], str2[80];
int l, i, j,position; for(position=0;position<strlen(str2);posi
tion++)
printf("Write something : "); {
gets(str1); printf(" %d ",i - j +
1+position);
printf("Look For "); }
gets(str2); }
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])

Problem 6: Write a program to take two #include <stdio.h>


#include <string.h>
input strings and copy the second one in
the first string. int main()
{
Solution: char first[100], second[100],
temp[100];

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); }

Problem 7: Write a program to take input gets(s);


string in lower case letter and convert it in
upper case letter and vice versa. while (s[char_in] != '\0') {
ch = s[char_in];
Solution: if (ch >= 'A' && ch <= 'Z')
s[char_in] = s[char_in] + 32;
#include <stdio.h> else if (ch >= 'a' && ch <= 'z')
#define max 700 s[char_in] = s[char_in] - 32;
#include<process.h> char_in++;
}
int main ()
{ printf("%s\n", s);
int char_in = 0;
char ch, s[max]; return 0;
}
printf("Enter a line : \n");

Problem 8: Reverse the user inputted int i;


printf("Write : ");
string and size calculation scanf("%s", stringi);

Solution: int nfs = sizeof(stringi)/4;


#include <stdio.h> for (i = nfs ; i >= 0; i--)
#include<string.h> {
#define arr_sz 231 printf("%c", stringi[i]);
}
int main () return 0;
{ }
char stringi[arr_sz];

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++;

Problem 10: Find the number of


putc(ch, fptr);
characters’ form FILE.
}
Solution:
// close
#include <stdio.h> fclose(fptr);
int main(void) { // read mode
fopen("ash.txt", "r");
FILE *fptr;
// displayin console
int line = 1,count = 0; printf("\nash:\n");
char ch; while( (ch = getc(fptr)) != EOF ) {
count++;
printf("%c", ch);
fptr = fopen("ash.txt", "w"); }
printf("Enter line: ");
printf("\n\n %d Charecters inputted\n\n
\n\n--------END-------\n",count);
while( (ch = getchar()) != '.' ) {
// close file
if (ch == '\n') fclose(fptr);
line++;

P a g e 5 | 13
return 0; }

Problem 11: Find the number of Lines form fclose(fptr);


// read mode
FILE. fopen("ash.txt", "r");

Solution: // displayin console


printf("\nenter no of line to stop
#include <stdio.h> :\n");
scanf("%d",&stop);
int main(void) { printf("\nTEXT are :\n");
while( (ch = getc(fptr)) != EOF &&
FILE *fptr; line2<=stop ){
int line = 1,count = 0, stop,line2 = 1; if(ch == '\n')
char ch; {
line2++;
fptr = fopen("ash.txt", "w"); }
printf("Enter line: "); count++;
printf("%c", ch);
}
while( (ch = getchar()) != '.' ) {

if (ch == '\n') printf("\n\n of Lines are :%d\n\n------


line++; --END-------\n",count,line);
putc(ch, fptr); // close file
fclose(fptr);
} return 0;
// close

Problem 12: Write a program to make a char ch, string_input[15],


operators[] = "+-*/%";
simple lexical analyzer FILE *fp;
char tr[20];
Solution: int i,j=0;
#include<stdio.h> fp = fopen("input.txt","r");
#include<stdlib.h>
#include<string.h> if(fp == NULL){
printf("error while opening the
int main(){ file\n");
keyword_check(); exit(0);
identifier_check(); }
math_operator_check(); printf("\nMath Operators : ");
logical_operator_check(); while((ch = fgetc(fp)) != EOF){
numerical_check(); for(i = 0; i < 6; ++i){
others_check(); if(ch == operators[i])
printf("%c ", ch);
return 0; }
} }
printf("\n");
void math_operator_check()
{
fclose(fp);
P a g e 6 | 13
} }
printf("\n");

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(fp == NULL){ fp = fopen("input.txt","r");


printf("error while opening the
file\n"); if(fp == NULL){
exit(0); printf("error while opening the
} file\n");
printf("\nNumerical Values : "); exit(0);
while((ch = fgetc(fp)) != EOF){ }
for(i = 0; i < 6; ++i){
if(ch == operators[i]) printf("\nIdentifiers : ");
printf("%c ", ch); while((ch = fgetc(fp)) != EOF){

} 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; }

for(i = 0; i < 32; ++i){ }


if(strcmp(keywords[i],
string_input) == 0){ printf("\n");
flag = 1;
break;
} fclose(fp);
} }

Problem 13: Evaluate mathematical #include <stdio.h>


Expression from a given user defined char expr[20];
variables and their values. char stack[20];
char var[20],val[20];
Solution: char str2[80],str3[80];

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);

int alphabet = 0, number = 0, i;


for (i=0; str[i]!= '\0'; i++)
{
// check for alphabets void postfixConvert()
if (isalpha(str[i]) != 0) {
{ char topsymb,operatorStack[20];
var[varPos]=str[i]; ctr=0;
varPos++; while(expr[ctr]!='\0')
} {//read till the end of input
if(expr[ctr]>='0'&&expr[ctr]<='9')
stack[++top]=expr[ctr];//add numbers
else if (isdigit(str[i]) != 0) straightaway
{ else
val[valPos]=str[i]; {
valPos++;
} while(topOper>=0&&precedence(operatorStac
k[topOper],expr[ctr]))

} {//check for the operators of higher


} precedence and then add them to stack

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();
}

Problem 14: Finding the FIRST and Follow of


a Grammar written in txt FILE. #include<stdio.h>
#include<string.h>
#define size 10
Solution:
int i,j,l,m,n=0,o,p,nv,z=0,x=0;
P a g e 10 | 13
char {
str[size],temp,temp2[size],temp3[20],*ptr if(
; pro[i].right_of_nonTerm[j][0]<65 ||
pro[i].right_of_nonTerm[j][0]>90 )
struct prod {
{
char pro[i].first[strlen(pro[i].first)]=pro[i]
left_of_non_term[size],right_of_nonTerm[s .right_of_nonTerm[j][0];
ize][size],first[size],fol[size]; }
int n; else if(
}pro[size]; pro[i].right_of_nonTerm[j][0]>=65 &&
pro[i].right_of_nonTerm[j][0]<=90 )
{

int main() temp=pro[i].right_of_nonTerm[j][0];


{ if(temp=='S')
FILE *f;
pro[i].first[strlen(pro[i].first)]='#';
for(i=0;i<size;i++) findter();
pro[i].n=0; }
}
f=fopen("lab6.txt","r"); }
while(!feof(f)) printf("-------------");
{
printf("\n\nFIRST\n");
fscanf(f,"%s",pro[n].left_of_non_term); for(i=0;i<n;i++)
if(n>0) {
{ printf("\n%s -> {
if( ",pro[i].left_of_non_term);
strcmp(pro[n].left_of_non_term,pro[n-
1].left_of_non_term) == 0 ) for(j=0;j<strlen(pro[i].first);j++)
{ {
for(l=j-1;l>=0;l--)
pro[n].left_of_non_term[0]='\0';
fscanf(f,"%s",pro[n- if(pro[i].first[l]==pro[i].first[j])
1].right_of_nonTerm[pro[n-1].n]); break;
pro[n-1].n++; if(l==-1)
continue; printf("%c
} ",pro[i].first[j]);
} }
printf("}");
fscanf(f,"%s",pro[n].right_of_nonTerm[pro }
[n].n]);
pro[n].n++; for(i=0;i<n;i++)
n++;
} temp2[i]=pro[i].left_of_non_term[0];
pro[0].fol[0]='$';
printf("\nGiven Grammar"); for(i=0;i<n;i++)
printf("\n-------------\n"); {
for(i=0;i<n;i++) for(l=0;l<n;l++)
for(j=0;j<pro[i].n;j++) {
printf("%s = for(j=0;j<pro[i].n;j++)
%s\n",pro[i].left_of_non_term,pro[i].righ {
t_of_nonTerm[j]);
ptr=strchr(pro[l].right_of_nonTerm[j],tem
pro[0].first[0]='#'; p2[i]);
for(i=0;i<n;i++) if( ptr )
{ {
for(j=0;j<pro[i].n;j++)

P a g e 11 | 13
p=ptr- for(k=0;k<n;k++)
pro[l].right_of_nonTerm[j]; {

if(pro[l].right_of_nonTerm[j][p+1]>=65 && if(temp==pro[k].left_of_non_term[0])


pro[l].right_of_nonTerm[j][p+1]<=90) {
{ for(t=0;t<pro[k].n;t++)
for(o=0;o<n;o++) {
if(
if(pro[o].left_of_non_term[0]==pro[l].rig pro[k].right_of_nonTerm[t][0]<65 ||
ht_of_nonTerm[j][p+1]) pro[k].right_of_nonTerm[t][0]>90 )

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])
} {

void findter() strcat(pro[i].fol,pro[o1].first);


{ chk++;
int k,t; }

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

You might also like