17
17
P (2150708) 140123116017
EXPERIMENT :- 1
A.I.M :- (A)WAP to find number of vowels in a file.
#include<process.h>
#include <stdio.h>
void main(int argc,char *argv[])
{
FILE *fp1;
int vowel=0,consonant=0;
char ch;
clrscr();
if(argc!=2)
{
printf("Insufficient Arguments");
exit(0);
}
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{
printf("Source can't be opened");
exit(0);
}
ch=fgetc(fp1);
while(ch!=EOF)
{
if((ch=='a')||(ch=='A')||(ch=='e')||(ch=='E')||(ch=='i')||(ch=='I')||(ch=='o') ||(ch=='
O')||(ch=='u')||(ch=='U'))
{
vowel++;
}
else
{
consonant++;
}
ch=fgetc(fp1);
}
printf("\n Number of vowels are = %d",vowel);
getch();
}
S.P (2150708) 140123116017
Output:
S.P (2150708) 140123116017
#include <stdio.h>
int main()
{
char s[1000],i;
printf(Enter a string:\n);
scanf(%s,s);
for(i=0;s[i]!=\0;++i);
printf(Length of string:%d,i);
return 0;
}
Output :-
S.P (2150708) 140123116017
EXPERIMENT :- 2
#include <stdio.h>
int main(void) {
FILE *fp1, *fp2;
//'filename'is a 40 character string to store filename
char filename[40];
char c;
int del_line, temp = 1;
//asks user for file name
printf("Enter file name: ");
//receives file name from user and stores in 'filename'
scanf("%s", filename);
fp1 = fopen(filename, "r");
//open file in read mode
c = getc(fp1);
//print the contents of file .
while (c != EOF) {
printf("%c", c);
c = getc(fp1);
}
//ask user for line number to be deleted.
printf(" Enter line number to be deleted and replaced");
scanf("%d", &del_line);
//take fp1 to start point.
rewind(fp1);
//open copy.c in write mode
fp2 = fopen("copy.c", "w");
c = getc(fp1);
while (c != EOF) {
if (c == '') {
temp++;
}
//till the line to be deleted comes,copy the content from one file to other
if (temp != del_line){
putc(c, fp2);
}
S.P (2150708) 140123116017
Output:
S.P (2150708) 140123116017
EXPERIMENT :- 3
A.I.M :- WAP to develop lexical analysis of an expression evaluator.
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int i=0,id=0,kw=0,num=0,op=0;
char keys[32][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
main()
{
char ch,str[25],seps[15]=" \t\n,;(){}[]#\"<>",oper[]="!%^&*-+=~|.<>/?";
int j;
char fname[50];
FILE *f1;
printf("enter file path (drive:\\fold\\filename)");
scanf("%s",fname);
f1 = fopen(fname,"r");
if(f1==NULL)
{
printf("file not found");
exit(0);
}
while((ch=fgetc(f1))!=EOF)
{
for(j=0;j<=14;j++)
{
if(ch==oper[j])
{
printf("%c is an operator\n",ch);
op++;
str[i]='\0';
S.P (2150708) 140123116017
keyw(str);
}
}
for(j=0;j<=14;j++)
{
if(i==-1)
break;
if(ch==seps[j])
{
if(ch=='#')
{
while(ch!='>')
{
printf("%c",ch);
ch=fgetc(f1);
}
printf("%c is a header file\n",ch);
i=-1;
break;
}
if(ch=='"')
{
do
{
ch=fgetc(f1);
printf("%c",ch);
}while(ch!='"');
printf("\b is an argument\n");
i=-1;
break;
}
str[i]='\0';
keyw(str);
}
}
if(i!=-1)
{
str[i]=ch;
i++;
S.P (2150708) 140123116017
}
else
i=0;
}
printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers: %d\n",kw,id,op,num);
}
void keyw(char *p)
{
int k,flag=0;
for(k=0;k<=31;k++)
{
if(strcmp(keys[k],p)==0)
{
printf("%s is a keyword\n",p);
kw++;
flag=1;
break;
}
}
if(flag==0)
{
if(isdigit(p[0]))
{
printf("%s is a number\n",p);
num++;
}
else
{
if(p[0]!='\0')
{
printf("%s is an identifier\n",p);
id++;
}
}
}
i=-1;
}
S.P (2150708) 140123116017
Output :-
S.P (2150708) 140123116017
EXPERIMENT :- 4
A.I.M :- WAP to develop TOP-DOWN PARSER using C/CPP.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class parse
{
int nt,t,m[20][20],i,s,n,p1,q,k,j;
char p[30][30],n1[20],t1[20],ch,b,c,f[30][30],fl[30][30];
public:
int scant(char);
int scannt(char);
void process();
void input();
};
int parse::scannt(char a)
{
int c=-1,i;
for(i=0;i<nt;i++)
{
if(n1[i]==a)
{
return i;
}
}
return c;
}
int parse::scant(char b)
{
int c1=-1,j;
for(j=0;j<t;j++)
{
if(t1[j]==b)
{
return j;
}
S.P (2150708) 140123116017
}
return c1;
}
void parse::input()
{
cout<<"Enter the number of productions:";
cin>>n;
cout<<"Enter the productions one by one"<<endl;
for(i=0;i<n;i++)
cin>>p[i];
nt=0;
t=0;
}
void parse::process()
{
for(i=0;i<n;i++)
{
if(scannt(p[i][0])==-1)
n1[nt++]=p[i][0];
}
for(i=0;i<n;i++)
{
for(j=3;j<strlen(p[i]);j++)
{
if(p[i][j]!='e')
{
if(scannt(p[i][j])==-1)
{
if((scant(p[i][j]))==-1)
t1[t++]=p[i][j];
}
}
}
}
t1[t++]='$';
for(i=0;i<nt;i++)
{
for(j=0;j<t;j++)
S.P (2150708) 140123116017
m[i][j]=-1;
}
for(i=0;i<nt;i++)
{
cout<<"Enter first["<<n1[i]<<"]:";
cin>>f[i];
}
for(i=0;i<nt;i++)
{
cout<<"Enter follow["<<n1[i]<<"]:";
cin>>fl[i];
}
for(i=0;i<n;i++)
{
p1=scannt(p[i][0]);
if((q=scant(p[i][3]))!=-1)
m[p1][q]=i;
if((q=scannt(p[i][3]))!=-1)
{
for(j=0;j<strlen(f[q]);j++)
m[p1][scant(f[q][j])]=i;
}
if(p[i][3]=='e')
{
for(j=0;j<strlen(fl[p1]);j++)
m[p1][scant(fl[p1][j])]=i;
}
}
for(i=0;i<t;i++)
cout<<"\t"<<t1[i];
cout<<endl;
for(j=0;j<nt;j++)
{
cout<<n1[j];
for(i=0;i<t;i++)
{
cout<<"\t"<<" ";
if(m[j][i]!=-1)
cout<<p[m[j][i]];
S.P (2150708) 140123116017
}
cout<<endl;
}
}
void main()
{
clrscr();
parse p;
p.input();
p.process();
getch();
}
Output :-
S.P (2150708) 140123116017
EXPERIMENT :- 5
A.I.M :- WAP to implement PASS-1 of assembler.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char opcode[10],operand[10],label[10],a[10],ad[10],symbol[10],ch; char
code[10][10],code1[10][10]={"33","44","53","57"};
char mnemonic[10][10]={"START","LDA","STA","LDCH","STCH","END"};
char mnemonic1[10][10]={"LDA","STA","LDCH","STCH"};
int locctr,start,length,i=0,j=0,k,l=0;
int st,diff,address,add,len,actual_len,finaddr,prevaddr;
FILE *fp1,*fp2,*fp3,*fp4,*fp5,*fp6,*fp7;
clrscr();
fp1=fopen("INPUT.DAT","r");
fp2=fopen("SYMTAB.DAT","w");
fp3=fopen("INETERMED.DAT","w");
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
start=atoi(operand);
locctr=start;
fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
else
locctr=0;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp3,"%d",locctr);
if(strcmp(label,"**")!=0)
fprintf(fp2,"%s\t%d\n",label,locctr);
strcpy(code[i],mnemonic[j]);
while(strcmp(mnemonic[j],"END")!=0)
{
S.P (2150708) 140123116017
if(strcmp(opcode,mnemonic[j])==0)
{
locctr+=3;
break;
}
strcpy(code[i],mnemonic[j]);
j++;
}
if(strcmp(opcode,"WORD")==0)
locctr+=3;
else if(strcmp(opcode,"RESW")==0)
locctr+=(3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0)
locctr+=(atoi(operand));
else if(strcmp(opcode,"BYTE")==0)
++locctr;
fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
length=locctr-start;
fcloseall();
printf("\n\nThe contents of Input file:\n\n");
fp1=fopen("INPUT.DAT","r");
ch=fgetc(fp1);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp1);
}
printf("\n\nLength of the input program is %d.",length);
printf("\n\nThe contents of Symbol Table:\n\n");
fp2=fopen("SYMTAB.DAT","r");
ch=fgetc(fp2);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp2);
}
fcloseall();
S.P (2150708) 140123116017
fp4=fopen("ASSMLIST.DAT","w");
fp5=fopen("SYMTAB.DAT","r");
fp6=fopen("INTERMED.DAT","r");
fp7=fopen("OBJCODE.DAT","w");
fscanf(fp6,"%s%s%s",label,opcode,operand);
while(strcmp(opcode,"END")!=0)
{
prevaddr=address;
fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);
}
finaddr=address;
fclose(fp6);
fp6=fopen("INTERMED.DAT","r");
fscanf(fp6,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp4,"\t%s\t%s\t%s\n",label,opcode,operand);
fprintf(fp7,"H^%s^00%s^00%d\n",label,operand,finaddr);
fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);
st=address;
diff=prevaddr-st;
fprintf(fp7,"T^00%d^%d",address,diff);
}
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"BYTE")==0)
{
fprintf(fp4,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
len=strlen(operand);
actual_len=len-3;
fprintf(fp7,"^");
for(k=2;k<(actual_len+2);k++)
{
itoa(operand[k],ad,16);
fprintf(fp4,"%s",ad);
fprintf(fp7,"%s",ad);
}
fprintf(fp4,"\n");
}
else if(strcmp(opcode,"WORD")==0)
S.P (2150708) 140123116017
{
len=strlen(operand);
itoa(atoi(operand),a,10);
fprintf(fp4,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,);
fprintf(fp7,"^00000%s",a);
}
else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0))
fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
else
{
while(strcmp(opcode,mnemonic1[l])!=0)
l++;
if(strcmp(operand,"COPY")==0)
fprintf(fp4,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opc
ode,operand,code1[l]);
else
{
rewind(fp5);
fscanf(fp5,"%s%d",symbol,&add);
while(strcmp(operand,symbol)!=0)
fscanf(fp5,"%s%d",symbol,&add);
fprintf(fp4,"%d\t%s\t%s\t%s\t%s%d\n",address,lab
el,opcode,operand,code1[l],add);
fprintf(fp7,"^%s%d",code1[l],add);
}
}
fscanf(fp6,"%d%s%s%s",&address,label,opcode,operand);
}
fprintf(fp4,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
fprintf(fp7,"\nE^00%d",st);
printf("\nObject Program has been generated.");
fcloseall();
printf("\n\nObject Program:\n\n");
fp7=fopen("OBJCODE.DAT","r");
ch=fgetc(fp7);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp7);
}
S.P (2150708) 140123116017
fcloseall();
getch();
}
Output :-
S.P (2150708) 140123116017
EXPERIMENT :- 6
A.I.M :- WAP to implement PASS-2 of assembler.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],ad[10],label[10],opcode[10],operand[10],symbol[10],ch;
int st,diff,i,address,add,len,actual_len,finaddr,prevaddr,j=0;
char mnemonic[15][15]={"LDA","STA","LDCH","STCH"};
char code[15][15]={"33","44","53","57"};
FILE *fp1,*fp2,*fp3,*fp4;
clrscr();
fp1=fopen("ASSMLIST.DAT","w");
fp2=fopen("SYMTAB.DAT","r");
fp3=fopen("INTERMED.DAT","r");
fp4=fopen("OBJCODE.DAT","w");
fscanf(fp3,"%s%s%s",label,opcode,operand);
while(strcmp(opcode,"END")!=0)
{
prevaddr=address;
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
finaddr=address;
fclose(fp3);
fp3=fopen("INTERMED.DAT","r");
fscanf(fp3,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp1,"\t%s\t%s\t%s\n",label,opcode,operand);
fprintf(fp4,"H^%s^00%s^00%d\n",label,operand,finaddr);
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
st=address;
diff=prevaddr-st;
fprintf(fp4,"T^00%d^%d",address,diff);
}
S.P (2150708) 140123116017
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"BYTE")==0)
{
fprintf(fp1,"%d\t%s\t%s\t%s\t",address,label,opcode,operand);
len=strlen(operand);
actual_len=len-3;
fprintf(fp4,"^");
for(i=2;i<(actual_len+2);i++)
{
itoa(operand[i],ad,16);
fprintf(fp1,"%s",ad);
fprintf(fp4,"%s",ad);
}
fprintf(fp1,"\n");
}
else if(strcmp(opcode,"WORD")==0)
{
len=strlen(operand);
itoa(atoi(operand),a,10);
fprintf(fp1,"%d\t%s\t%s\t%s\t00000%s\n",address,label,opcode,operand,a);
fprintf(fp4,"^00000%s",a);
}
else if((strcmp(opcode,"RESB")==0)||(strcmp(opcode,"RESW")==0))
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
else
{
while(strcmp(opcode,mnemonic[j])!=0)
j++;
if(strcmp(operand,"COPY")==0)
fprintf(fp1,"%d\t%s\t%s\t%s\t%s0000\n",address,label,opcode,operand,code[j]);
else
{
rewind(fp2);
fscanf(fp2,"%s%d",symbol,&add);
while(strcmp(operand,symbol)!=0)
fscanf(fp2,"%s%d",symbol,&add);
fprintf(fp1,"%d\t%s\t%s\t%s\t%s%d\n",address,label,opcode,operand,code[j],add);
fprintf(fp4,"^%s%d",code[j],add);
S.P (2150708) 140123116017
}
}
fscanf(fp3,"%d%s%s%s",&address,label,opcode,operand);
}
fprintf(fp1,"%d\t%s\t%s\t%s\n",address,label,opcode,operand);
fprintf(fp4,"\nE^00%d",st);
printf("\n Intermediate file is converted into object code");
fcloseall();
}
fcloseall();
getch();
}
Input Files :-
INTERMED.DAT
COPY START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 1
2015 FIVE WORD 5
2018 CHARZ BYTE C'EOF'
2019 C1 RESB 1
2020 ** END **
SYMTAB.DAT
ALPHA 2012
FIVE 2015
CHARZ 2018
C1 2019
S.P (2150708) 140123116017
Output :-
S.P (2150708) 140123116017
EXPERIMENT :- 7
A.I.M :- WAP to find substring of length greater than or equal to 3 from given
string which must be palindrome and unique
#include <stdio.h>
#include <string.h>
int start = 0;
int len = strlen(str);
low = i - 1;
high = i;
while (low >= 0 && high < len && str[low] == str[high])
{
if (high - low + 1 > maxLength)
{
start = low;
maxLength = high - low + 1;
}
--low;
++high;
}
low = i - 1;
high = i + 1;
while (low >= 0 && high < len && str[low] == str[high])
{
if (high - low + 1 > maxLength)
{
start = low;
S.P (2150708) 140123116017
return maxLength;
}
int main()
{
char str[] = "forgeeksskeegfor";
printf("\nLength is: %d\n", longestPalSubstr( str ) );
return 0;
}
Output :-
S.P (2150708) 140123116017
EXPERIMENT :- 8
A.I.M :- Write a program which implements Operator Precedence Grammar.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
char str[20],stk[20],pstk[20];
int tos=-1,flag=0,ptr=0,rm=-1,i,j;
char q[9][9]={
{'>','>','<','<','<','<','>','<','>'},
{'>','>','<','<','<','<','>','<','>'},
{'>','>','>','>','<','<','>','<','>'},
{'>','>','>','>','<','<','>','<','>'},
{'>','>','<','<','<','<','>','<','>'},
{'<','<','<','<','<','<','=','<','E'},
{'>','>','>','>','>','E','>','E','>'},
{'>','>','>','>','>','E','>','E','>'},
{'<','<','<','<','<','<','E','<','A'},
};
char c[9]={'+','-','*','/','^','a','(',')','$'};
void pushin(char a)
{
tos++;
stk[tos]=a;
}
char popout()
{
char a;
a=stk[tos];
tos--;
return(a);
}
S.P (2150708) 140123116017
int find(char a)
{
switch(a)
{
case'+':return 0;
case'-':return 1;
case'*':return 2;
case'/':return 3;
case'^':return 4;
case'(':return 5;
case')':return 6;
case'a':return 7;
case'$':return 8;
}
return-1;
}
void display(char a)
{
printf("\n SHIFT %c",a);
}
void display1(char a)
{
if(a!='(')
{
if(isalpha(a))
printf("\n REDUCE E--> %c",a);
else if(a==')')
printf("\n REDUCE E-->(E)");
else
printf("\n REDUCE E-->E %c E",a);
}
}
{
if(isalpha (a))
a='a';
if(isalpha(b))
b='a';
if(q[find(a)][find(b)]==d)
return 1;
else
return 0;
}
void main()
{
clrscr();
printf("\n\n\t The productions used are:\n\t");
printf("E-->E*E/E+E/E^E/E*E/E-E\n\tE-->E/E \n\tE-->a/b/c/d/e.../z");
printf("\n\t Enter an expression that terminals with $:");
fflush(stdin);
i=-1;
while(str[i]!='$')
{
i++;
scanf("%c",&str[i]);
}
for(j=0;j<i;j++)
{
if((str[j]=='(')||(str[j]==')')||(str[j+1]=='(')||(str[j+1]==')'))
{}
else if
((isalpha(str[j])==0)&&(isalpha(str[j+1])==0))||((isalpha(str[j])!=0)&&(isalpha(str[j+1])!=0)))
{
printf("ERROR");
getch();
exit(0);
}
}
S.P (2150708) 140123116017
if((((isalpha(str[0]))!=0)||(str[0]=='('))&&(((isalpha(str[i-1]))!=0)||(str[i-1]==')')))
{
pushin('$');
printf("\n\n\n\t+\t-\t*\t/\t^\ta\t(\t)\t$\n\n");
for(i=0;i<9;i++)
{
printf("%c",c[i]);
for(j=0;j<9;j++)
printf("\t%c",q[i][j]);
printf("\n");
}
getch();
while(1)
{
if(str[ptr]=='$' && stk[tos]=='$')
{
printf("\n\n\t ACCEPT!");
break;
}
else if(rel(stk[tos],str[ptr],'<')||rel(stk[tos],str[ptr],'=='))
{
display(str[ptr]);
pushin(str[ptr]);
ptr++;
}
else if(rel(stk[tos],str[ptr],'>'))
{
do
{
rm++;
pstk[rm]=popout();
display1(pstk[rm]);
}
while(!rel(stk[tos],pstk[rm],'<'));
}
S.P (2150708) 140123116017
else
{
printf("\n\n\t NOT ACCEPTED!!!!!!!");
getch();
exit(1);
}
}
getch();
}
else
{
printf("ERROR");
getch();
}
}
Output :-
S.P (2150708) 140123116017
EXPERIMENT :- 9
A.I.M :- Write down following Macros:
1. Write a Macro that moves n numbers from the first operand to
second operand. N is specified as third operand.
2. Explain Macro substitution in C Language.
1). Write a Macro that moves n numbers from the first operand to second
operand. N is specified as third operand.
The preprocessor replaces every occurrence of the identifier int the source code by a string. The
definition should start with the keyword #define and should follow on identifier and a string with
at least one blank space between them. The string may be any text and identifier must be a valid
c name.
There are different forms of macro substitution. The most common form is
1. Simple macro substitution
2. Argument macro substitution
3. Nested macro substitution
Writing macro definition in capitals is a convention not a rule a macro definition can include
more than a simple constant value it can include expressions as well. Following are valid
examples:
Macros as arguments:
The preprocessor permits us to define more complex and more useful form of replacements it
takes the following form.
Notice that there is no space between identifier and left parentheses and the identifier f1,f2,f3 .
Fn is analogous to formal arguments in a function definition.
There is a basic difference between simple replacement discussed above and replacement of
macro arguments is known as a macro call
volume=CUBE(side);
volume =(side*side*side)
Nesting of macros:
We can also use one macro in the definition of another macro. That is macro definitions may be
nested. Consider the following macro definitions
# define SQUARE(x)((x)*(x))
Undefining a macro:
# undef identifier.
This is useful when we want to restrict the definition only to a particular part of the program.
S.P (2150708) 140123116017
EXPERIMENT :- 10
A.I.M :- Study of following Linker and Loader algorithms:
1. Program Relocation
2. Program Linking
Linker:
1) Program Relocation
2) Program Linking
Loader:
Relocating loader