Showing posts with label system software. Show all posts
Showing posts with label system software. Show all posts

6.26.2010

PASS 1,2

FUCT.CPP
#include
#include
#include
#include
#include
#include

int hexdec(char m[10])
{
char ds[10];
strcpy(ds,m);
char *s=strrev(ds);
int l=strlen(s);
char d[3]="00";
int i=0,sum=0;
while(i {
if(s[i]>=48 && s[i]<=57)
d[1]=s[i];
else
{
if(s[i]=='A')
strcpy(d,"10");
else if(s[i]=='B')
strcpy(d,"11");
else if(s[i]=='C')
strcpy(d,"12");
else if(s[i]=='D')
strcpy(d,"13");
else if(s[i]=='E')
strcpy(d,"14");
else if(s[i]=='F')
strcpy(d,"15");
}
// printf("-%s- ",d);
sum=sum+ (pow(16,i)*atof(d));
// printf("<%d> ",sum);

i++;
}
return sum;
}


PASS.C
#include "FUNCT.CPP"

int locctr=0;
int preloc=0;
int startAdd=0;
int proLength;

class Statement
{
public:
char label[20];
char opcode[20];
char operand[20];
void upper();
};
void Statement::upper()
{
strcpy(label,strupr(label));
strcpy(opcode,strupr(opcode));
strcpy(operand,strupr(operand));
}
class Intermediate:public Statement
{
public:
char location[20];
char label[20];
char opcode[20];
char operand[20];
void insert(int a,char b[],char c[],char d[])
{
char *st;
sprintf(st,"%x",a);
strcpy(location,st);
strcpy(label,b);
strcpy(opcode,c);
strcpy(operand,d);
cout< }
};

class Optab
{
public:
char opcode[20];
char opvalue[10];
friend int search(char *s);
};
Optab Op[]={ {"ADD","18"},{"SUB","1C"} };

int search(char s[])
{
for(int i=0;i<2;i++)
{
if( stricmp(Op[i].opcode,s) ==0 )
{
return 1;
}
}
return 0;
}


class Symtab
{
public:
char label[20][20];
int address[20];
int top;
Symtab()
{
top=-1;
}
int search(char *sp)
{
for(int i=0;i<=top;i++)
{
if(stricmp(sp,label[i])==0)
{
return 1;
}
}
return 0;
}
void insert(char *lab,int ad)
{
top++;
strcpy(label[top],lab);
address[top]=ad;
}
};
void readprogram()//function read program
{
ofstream f1;
Statement s;
f1.open("source.txt",ios::binary);
cout<<"Type your program: \n";
cin>>s.label>>s.opcode>>s.operand;
while( stricmp(s.opcode,"END") !=0 )
{
s.upper();
f1.write( (char*)&s,sizeof(Statement));
cin>>s.label>>s.opcode>>s.operand;
s.upper();
}
f1.write( (char*)&s,sizeof(Statement));
f1.close();
}
void printprogram()//function print program
{
ifstream f1;
Statement s;
f1.open("source.txt",ios::binary);
cout<<"\nYour program: \n";
while(f1.read( (char*)&s,sizeof(Statement)))
{
cout< }
f1.close();
}

void pass1()
{

ifstream f2;
ofstream f3;
Statement s;
Symtab S;
Intermediate I;
f2.open("source.txt",ios::binary);
f3.open("intermediate.txt",ios::binary);

f2.read( (char*)&s,sizeof(Statement));

if( stricmp(s.opcode,"START")==0 || !f2.eof() )
{
startAdd=hexdec(s.operand);
locctr=startAdd;
preloc=startAdd;
I.insert(preloc,s.label,s.opcode,s.operand);
// f3.write( (char*)&I,sizeof(Intermediate) );
f2.read( (char*)&s,sizeof(Statement));
}
while( !stricmp(s.opcode,"END") && !f2.eof())
{
if( stricmp(s.label,"-")!=0 )
{
if( S.search(s.label)==1)
cout<<"Error : duplicate label \n";
else
S.insert(s.label,locctr);
}
if( search(s.opcode)==1 )
{
locctr+=3;
}
else if( stricmp(s.opcode,"WORD")==0 )
{
locctr=locctr+3;
}
else if( stricmp(s.opcode,"RESW")==0 )
{
locctr=locctr+3*hexdec(s.operand);
}
else if( stricmp(s.opcode,"RESB")==0 )
{
locctr=locctr+hexdec(s.operand);
}
else if( stricmp(s.opcode,"BYTE")==0 )
{
locctr=locctr+strlen(s.operand)-3;
}
else
{
cout<<"Error : opcode not found \n"< }
I.insert(preloc,s.label,s.opcode,s.operand);
// f3.write( (char*)&I,sizeof(Intermediate) );
f2.read( (char*)&s,sizeof(Statement));
preloc=locctr;
}//while end
I.insert(preloc,s.label,s.opcode,s.operand);
// f3.write( (char*)&I,sizeof(Intermediate) );
proLength=locctr-startAdd;
f2.close();
// f3.close();
}

void main()
{
clrscr();
readprogram();
printprogram();
// pass1();
getch();
return;
}

e-CLOSURE

#include
#include
#include
#include
char ttable[30][10][10];
int stack[30],top=-1;
void eclosure(int start,int end,int *closure)
{
while(top!=-1)
{
for(int i=start;i<=end;i++)
{
if(strchr(ttable[stack[top]][i],'e')!=NULL)
{

if(closure[i]==44)
{
top++;
stack[top]=i;
closure[i]=i;
i=start;
}
}
}
top--;
}
return;
}
void main()
{
clrscr();
int sstate,estate,start,end;
char in[30];
fstream p;
p.open("input.txt",ios::in); //input of form state state transition
p>>start>>end;
p>>sstate>>estate>>in;
while(p.eof()!=1)
{
strcat(ttable[sstate][estate],in);
p>>sstate>>estate>>in;
}
int n;
cout<<"Enter no. of states: ";
cin>>n;
int closure[10]={44,44,44,44,44,44,44,44,44,44},j=1;
cout<<"Enter states: ";
while(j<=n)
{
cin>>sstate;
stack[++top]=sstate;
closure[sstate]=sstate;
j++;
}
eclosure(start,end,closure);
cout<<"closure set is: ";
for(int i=0;i<10;i++)
{
if(closure[i]!=44)
cout< }
getch();
return;
}

REC-DESCENT PARSER

#include
#include
#include
char ip[30];
int v=0;
void T();
void Eprime();
void F();
void Tprime();
void E()
{
T();
Eprime();
return;
}
void T()
{
F();
Tprime();
return;
}
void Eprime()
{
if(ip[v]=='+')
{
v++;
T();
Eprime();
}
return;
}
void F()
{
if(ip[v]=='(')
{
v++;
E();
if(ip[v]==')')
{
v++;
}
else
{
cout<<"Error.....";
exit(0);
}
}
else if(ip[v]=='i' &&ip[v+1]=='d')
{
v=v+2;
}
else
{
cout<<"Error.......";
exit(0);
}
return;
}
void Tprime()
{
if(ip[v]=='*')
{
v++;
F();
Tprime();
}
return;
}
void main()
{
clrscr();
cout<<"Enter input string: ";
cin>>ip;
E();
cout<<"\n\nString succesfully parsed";
getch();
return;
}

OPERATOR PRESEDANCE

#include
#include
#include
#include
char stack[30];
int top=-1;
void main()
{
clrscr();
fstream filep;
char str[30],terminal[30],a,table[30][30];
int i=0;
cout<<"Enter input string: ";
cin>>str;
filep.open("prece-re.txt",ios::in);
filep>>terminal;
while(filep.eof()!=1)
{
filep>>table[i++];
}
filep.close();
char stop,in,*ptr;
int p,q;
i=0;
top++;
stack[top]='$';
while(1)
{
stop=stack[top];
in=str[i];
if(stop=='$' && in=='$')
{
cout<<"\nstring accepted";
break;
}
else
{
ptr=strchr(terminal,stop);
p=ptr-terminal;
ptr=strchr(terminal,in);
q=ptr-terminal;
if(table[p][q]=='<' || table[p][q]=='=')
{
top++;
stack[top]=table[p][q];
top++;
stack[top]=in;
i++;
}
else if(table[p][q]=='>')
{
while(stack[top]!='<' && top!=-1)
{
top--;
}
if(top==-1)
{
cout<<"\n\nError..." ;
break;
}
top--;
}
else
{
cout<<"\n\nError...";
break;
}
}
}
getch();
return;
}

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

FIRST AND FOLLOW

#include
#include
#include
#include
char p[50][20][20];
char first[30],follow[30],nonter[30],ter[30];
int v=0,s;
void foundfirst(char non)
{

char *ptr;
int a;
ptr=strchr(nonter,non);
if(ptr!=NULL)
a=ptr-nonter;
if(ptr!=NULL)
{
for(int i=0;i<10;i++)
{
if(p[a][i][0]!='\0')
{
non=p[a][i][0];
foundfirst(non);
}
}
}
else
{
first[v++]=non;
first[v]='\0';
return;
}
}
void findfollow(char non[])
{
char *ptr=strchr(nonter,non[0]);
if(ptr!=NULL)
{
for(int i=0;i<=10;i++)
{

for(int j=0;j<10;j++)
{
char *b=strstr(p[i][j],non);
if(b!=NULL)
{
v=0;
char f[30];
strcpy(f,b);
if(strcmp(b+1,"\0")==0)
f[1]='e';
foundfirst(f[1]);

strcat(follow,first);
s=strlen(follow);
if(strstr(first,"e")!=NULL)
{
v=0;
if(nonter[i]!=non[0])
{
non[0]=nonter[i];
non[1]='\0';
findfollow(non);
}
}
}
}
}
}
else
{
follow[s++]=non[0];
follow[s]='\0';
return;
}
return;
}
void main()
{
clrscr();
char start;
fstream fp;
fp.open("grammer.txt",ios::in);
char sym,temp[30];
int a1,i=0,j=0;
fp>>ter;
fp>>nonter;
fp>>temp;
do
{
j=0;
while(strcmp(temp,"$")!=0)
{
strcpy(p[i][j],temp);
fp>>temp;
j++;
}
i++;
fp>>temp;
}while(fp.eof()!=1);
fp.close();

cout<<"\nEnter symbol: ";
cin>>sym;
foundfirst(sym);
cout<<"\nFirst: {" ;
for(i=0;first[i]!='\0';i++)
cout<cout<<"}";
char nonterm1[30];
cout<<"\nEnter symbol: ";
cin>>nonterm1;
findfollow(nonterm1);
cout<<"\nFollow: {" ;
for(i=0;follow[i]!='\0';i++)
{
if(follow[i]!='e')
cout<}
if(nonterm1[0]==nonter[0])
cout<<"$";
cout<<"}";
getch();
return;
}

LEADING TRAILING

#include
#include
#include
#include
char t[30][10][10],lset[30];
int h;
void leading(char ter[],char nonter[],char sym)
{
char *str;
int post,posnont;
str=strchr(nonter,sym);
posnont=str-nonter;
for(int i=0;i<10;i++)
{

if(t[posnont][i][0]!='\0')
{
if(strchr(ter,t[posnont][i][0])!=NULL)
{
str=strchr(ter,t[posnont][i][0]);
post=str-ter;
lset[post]=t[posnont][i][0];

}
else if(strchr(nonter,t[posnont][i][0])!=NULL && t[posnont][i][1]!='\0')
{
str=strchr(ter,t[posnont][i][1]);
post=str-ter;
lset[post]=t[posnont][i][1];
if(t[posnont][i][0]!=sym)
leading(ter,nonter,t[posnont][i][0]);

}
else
{
leading(ter,nonter,t[posnont][i][0]);
}
}
}
return;
}

void main()
{
clrscr();
char ter[30],nonter[30],temp[30];
int i=0,j=0;
fstream fp;
fp.open("grammer.txt",ios::in);
fp>>ter;
fp>>nonter;
fp>>temp;
do
{
j=0;
while(strcmp(temp,"$")!=0)
{
strcpy(t[i][j],temp);
fp>>temp;
j++;
}
i++;
fp>>temp;
}while(fp.eof()!=1);
fp.close();
char sym;
cout<<"Enter symbol: ";
cin>>sym;
leading(ter,nonter,sym);
j=0;
cout<<"\n\n";
while(j<10)
{
if(strchr(ter,lset[j])!=NULL)
{
cout< }
j++;
}
getch();
return;
}

BOOT LOADER

#include
#include
#include

void main()
{
clrscr();
fstream fp;
fp.open("objcode.txt",ios::in);
char name[20],temp[20],a,b;
int address,length,i;
fp>>temp>>name>>temp>>temp;
fp.setf(ios::basefield,ios::hex);
cout<<"Program name : "<fp>>a>>address>>length;
do
{
i=0;
while(i {
fp.setf(ios::skipws);
fp>>a;
fp>>b;
cout.setf(ios::basefield,ios::hex);
cout<<"\n"< address+=1;
i++;
}
fp>>a>>address>>length;
}while(a!='E');
cout<<"\n execution address is: "<fp.close();
return;
}

NFA TO DFA

KERALA UNIVERSITY COMPUTER SCIENCE LAB PPROGRAMS

NFA TO DFA CONVERSTION

#include
#include
#include
#include
char nttable[30][30][10],dttable[30][30][10];
int stack[30],top=-1;
struct node
{
int a;
node *link;
};
node *top1=NULL;
int *eclosure(int start,int end)
{
int *closure=NULL;
closure=new int[20];
for(int i=0;i<20;i++)
closure[i]=44;
if(top==-1)
return(NULL);
while(top!=-1)
{
closure[stack[top]]=stack[top];
for(int i=start;i<=end;i++)
{
if(strchr(nttable[stack[top]][i],'e')!=NULL)
{
if(closure[i]==44)
{
top++;
stack[top]=i;
closure[i]=i;
i=start;
}
}
}
top--;
}
return(closure);
}
void findtransition(char a,int b,int start,int end)
{
for(int i=start;i<=end;i++)
{
if(strchr(nttable[b][i],a)!=NULL)
{
top++;
stack[top]=i;
}
}
return;
}
int newstate(int *closure,int *news[30],int nostates)
{
int x;
for(int i=0;i<=nostates;i++)
{
x=0;
for(int j=0;j<20;j++)
{
if(closure[j]!=44)
{
if(closure[j]!=news[i][j])
{
x=1;
j=21;
}
}
}
if(x==0)
return(i);
}
return(-1);
}
void main()
{
clrscr();
fstream p;
char inchar[30];
int start,sstate,estate,end,*news[30],nostates=-1;
char in[30];
cout<<"Enter input characters: ";
cin>>inchar;
p.open("input.txt",ios::in);
p>>start>>end;
do
{
p>>sstate>>estate>>in;
strcat(nttable[sstate][estate],in);
} while(p.eof()!=1);
int *closure;
top++;
stack[top]=start;
closure=eclosure(start,end);
news[++nostates]=closure;
node *temp=new node;
temp->a=nostates;
temp->link=top1;
top1=temp;
int d,yes,g;
cout<<"start end transition";
while(top1!=NULL)
{
d=top1->a;
top1=top1->link;
for(int i=0;i<20;i++)
{
if(news[d][i]!=44)
{
for(int j=0;inchar[j]!='\0';j++)
{
findtransition(inchar[j],news[d][i],start,end);
closure=eclosure(start,end);
if(closure!=NULL)
{

yes=newstate(closure,news,nostates);
g=yes;
if(yes==-1)
{
nostates++;
g=yes;
node *temp=new node;
temp->a=nostates;
temp->link=top1;
top1=temp;
news[nostates]=closure;
}
dttable[d][g][0]=inchar[j];
dttable[d][g][1]='\0';
cout<<"\n"< }
}
}
}
}
getch();
return;
}