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

17

The document contains code for implementing a top-down parser in C/C++. It defines a Parse class with methods to scan terminal and non-terminal symbols, take input for grammar productions and parse table, and process the grammar to populate the parse table. The main methods are input() to take grammar as input, process() to analyze the grammar and populate the parse table, scannt() and scant() to scan symbols.

Uploaded by

Kunal Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

17

The document contains code for implementing a top-down parser in C/C++. It defines a Parse class with methods to scan terminal and non-terminal symbols, take input for grammar productions and parse table, and process the grammar to populate the parse table. The main methods are input() to take grammar as input, process() to analyze the grammar and populate the parse table, scannt() and scant() to scan symbols.

Uploaded by

Kunal Joshi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

S.

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

A.I.M :- (B)WAP to find number of string in a file.

#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

A.I.M :- write a program to replace a string from a file using c/cpp

#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

else //when the line to be deleted comes


{
while ((c = getc(fp1)) != '') {
}
//read and skip the line ask for new text
printf("Enter new text");
//flush the input stream
fflush(stdin);
putc('', fp2); //put '' in new file
while ((c = getchar()) != ''){
putc(c, fp2);
//take the data from user and place it in new file
fputs("
", fp2);
temp++;
}
//continue this till EOF is encountered
c = getc(fp1);
}
//close both files
fclose(fp1);
fclose(fp2);
//remove original file
remove(filename);
//rename new file with old name opens the file in read mode
rename("copy.c", filename);
fp1 = fopen(filename, "r");
//reads the character from file
c = getc(fp1);
//until last character of file is encountered
while (c != EOF){
printf("%c", c);
//all characters are printed
c = getc(fp1);
}
//close the file pointer
fclose(fp1);
return 0;
}
}
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>

void keyw(char *p);

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

printf("\n\nThe contents of Intermediate file:\n\n\t");


fp3=fopen("INTERMED.DAT","r");
ch=fgetc(fp3);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp3);
}
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);
}
printf("\n\nThe contents of Output file :\n\n");
fp1=fopen("ASSMLIST.DAT","r");
ch=fgetc(fp1);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp1);
}
printf("\n\nThe contents of Object code file :\n\n");
fp4=fopen("OBJCODE.DAT","r");
ch=fgetc(fp4);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp4);
S.P (2150708) 140123116017

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

void printSubStr(char* str, int low, int high)


{
for( int i = low; i <= high; ++i )
printf("%c", str[i]);
}

int longestPalSubstr(char *str)


{
int maxLength = 1; // The result (length of LPS)

int start = 0;
int len = strlen(str);

int low, high;

for (int i = 1; i < len; ++i)


{

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

maxLength = high - low + 1;


}
--low;
++high;
}
}

printf("Longest palindrome substring is: ");


printSubStr(str, start, start + maxLength - 1);

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

int rel(char a,char b,char d)


S.P (2150708) 140123116017

{
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.

2). Explain Macro substitution in C Language.


Macros:
Macro substitution is a process where an identifier in a program is replaced by a pre defined
string composed of one or more tokens we can use the #define statement for the task.

It has the following form

#define identifier string

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

Simple macro substitution:


Simple string replacement is commonly used to define constants example:
#define pi 3.1415926
S.P (2150708) 140123116017

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:

#define AREA 12.36

Macros as arguments:

The preprocessor permits us to define more complex and more useful form of replacements it
takes the following form.

# define identifier(f1,f2,f3..fn) string.

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

A simple example of a macro with arguments is

# define CUBE (x) (x*x*x)

If the following statements appears later in the program,

volume=CUBE(side);

The preprocessor would expand the statement to

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:

A defined macro can be undefined using the statement

# 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

1. program_linked_origin := <link origin> from the linker command;

2. for each object module mentioned in the linker command

a) t_origin := translated origin of the object module;


OM_size := size of the object module;
b) relocation_factor := program_linked_origin t_origin:
c) Read the machine language program contained in the program component of the object
module into the work_area.
d) Read RELOCTAB of the object module.
e) For each entry in RELOCTAB
i. Translated_address := address found in the RELOCTAB entry;
ii. Address_in_work_area := address of work_area + translated_address t_origin;
iii. Add relocation_factor to the operand address found in the word that has the
address address_in_work_area.
f) program_linked_origin := program_linked_origin + OM_size;

2) Program Linking

1. program_linked_origin := <link origin> from the linker command;

2. for each object module mentioned in the linker command

a) t_origin := translated origin of the object module;


OM_size := size of the object module;
b) relocation_factor := program_linked_origin t_origin:
c) Read the machine language program contained in the program component of the object
module into the work_area.
d) Read LINKTAB of the object module.
e) Enter (object module name, program_linked_origin) in NTAB.
f) For each LINKTAB entry with type = PD
name := symbol field of the LINKTAB entry;
linked_address := translated_address + relocation_factor;
Enter (name, linked_address) in a new entry of the NTAB.
S.P (2150708) 140123116017

g) program_linked_origin := program_linked_origin + OM_size;

3. For each object module mentioned in the linker command

a) t_origin := translated origin of the object module;


program_linked_origin := linked_address from NTAB.
b) For each LINKTAB entry with type = EXT
i. address_in_work_area := address of work_area + program_linked_origin - <link
origin> in linker command + translated_address t_origin;
ii. Search the symbol found in the symbol field of the LINKTAB entry in NTAB and
note its linked address. Copy this address into the operand address field in the
word that has the address address_in_work_area.

Loader:

Relocating loader

1. program_load_origin := load origin specified in the loader command;


2. program_linked_origin := linked origin specified in the header record;
3. relocation_factor := program_load_origin - program_linked_origin;
4. for each binary image record
a) code_linked_address := linked address specified in the record;
b) code_load_address := code_linked_address + relocation_factor;
c) byte_count := count of the number of bytes in the record;
d) Move byte_count bytes from the record to the memory area with start address
code_load_address.
5. Read RELOCTAB of the program.
6. For each entry in the RELOCTAB
a) instruction_linked_address := address specified in the RELOCTAB entry;
b) instruction_load_address := instruction_linked_address + relocation_factor;
Add relocation_factor to the operand address used in the instruction that has the address
instruction_load_address.

You might also like