Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical 7: AIM: Write A Program To Generate Quadruple Table Form Given String

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Practical 7

AIM: Write a program to generate quadruple table form given string.


#include<stdio.h>
#include<string.h>
main()
{
char line[20];
int s[20];
int t=1;
int i=0;
printf("Enter string.. :");
gets(line);
for(i=0;i<20;i++)s[i]=0;
printf("op\ta1\ta2\tres\n");
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='/' || line[i]=='*')
{
printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='+' || line[i]=='-')
{
printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
printf("\n:=\tt%d\t\t%c",t-1,line[0]);
getch();
}

Output
Enter string.. :a=b*c-b*c

op a1 a2 res
:= c t1
* b t1 t2
:= c t3
* b t3 t4
- t2 t4 t5
:= t5 a
Practical 8
AIM: To write a C program to generate the symbol table for the given assembly language.

#include<stdio.h>
#include<conio.h>
struct sym
{
char lab[10];
int val;
};
void main ()
{
FILE *f1;
char la[10],op[10],opr[10],a[1000],c,key[10];
int i,j,lc=0,m=0,flag,ch=0;
struct sym s[10];
clrscr();
f1=fopen("a1.txt","r");
c=fgetc(f1);
i=0;
printf ("\n SOURCE PROGRAM \n");
while(c!=EOF)
{
a[i]=c;
c=fgetc(f1);
i++;
}
while(ch<4)
{
printf("1-symbol table creation\n");
printf("2-serch\n");
printf("3-display\n");
printf(">3-Exit\n");
printf("enter ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
i=0;
while(strcmp(op,"end")!=0)
{
if(a[i]=='\t')
{
strcpy(la," ");
i++;
}
else
{
j=0;
while(a[i] !='\t')
{
la[j]=a[i];
i++;
j++;
}
la[j]='\0';
i++;
}
if(a[i]=='\t')
{
strcpy(op," ");
i++;
}
else
{
j=0;
while(a[i]!='\t')
{
op[j]=a[i];
i++;
j++;
}
op[j]='\0';
i++;
}
if(a[i]=='\t')
{
strcpy(opr," ");
i++;
}
else
{
j=0;
while(a[i]!='\n')
{
opr[j]=a[i];
i++;
j++;
}
opr[j]='\0';
i++;
}
j=0;
if(strcmp(la," ")!=0)
{
strcpy(s[m].lab,la);
if(strcmp(op,"start")==0)
{
lc=atoi(opr);
s[m].val=lc;
m++;
printf("%s\t%s\t%s\n",la,op,opr);
continue;
}
else if(strcmp(op,"equ")==0)
{
s[m].val=atoi(opr);
m++;
}
else if(strcmp(op,"resw")==0)
{
s[m].val=lc;
lc=lc+atoi(opr) *3;
m++;
}
else if(strcmp(op,"resb")==0)
{
s[m].val=lc;
lc=lc+atoi(opr);
m++;
}
else
{
s[m].val=lc;
lc=lc+3;
m++;
}
}
else
lc=lc+3;
printf("%s\t%s\t%s\n",la,op,opr);
}
break;
case 2:
printf("enter the lable to be searched\n");
scanf("%s",&key);
flag=0;
for(i=0;i<m;i++)
{
if(strcmp(key,s[i].lab)==0)
{
printf("%s\t%d\n",s[i].lab,s[i].val);
flag=1;
break;
}
else
continue;
}
if(flag==0)
printf("lable not found\n");
break;
case 3:
printf("\n symbol table \n");
for(i=0;i<m;i++)
printf("\n%s\t%d\n",s[i].lab,s[i].val);
break;
}
}
}

Output :

start 1000
lda one
add val
sta two
val equ 10
one word 100
two resw 1
end

1-symbol table creation


2-serch
3-display
>3-Exit
enter ur choice
3
symbol table

val 10

one 1009

two 1012

1-symbol table creation


2-serch
3-display
>3-Exit
enter ur choice
2
enter the lable to be searched
val
val 10
1-symbol table creation
2-serch
3-display
>3-Exit
enter ur choice
2

enter the lable to be searched


qw
lable not found
Practical 9
Aim: Implementation of MACRO

#include<stdio.h>
#include<conio.h>
struct name
{
char nt[10];
}nametab[1];
struct arg
{
char ar[30];
int index;
}argtab[1];
void main()
{
FILE *s,*def,*ext;
char label[20],opcode[20],operand[20],in[10];
int nt=0,i=0,j=0,ar=0,pos;
clrscr();
ext=fopen("extern.txt","w");
s=fopen("source.txt","r");
def=fopen("define.txt","w");
fscanf(s,"%s%s%s",label,opcode,operand);
while(strcmp(opcode,"END")!=0)
{
if(strcmp(opcode,"MACRO")==0)
{
strcpy(nametab[nt].nt,label);
printf("Name of the macro=%s",nametab[nt].nt);
nt++;
strcpy(argtab[ar].ar,operand);
argtab[ar].index=1;
//fprintf(def,".\t%s\t%s\n",label,operand);
while(strcmp(opcode,"MEND")!=0)
{
fscanf(s,"%s%s%s",label,opcode,operand);
if(strcmp(operand,argtab[ar].ar)==0)
{
strcpy(operand,"?");
pos=argtab[ar].index;
j=1;
}
fprintf(def,"\t%s\t%s",opcode,operand);
if(j==1)
{
fprintf(def,"%d",pos);
j=0;
}
fprintf(def,"\n");
}
}
for(i=0;i<nt;i++)
{
if(strcmp(opcode,nametab[i].nt)==0)
{
fprintf(ext,".\t%s\t%s\n",opcode,operand);
fclose(def);
strcpy(in,operand);
freopen("define.txt","r",def);
fscanf(def,"%s%s",opcode,operand);
while(strcmp(opcode,"MEND")!=0)
{
if(operand[0]=='?')
strcpy(operand,in);
fprintf(ext,"\t%s\t%s\n",opcode,operand);
fscanf(def,"%s%s",opcode,operand);
}
break;
}
}
if(strcmp(opcode,"MEND")!=0)
{
if(strcmp(label,"*")!=0)
fprintf(ext,"%s\t%s\t%s\n",label,opcode,operand);
else
fprintf(ext,"\t%s\t%s\n",opcode,operand);
}
fscanf(s,"%s%s%s",label,opcode,operand);
}
fprintf(ext,"\t%s\t%s\n",opcode,operand);
getch();
}
INPUT :SOURSE.TXT

PG MACRO &A
* ADD &A
* STA BETA
* MEND *
PGM START 1000
* PG ALPHA
* LDA BETA
* STA GAMMA
* END PGM

OUTPUT:
DEFINE.TXT
ADD ?1
STA BETA
MEND *

EXTERN.TXT
PGM START 1000
. PG ALPHA
LDA BETA
STA GAMMA
END PGM
AIM : Use macro features of C language

#include<stdio.h>
#define SUM(A, B) (A + B)
#define PROD(A, B) (A * B)
int main()
{
int num1, num2, sum, product;
printf("Enter the Two numbers:");
scanf("%d%d",&num1,&num2);
sum=SUM(num1,num2);
product=PROD(num1,num2);
printf("Sum of two numbers using Macros is:%d\n",sum);
printf("Product of two numbers using macros is:%d\n",product);
return 0;
}

OUTPUT:-

Enter the Two numbers: 10 20

Sum of two numbers using Macros is: 30

Product of two numbers using macros is: 200

You might also like