6.26.2010

MACRO PROCESSOR PGM

#include
#include
#include
#include
#define FALSE 0;
#define TRUE 1;
int expanding=FALSE;
struct input
{
char label[30],opcode[30],operand[30];
};
struct DEFTAB
{
char label[30],opcode[30],operand[30];
} ;
struct ARGTAB
{
char arg[30];
};
struct NAMTAB
{
char macroname[30];
int ptr;
};
NAMTAB nt[20];
int ntv=0;
ARGTAB argtab;

DEFTAB t[50];
int tv=0;
input ip[50];
int ipv=0;
char oper[30];
int q;
void expand(char label[],char opcode[],char operand[]);
void define(char label[],char opcode[],char operand[]);
int search(char opcode[])
{
for(int i=0;i if(strcmp(nt[i].macroname,opcode)==0)
return(nt[i].ptr);
return(-1);
}

void processline(char label[],char opcode[30],char operand[])
{
int k=search(opcode);
if(k!=-1)
{
tv=k;
expand(label,opcode,operand);
}
else if(strcmp(opcode,"macro")==0)
{
char a[30];
int j=0;
for(int i=0;operand[i]!='\0';i++)
{
if(operand[i]!='&')
a[j++]=operand[i];
}
a[j]='\0';
strcpy(operand,a);
define(label,opcode,operand);
}
else
{
cout<<"\n"< }
return;
}
void getline(char label[30],char opcode[30],char operand[30])
{
int l;
if(expanding==1)
{
strcpy(label,t[tv].label);
strcpy(opcode,t[tv].opcode);
strcpy(operand,t[tv++].operand);
l=strlen(operand);
l--;
operand[0]=argtab.arg[l];
operand[1]='\0';
}
else
{
strcpy(label,ip[ipv].label);
strcpy(opcode,ip[ipv].opcode);
strcpy(operand,ip[ipv++].operand);
}
return;
}
void expand(char label[],char opcode[],char operand[])
{
expanding=TRUE;
strcpy(argtab.arg,operand);
getline(label,opcode,operand);
while(strcmp(opcode,"mend")!=0)
{
processline(label,opcode,operand);
getline(label,opcode,operand);
}
expanding=FALSE;
tv=0;
return;
}
void define(char label[],char opcode[],char operand[])
{

int level=1,l,i=0;
char *str;
strcpy(oper,operand);
strcpy(nt[ntv].macroname,label);
nt[ntv].ptr=tv;
ntv++;
while(level>0)
{
i=0;
getline(label,opcode,operand);
if(strcmp(operand,"*")!=0)
{
str=strchr(oper,operand[0]);
l=str-oper;
operand[0]='\0';
while(i<=l)
{
strcat(operand,"?");
i++;
}
}
strcpy(t[tv].label,label);
strcpy(t[tv].opcode,opcode);
strcpy(t[tv++].operand,operand);
if(strcmp(opcode,"macro")==0)
{
level++;
}
else if(strcmp(opcode,"mend")==0)
{
level--;
}
}
/* for( i=0;i<=tv;i++)
cout<<"\n"< getch(); */
return;
}
void main()
{
clrscr();
char label[30],opcode[30],operand[30];
fstream fp;
fp.open("macroin.txt",ios::in);
int i=0;
while(fp.eof()!=1)
{
fp>>ip[i].label>>ip[i].opcode>>ip[i].operand;
i++;
}
while(strcmp(opcode,"end")!=0)
{
getline(label,opcode,operand);
processline(label,opcode,operand);
}
getch();
return;
}

No comments: