Saturday, July 28, 2007
Thursday, January 25, 2007
SYSTEM SOFTWARE LAB PROGRAMS
TWO PASS ASSEMBLER
#include
#include
#include
#include
#include
char line[30],LABEL[10],OPECODE[10],OPERAND[10];
char NAME[7],START[7];
int index=0,count,LOCCTR,BEG,PGMLEN;
FILE *fp,*opcode;
struct symtab
{
char name[10];
char value[10];
}SYMTAB[20];
struct optab
{
char name[10];
char value[10];
}OPTAB[20];
int ssize=0,osize=0;
struct inter
{
char loc[5];
char label[10];
char opecode[10];
char operand[10];
}INTER;
struct header
{
char t;
char name[6];
char start[6];
char len[6];
}HEADER={'H'};
struct text
{
char t;
char code[19];
char start[6];
char len[6];
}TEXT={'T'};
struct end
{
char t;
char first[6];
}END={'E'};
void READ()
{
int i=0,j=0,k=0;
strset(line,'\0');
index=0;
do
{ line[i++]=getc(fp);
if(feof(fp))
{
line[i-1]='\n';
break;
}
}
while (line[i-1]!='\n');
i=0;
while (line[i]==' ')
i++;
k++;
while (line[i]!=' ')
LABEL[j++]=line[i++];
LABEL[j]='\0';
while(line[i]==' ')
i++;
j=0;
k++;
while(line[i]!=' '&&line[i]!='\n'&&line[i]!=',')
OPECODE[j++]=line[i++];
if(line[i]==',')
index=1;
OPECODE[j]='\0';
if(line[i]==' ')
{
while(line[i]==' ')
i++;
j=0;
k++;
while(line[i]!=' '&&line[i]!='\n'&&line[i]!=',')
OPERAND[j++]=line[i++];
OPERAND[j]='\0';
if(line[i]==',')
index=1;
}
count=k;
if(k==2)
{
strcpy(OPERAND,OPECODE);
strcpy(OPECODE,LABEL);
strset(LABEL,'\0');
}
}
int operand_value(char x[10])
{
int i=0,l=strlen(x),p=1;
while(l>0)
{
i+=(x[--l]-48)*p;
p*=10;
}
return i;
}
int SSEARCH(char x[])
{
int i=0;
for(i=0;i {
if(strcmpi(SYMTAB[i].name,x)==0)
return i;
}
return -1;
}
int OSEARCH(char x[])
{
int i=0;
for(i=0;i {
if(strcmpi(OPTAB[i].name,x)==0)
return i;
}
return -1;
}
void initial()
{
int i=0,j,k;
char c;
opcode=fopen("e:\\s6cs\\cs_32_34\\opcode.txt","r");
while(!feof(opcode))
{
strset(OPTAB[i].name,'\0');
strset(OPTAB[i].value,'\0');
c=getc(opcode);
j=0;
while(c!=32)
{
OPTAB[i].name[j++]=c;
c=getc(opcode);
if(feof(opcode))
break;
}
while(c==32)
c=getc(opcode);
j=0;
while(c!='\n')
{
OPTAB[i].value[j++]=c;
c=getc(opcode);
if(feof(opcode))
break;
}
i++;
osize++;
}
fclose(opcode);
}
char * dec_hex(int num)
{
int i=0,j=0,rem=0;
char * str;
char str1[10];
str=(char*)malloc(sizeof(str1));
while(num>0)
{
rem=num%16;
num=num/16;
if(rem<10)
str1[i++]=rem+48;
else if(rem==10)
str1[i++]='A';
else if(rem==11)
str1[i++]='B';
else if(rem==12)
str1[i++]='C';
else if(rem==13)
str1[i++]='D';
else if(rem==14)
str1[i++]='E';
else if(rem==15)
str1[i++]='F';
}
while(i<4)
str1[i++]='0';
str1[i]='\0';
while(i>0)
{
str[j++]=str1[--i];
}
str[j]='\0';
return str;
}
int hex_dec(char x[])
{
int i=0,l=strlen(x),p=1;
char c;
while(l>0)
{
c=x[--l];
if(c>64)
c-=55;
else
c-=48;
i+=c*p;
p*=16;
}
return i;
}
void INSERT()
{
char *t;
t=dec_hex(LOCCTR);
strcpy(SYMTAB[ssize].name,LABEL);
strcpy(SYMTAB[ssize++].value,t);
}
void writeinter(FILE *f)
{
strset(INTER.operand,'\0');
strset(INTER.opecode,'\0');
strset(INTER.label,'\0');
strcpy(INTER.operand,OPERAND);
strcpy(INTER.opecode,OPECODE);
strcpy(INTER.label,LABEL);
fwrite(&INTER,sizeof(INTER),1,f);
fputc('\n',f);
}
void pass1()
{
FILE *finter;
int sym=0,op=0;
finter=fopen("INTER.txt","w");
READ();
if(strcmpi(OPECODE,"START")==0)
{
LOCCTR=BEG=hex_dec(OPERAND);
strcpy(NAME,LABEL);
strcpy(START,OPERAND);
index=0;
writeinter(finter);
READ();
}
else LOCCTR=0;
while(strcmpi(OPECODE,"END")!=0)
{
if(strcmpi(LABEL,".")!=0)
{
if(count==3)
{
sym=SSEARCH(LABEL);
printf("DUPLICATE ERROR");
INSERT();
}
strcpy(INTER.loc,dec_hex(LOCCTR));
op=OSEARCH(OPECODE);
if(op>=0)
LOCCTR+=3;
else if(strcmpi(OPECODE,"WORD")==0)
LOCCTR+=3;
else if(strcmpi(OPECODE,"RESW")==0)
LOCCTR+=3*operand_value(OPERAND);
else if(strcmpi(OPECODE,"RESB")==0)
LOCCTR+=operand_value(OPERAND);
else if(strcmpi(OPECODE,"BYTE")==0)
LOCCTR+=strlen(OPERAND);
else
printf("INVALID OPECODE");
}
writeinter(finter);
READ();
}
writeinter(finter);
PGMLEN=LOCCTR-BEG;
fclose(finter);
}
void pass2()
{
FILE *finter,*fout;
int sym=0,op=0,size=0;
char addr[5],code[7],opf[15];
finter=fopen("INTER.txt","r");
printf("NAME OF OUTPUT FILE:- ");
gets(opf);
fout=fopen(opf,"w");
fread(&INTER,sizeof(INTER),1,finter);
fseek(finter,2,1);
if(strcmpi(INTER.opecode,"START")==0)
{
strcpy(HEADER.name,NAME);
strcpy(HEADER.start,START);
strcpy(HEADER.len,dec_hex(PGMLEN));
fread(&INTER,sizeof(INTER),1,finter);
fseek(finter,2,1);
}
fwrite(&HEADER,sizeof(HEADER),1,fout);
putc('\n',fout);
strcpy(TEXT.start,INTER.loc);
while(strcmpi(INTER.opecode,"END")!=0)
{
strset(code,'\0');
strset(addr,'\0');
op=OSEARCH(INTER.opecode);
if(op>0)
{
if(strlen(INTER.operand)>=1)
{
sym=SSEARCH(INTER.operand);
if(sym>=0)
{
strcpy(addr,SYMTAB[sym].value);
}
}
strcpy(code,OPTAB[op].value);
strcat(code,addr);
}
else if(strcmpi(INTER.opecode,"BYTE")==0||
strcmpi(INTER.opecode,"BYTE")==0);
if(size==3)
{
strcpy(TEXT.len,"0009");
fwrite(&TEXT,sizeof(TEXT),1,fout);
putc('\n',fout);
size=0;
strcpy(TEXT.start,INTER.loc);
strset(TEXT.code,'\0');
}
strcat(TEXT.code,code);
if(op>=0)
size++;
fread(&INTER,sizeof(INTER),1,finter);
fseek(finter,2,1);
}
size*=3;
strcpy(TEXT.len,dec_hex(size));
fwrite(&TEXT,sizeof(TEXT),1,fout);
putc('\n',fout);
strcpy(END.first,START);
fwrite(&END,sizeof(END),1,fout);
fclose(fout);
}
void main()
{
int sym,op;
char text[20],ip[15];
clrscr();
initial();
printf("NAME OF CODE FILE:- ");
gets(ip);
fp=fopen(ip,"r");
index=0;
pass1();
printf("\n\n\n YOUR PROGRAM %s ",ip);
printf("IS SUCCESSFULLY ASSEMBLED\n\n");
pass2();
fclose(fp);
}
INPUT FILES
PROGRAM.TXT
COPY START 0050
LDA ZERO
STA INDEX
ADDLP LDX INDEX
LDA ALPHA
ADD BETA
STA GAMMA
LDA INDEX
ADD THREE
STA INDEX
COMP K300
JLT ADDLP
INDEX RESW 1
ALPHA RESW 1
BETA RESW 1
GAMMA RESW 1
ZERO WORD 0
THREE WORD THREE
K300 WORD 300
END START
OPCODE.TXT
ADD 18
AND 40
COMP 20
DIV 24
JMP 3C
JEQ 30
JGT 34
JLT 38
JSUB 48
LDA 00
LDCH 50
LDL 08
LDX 04
MUL 20
OR 44
RD D8
RSUB 4C
STA 0C
STCH 54
STL 14
STX 10
SUB 1C
TIX 2C
WD DC
OUTPUT
NAME OF CODE FILE:- PROG.TXT
YOUR PROGRAM PROG.TXT IS SUCCESSFULLY ASSEMBLED
NAME OF OUTPUT FILE:- OUT.TXT
OUTPUT FILES
INTER.TXT
COPY START 0050
0050 LDA ZERO
0053 STA INDEX
0056 ADDLP LDX INDEX
0059 LDA ALPHA
005C ADD BETA
005F STA GAMMA
0062 LDA INDEX
0065 ADD THREE
0068 STA INDEX
006B COMP K300
006E JLT ADDLP
0071 INDEX RESW 1
0074 ALPHA RESW 1
0077 BETA RESW 1
007A GAMMA RESW 1
007D ZERO WORD 0
0080 THREE WORD THREE
0083 K300 WORD 300
0083 END START
OUT.TXT
HCOPY 0050 0036
T00007D0C0071040071 0050 0009
T0000740C007A 0059 0009
T0000710C0071 0062 0009
T200083380056 006B 0006
E0050
LEXICAL ANALYSER
#include
#include
#include
FILE *ptr1,*ptr2,*ptr3;
char line[50],kword[10][20],op[10][10],cons[10][10],iden[10][10],word[20],others[10][10];
int i,j,k,kw=0,ow=0,iw=0,cw=0,opw=0,flag1=0;
int opcheck()
{
char c; int v=0;
ptr3=fopen("opfile1.txt","r");
while(c!='$')
{
c=fgetc(ptr3);
if(line[i]==c)
{
v=1;
break;
}
}
fclose(ptr3);
return (v);
}
void check()
{
int lim=0;
int flag=0,num,check1,check2;
char key[10];
flag1=0;
ptr2=fopen("keyfile.txt","r");
while(!feof(ptr2))
{
fscanf(ptr2,"%s",key);
if(strcmp(word,key)==0)
{
flag=1;
check2=0;
for(num=0;num {
check1=strcmp(kword[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
strcpy(kword[kw++],word);
break;
}
}
fclose(ptr2);
if(flag==0)
{
for(k=0;word[k]!='\0';k++)
if (!isdigit(word[k]))
flag1=1;
if(flag1==0)
{
check2=0;
for(num=0;num {
check1=strcmp(cons[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
strcpy(cons[cw++],word);
}
else
if(!isdigit(word[0]))
{
check2=0;
for(num=0;num {
check1=strcmp(iden[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
strcpy(iden[iw++],word);
}
else
printf("invalid token%s",word);
}
}
int opcheck1()
{
char c[10];
int v=0;
ptr3=fopen("opfile.txt","r");
while(!feof(ptr3))
{
fgets(c,3,ptr3);
if(strcmp(word,c)==0)
{
v=1;
break;
}}
fclose(ptr3);
return (v);
}
void tokenise()
{
int flag=0,num,check1,check2;
i=0;
while(line[i]!='\n')
{
j=0;
while(line[i]==' ')
i++;
while(isalnum(line[i]))
word[j++]=line[i++];
word[j]='\0';
if (strcmp(word,"\0")!=0)
{
check();
strcpy(word,"\0");
continue;
}
j=0;
while (line[i]==' ')
i++;
while(opcheck()==1)
word[j++]=line[i++];
word[j]='\0';
if( strcmp(word,"\0")!=0)
{
if(word[i]!='\0')
if(opcheck1()!=1)
flag=1;
if(flag==0)
{
check2=0;
for(num=0;num {
check1=strcmp(op[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
strcpy(op[opw++],word);
}
strcpy(word,"\0");
continue;
}
j=0;
while (line[i]==' ')
i++;
while(ispunct(line[i]))
word[j++]=line[i++];
word[j]='\0';
if (strcmp(word,"\0")!=0)
{
check2=0;
for(num=0;num {
check1=strcmp(others[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
{
strcpy(others[ow++],word);
strcpy(word,"\0");
continue;
}
}
}
}
void main()
{
int j;
clrscr();
ptr1=fopen("program.txt","r");
fgets(line,50,ptr1);
while(!feof(ptr1))
{
tokenise();
fgets(line,50,ptr1);
}
fclose(ptr1);
printf(" \n\nKEYWORDS\n\n");
for(i=0;i printf(" %s ",kword[i]);
printf(" \n\nIDENTIFIERS\n\n");
for(i=0;i printf(" %s ",iden[i]);
printf(" \n\nCONSTANTS\n\n");
for(i=0;i printf(" %s ",cons[i]);
printf(" \n\nOPERATORS\n\n");
for(i=0;i printf(" %s ",op[i]);
printf(" \n\nOTHERS\n\n");
for(i=0;i printf(" %s ",others[i]);
getch();
}
INPUT FILES
KEYFILE.TXT
main ,
if ,
goto ,
continue ,
else ,
do ,
for ,
float ,
case ,
break ,
void ,
int ,
char ,
while ,
printf ,
scanf ,
switch ;
OPFILE.TXT
!= ,
++ ,
-- ,
+ ,
- ,
* ,
/ ,
== ,
<= ,
>= ,
&& ,
|| ,
< ,
> ;
OPFILE1.TXT
+-*/<>=%$&(){}
PROGRAM.TXT
void main( )
{
int i,a=10;
int b=2;
char c[15] ;
if(a<=15)
{
a=a-1;
a=a+2;
}
b=b*3;
b=b/4;
for(i=0;i<=10;i++)
{
printf( "%d" ,a) ;
}
}
OUTPUT
KEYWORDS
void main int char if for printf
IDENTIFIERS
i a b c d
CONSTANTS
10 2 15 1 3 4 0
OPERATORS
= <= - + * / ++
OTHERS
( ) { , ; [ ] } "% "
ABSOLUTE LOADER
#include
#include
#include
#include
#include
#include
void main()
{
FILE *objfile,*ofile;
char line[80],locctr[7],number[7],value[7],t[7]="\0",obj[10];
char *temp="\0";
int i,j,k,len,a[6],l,m;
long loc=0,num=0,x;
clrscr();
printf("\n\t*******************************************************\n");
printf("\n\t\t\tABSOLUTE LOADER");
printf("\n\t*******************************************************\n");
printf("\n\nEnter the name of object file:\t");
gets(obj);
//objfile = fopen(obj,"r");
objfile = fopen("st.txt","r");
fgets(line,80,objfile);
ofile = fopen("memory.txt","w");
for(i=2;line[i] != '^';i++)
i++;
for(j=0;line[i] != '^';j++,i++)
locctr[j] = line[i];
locctr[j] = '\0';
len=strlen(locctr);
for(k=0;k {
if(locctr[k]>57)
a[k] = locctr[k] - 55;
else
a[k] = locctr[k] - 48;
}
for(k=len-1;k>=0;k--)
loc += a[k] * pow(16,len-1-k);
i++;
for(j=0;line[i] != '\0';j++,i++);
fgets(line,80,objfile);
while(line[0] != 'E')
{
for(j=0,i=2;line[i] != '^'&&line[i] != '\n';i++,j++)
number[j] = line[i];
number[j] = '\0';
len=strlen(number);
for(k=0;k {
if(number[k]>57)
a[k] = number[k] - 55;
else
a[k] = number[k] - 48;
}
for(k=len-1;k>=0;k--)
num += a[k] * pow(16,len-1-k);
while((num - loc) > 0)
{
k=0;l=0;
t[0] = '\0';
temp = t;
x = loc;
while(x>0)
{
a[l] = x%16;
l++;
x /= 16;
}
l--;
while(k<(5-1))
{
*temp= '0';
temp++;
k++;
}
for(m=1;m>=0;m--)
{
if(a[m]>9)
*temp = a[m] + 55;
else
*temp = a[m] + 48;
temp++;
}
*temp= '\0';
fputs(t,ofile);
fputs(" **",ofile);
fputs("\n",ofile);
loc++;
}
i = 12;
while(line[i] != '\0' && line[i] != '\n')
{
for(j=0;j<2;i++)
{
if(line[i] != '^')
{
value[j] = line[i];
j++;
}
}
value[j] = '\0';
k=0;
l=0;
t[0] = '\0';
temp = t;
x = loc;
while(x>0)
{
a[l] = x%16;
l++;
x /= 16;
}
l--;
while(k<(5-1))
{
*temp = '0';
temp++;
k++;
}
for(m=l;m>=0;m--)
{
if(a[m]>9)
*temp= a[m] + 55;
else
*temp= a[m] + 48;
temp++;
}
*temp= '\0';
fputs(t,ofile);
fputs(" ",ofile);
fputs(value,ofile);
fputs("\n",ofile);
loc++;
}
fgets(line,80,objfile);
}
printf("\n\n\n\t\t");
textcolor(YELLOW+BLINK);
cprintf("The object file is loaded into the memory");
getch();
fclose(objfile);
fclose(ofile);
}
INPUT FILE
OBJCODE.TXT
H^MERGE^000064^000020
T^000066^1E^180065^34006F^180065^280064^200064^DC007E^280065^38006F
180064^C90064
E^000064
OUTPUT
*******************************************************
ABSOLUTE LOADER
*******************************************************
Enter the name of object file: OBJCODE.TXT
The object file is loaded into the memory
OUTPUT FILE
MEMORY.TXT
000000 **
000001 **
000002 **
000003 **
000004 **
000005 **
000006 **
000007 **
000008 **
000009 **
00000A **
00000B **
00000C **
00000D **
00000E **
00000F **
000010 **
000011 **
000012 **
000013 **
000014 **
000015 **
000016 **
000017 **
000018 **
000019 **
00001A **
00001B **
00001C **
00001D **
00001E **
00001F **
000020 **
000021 **
000022 **
000023 **
000024 **
000025 **
000026 **
000027 **
000028 **
000029 **
00002A **
00002B **
00002C **
00002D **
00002E **
00002F **
000030 **
000031 **
000032 **
000033 **
000034 **
000035 **
000036 **
000037 **
000038 **
000039 **
00003A **
00003B **
00003C **
00003D **
00003E **
00003F **
000040 **
000041 **
000042 **
000043 **
000044 **
000045 **
000046 **
000047 **
000048 **
000049 **
00004A **
00004B **
00004C **
00004D **
00004E **
00004F **
000050 **
000051 **
000052 **
000053 **
000054 **
000055 **
000056 **
000057 **
000058 **
000059 **
00005A **
00005B **
00005C **
00005D **
00005E **
00005F **
000060 **
000061 **
000062 **
000063 **
000064 **
000065 **
000066 18
000067 00
000068 65
000069 34
00006A 00
00006B 6F
00006C 18
00006D 00
00006E 65
00006F 28
000070 00
000071 64
000072 20
000073 00
000074 64
000075 DC
000076 00
000077 7E
000078 28
000079 00
00007A 65
00007B 38
00007C 00
00007D 6F
00007E **
00007F **
000080 **
000081 **
000082 **
000083 **
000084 **
000085 **
000086 **
000087 **
000088 **
000089 **
00008A **
00008B **
00008C **
00008D **
00008E **
00008F **
000090 **
000091 **
000092 **
000093 **
000094 **
000095 **
000096 **
000097 **
000098 **
000099 **
00009A **
00009B **
00009C **
00009D **
00009E **
00009F **
0000A0 **
0000A1 **
0000A2 **
0000A3 **
0000A4 **
0000A5 **
0000A6 **
0000A7 **
0000A8 **
0000A9 **
0000AA **
0000AB **
0000AC **
0000AD **
0000AE **
0000AF **
0000B0 **
0000B1 **
0000B2 **
0000B3 **
0000B4 **
0000B5 **
0000B6 **
0000B7 **
0000B8 **
0000B9 **
0000BA **
0000BB **
0000BC **
0000BD **
0000BE **
0000BF **
0000C0 **
0000C1 **
0000C2 **
0000C3 **
0000C4 **
0000C5 **
0000C6 **
0000C7 **
0000C8 **
0000C9 **
0000CA 4
MACRO PROCESSOR
#include
#include
#include
#include
#include
#include
FILE *fp,*fo;
int expand=0;
struct line
{
char lab[20];
char opc[20];
char opr[25];
}DEFTAB[30];
int idef=0;
struct nametab
{
char name[20];
int st;
int end;
int ast;
int aend;
}NAMETAB[10],NAM;
int nam=0;
char ARGPOS[5][10];
int arg=0;
struct arg
{
char name[25];
char pos[25];
}ARGTAB[10];
struct line GETLINE();
void PROCESSLINE(struct line LINE);
void replace(char src[25],char pat[15],char newpat[15])
{
char *w,t[25];
int i=0,j,k;
w=strstr(src,pat);
strcpy(t,w);
if(w!='\0')
{
*w='\0';
strcat (src,newpat);
}
while(pat[i]==t[i])
i++;
j=strlen(src);
while(t[i-1]!='\0')
src[j++]=t[i++];
strcpy(t,src);
strset(src,'\0');
for( k=0;k<=5;k++)
src[i]='\0';
strcpy(src,t);
}
struct line GETLINE()
{
struct line LINE={"\0","\0","\0"};
char lin[40]={"\0"};
int i=1,j;
char c=getc(fp);
lin[0]=c;
while(lin[i-1]!='\n')
{
lin[i++]=getc(fp);
if(feof(fp))
break;
}
lin[i-1]='\n';
i=0;
while(lin[i++]==' ');
i--;
j=0;
while(lin[i]!=' ' && lin[i]!='\n')
LINE.lab[j++]=lin[i++];
while(lin[i++]==' ');
i--;
j=0;
while(lin[i]!=' ' && lin[i]!='\n')
LINE.opc[j++]=lin[i++];
while(lin[i++]==' ');
i--;
j=0;
while(lin[i]!=' ' && lin[i]!='\n')
LINE.opr[j++]=lin[i++];
if(strcmp(LINE.opr,"\0")==0)
{
strset(LINE.opr,'\0');
strcpy(LINE.opr,LINE.opc);
strset(LINE.opc,'\0');
strcpy(LINE.opc,LINE.lab);
strset(LINE.lab,'\0');
}
return LINE;
}
int NAM_SEARCH(char x[])
{
int i;
for (i=0;i if (strcmp(NAMETAB[i].name,x)==0)
{
NAM=NAMETAB[i];
return i;
}
return(-1);
}
void EXPAND(struct nametab nam, struct line LINE)
{
char x[20];
int rdef=nam.st;
int i=0,j=0,k;
expand=1;
arg=nam.ast;
strcpy(x,LINE.lab);
putc('.',fo);
fwrite(&LINE,sizeof(LINE),1,fo);
putc('\n',fo);
while(LINE.opr[i]!='\0')
{
strset(ARGTAB[arg].name,'\0');
while(LINE.opr[i]!=','&&LINE.opr[i]!='\0')
ARGTAB[arg].name[j++]=LINE.opr[i++];
j=0;
arg++;
i++;
}
LINE=DEFTAB[rdef++];
while(strcmp(LINE.opc,"MEND")!=0)
{
LINE=DEFTAB[rdef++];
if(rdef==nam.st+2)
strcpy(LINE.lab,x);
for(k=nam.ast;k replace(LINE.opr,ARGTAB[k].pos,ARGTAB[k].name);
if(strcmp(LINE.opc,"MEND")!=0)
PROCESSLINE(LINE);
}
expand=0;
}
void DEFINE(struct line LINE)
{
int i=0,j=0,k;
int level=1;
strcpy(NAMETAB[nam].name,LINE.lab);
NAMETAB[nam].st=idef;
NAMETAB[nam].ast=arg;
while(LINE.opr[i]!='\0')
{
while(LINE.opr[i]!=','&& LINE.opr[i]!='\0')
ARGTAB[arg].name[j++]=LINE.opr[i++];
j=0;
arg++;
i++;
}
for (k=NAMETAB[nam].ast;k {
ARGTAB[k].pos[0]='?';
ARGTAB[k].pos[2]='\0';
ARGTAB[k].pos[1]=toascii(k+1+48);
}
DEFTAB[idef++]=LINE;
while(level>0)
{
LINE=GETLINE();
for(k=NAMETAB[nam].ast;k replace(LINE.opr,ARGTAB[k].name,ARGTAB[k].pos);
DEFTAB[idef++]=LINE;
if(strcmp(LINE.opc,"MACRO")==0)
level++;
else if (strcmp(LINE.opc,"MEND")==0)
level--;
}
NAMETAB[nam].end=idef;
NAMETAB[nam++].aend=arg;
}
void PROCESSLINE(struct line LIN)
{
int x;
struct line LINE={"\0","\0","\0"};
strcpy(LINE.lab,LIN.lab);
strcpy(LINE.opc,LIN.opc);
strcpy(LINE.opr,LIN.opr);
x=NAM_SEARCH(LINE.opc);
if (x>=0)
EXPAND(NAM,LINE);
else if(strcmp(LINE.opc,"MACRO")==0)
DEFINE(LINE);
else
{
fwrite(&LINE,sizeof(LINE),1,fo);
putc('\n',fo);
}
}
void main()
{
struct line LINE={"\0","\0","\0"};
char ip[20],op[20];
clrscr();
printf("\n\t*******************************************************\n");
printf("\n\t\t\tMACRO PROCESSOR");
printf("\n\t*******************************************************\n");
printf("\n\tENTER NAME OF INPUT FILE: ");
gets(ip);
printf("\n\tENTER NAME OF OUTPUT FILE: ");
gets(op);
fp=fopen(ip,"r");
fo=fopen(op,"w");
expand=0;
while(strcmp(LINE.opc,"END")!=0)
{
LINE=GETLINE();
PROCESSLINE(LINE);
}
}
INPUT FILE
MACRO.TXT
COPY START 1000
READ MACRO &INDEV,&BUFADR,&RECLTH
CLEAR X
CLEAR A
CLEAR S
+LDT #4096
TD =X'&INDEV'
JEQ *-3
RD =X'&INDEV'
COMPR A,S
JEQ *+11
STCH &BUFADR,X
TIXR T
JLT *-19
STX &RECLTH
MEND
WRITE MACRO &OUTDEV,&BUFADR,&RECLTH
CLEAR X
LDT &RECLTH
LDCH &BUFADR,X
TD =X'&OUTDEV'
JEQ *-3
WD =X'&OUTDEV'
TIXR T
JLT *-14
MEND
FIRST STL RETADR
CLOP READ F1,BUFER,LENGTH
LDA LENGTH
COMP #0
JEQ ENDFIL
WRITE 05,BUFFER,LENGTH
J CLOOP
ENDFIL WRITE 05,EOF,THREE
J @RETADR
EOF BYTE C'EOF'
THREE WORD 3
RETADR RESW 1
LENGTH RESW 1
BUFFER RESB 4096
END FIRST
OUTPUT
*******************************************************
MACRO PROCESSOR
*******************************************************
ENTER NAME OF INPUT FILE: MACRO.TXT
ENTER NAME OF OUTPUT FILE: MOUT.TXT
OUTPUT FILE
MOUT.TXT
COPY START 1000
FIRST STL RETADR
.CLOP READ F1,BUFER,LENGTH
CLOP CLEAR X
CLEAR A
CLEAR S
+LDT #4096
TD =X'F1'
JEQ *-3
RD =X'F1'
COMPR A,S
JEQ *+11
STCH BUFER,X
TIXR T
JLT *-19
STX LENGTH
LDA LENGTH
COMP #0
JEQ ENDFIL
. WRITE 05,BUFFER,LENGTH
CLEAR X
LDT LENGTH
LDCH BUFFER,X
TD =X'05'
JEQ *-3
WD =X'05'
TIXR T
JLT *-14
J CLOOP
.ENDFIL WRITE 05,EOF,THREE
ENDFIL CLEAR X
LDT THREE
LDCH EOF,X
TD =X'05'
JEQ *-3
WD =X'05'
TIXR T
JLT *-14
J @RETADR
EOF BYTE C'EOF'
THREE WORD 3
RETADR RESW 1
LENGTH RESW 1
BUFFER RESB 4096
END FIRST
NFA TO DFA CONVERSION
#include
#include
int total;
typedef struct
{
int current,next;
char input;
} transition;
transition nfa[20],dfa[20];
typedef struct
{
int eps[20];
int n,flag;
} closure;
closure e_closure(closure x)
{
int stack[20],top,a,i,j;
closure y;
for(top=0;top stack[top] = x.eps[top];
top--;
y = x;
while(top != -1)
{
a = stack[top--];
for(i=0;i {
if(nfa[i].current == a && nfa[i].input == 'E')
{
for(j=0;j {
if(nfa[i].next == y.eps[j])
break;
}
if(j == y.n)
{
stack[++top] = nfa[i].next;
y.eps[y.n] = nfa[i].next;
(y.n)++;
}
}
}
}
return y;
}
int checking(closure x,closure y)
{
int flag = 1,i;
if(x.n != y.n)
flag = 0;
for(i=0;flag && i {
if(x.eps[i]!=y.eps[i])
flag = 0;
}
return flag;
}
void main()
{
int i,j=0,check=1,k,l,m,z=0,ipn,o,final;
char input[10],x;
closure a[20],temp;
clrscr();
printf("Enter the number of transitions : ");
scanf("%d",&total);
printf("Enter the number of input symbols (without E) : ");
scanf("%d",&ipn);
printf("Enter the input symbols (except E) : ");
for(i=0;i {
scanf("%c",&x);
scanf("%c",&input[i]);
}
printf("Enter the transition table\n");
for(i=0;i {
printf("Current state : ");
scanf("%d",&nfa[i].current);
printf("Input symbol : ");
scanf("%c",&x);
scanf("%c",&nfa[i].input);
printf("Next state : ");
scanf("%d",&nfa[i].next);
}
printf("Enter the final state: ");
scanf("%d",&final);
temp.eps[0] = nfa[0].current;
temp.n = 1;
a[j] = e_closure(temp);
a[j].flag = 0;
j++;
k = 0;
while(check)
{
a[k].flag = 1;
for(i=0;i {
temp.n = 0;
for(m=0;m {
for(l=0;l {
if(nfa[l].current == a[k].eps[m] && nfa[l].input == input[i])
{
temp.eps[temp.n] = nfa[l].next;
(temp.n)++;
}
}
}
temp = e_closure(temp);
for(m=0;m if(checking(temp,a[m]))
break;
if(m == j)
{
temp.flag = 0;
a[j++] = temp;
}
for(l=0;l {
if(dfa[l].current == k && dfa[l].input == input[i])
break;
}
if(l == z)
{
dfa[z].current = k;
dfa[z].input = input[i];
dfa[z].next = m;
z++;
}
}
for(k=0;k if(a[k].flag == 0)
break;
if(k == j)
check = 0;
}
clrscr();
printf("DFA transition : \n");
printf("Current\tinput\tnext\n");
for(i=0;i {
printf("%d\t",dfa[i].current);
printf("%c\t",dfa[i].input);
printf("%d\n",dfa[i].next);
}
getch();
}
OUTPUT
Enter the number of transitions : 3
Enter the number of input symbols (without E) : 2
Enter the input symbols (except E) : a b
Enter the transition table
Current state :1
Input symbol : a
Next state : 1
Current state : 1
Input symbol : a
Next state : 2
Current state : 1
Input symbol : b
Next state : 2
Enter the final state: 2
DFA transition :
Current input next
0 a 1
0 b 2
1 a 1
1 b 2
2 a 3
2 b 3
3 a 3
3 b 3
OPERATOR PRECEDENCE PARSING
#include
#include
#include
#include
int i,k,w,e,q,length,rhslength,len,plength,row;
int column,i,j,u,valid,v,y,begin,top,z;
char aa,bb,temp,term[10],rhs[10],token[25],stack[20],nt,handle[10],tm;
char table[10][10]={
{' ','+','-','*','/','^','i','(',')','$'},
{'+','>','>','<','<','<','<','<','>','>'},
{'-','>','>','<','<','<','<','<','>','>'},
{'*','>','>','>','>','<','<','<','>','>'},
{'/','>','>','>','>','<','<','<','>','>'},
{'^','>','>','>','>','<','<','<','>','>'},
{'i','>','>','>','>','>',' ',' ','>','>'},
{'(','<','<','<','<','<','<','<','=',' '},
{')','>','>','>','>','>',' ',' ','>','>'},
{'$','<','<','<','<','<','<','<','>','>'}
};
char production[9][10]={
{'E','-','>','E','A','E','\0'},
{'E','-','>','(','E',')','\0'},
{'E','-','>','-','E','\0'},
{'E','-','>','i','\0'},
{'E','-','>','+','\0'},
{'E','-','>','-','\0'},
{'E','-','>','*','\0'},
{'E','-','>','/','\0'}
};
void find_row_for_a(char a)
{
for(j=0;j<10;j++)
{
if(table[j][0]==a)
break;
}
row=j;
if(j==10)
{
printf("string is not accepted\n");
exit(0);
}
}
void find_column_for_b(char b)
{
for(j=0;j<10;j++)
{
if(table[0][j]==b)
break;
}
column=j;
if(j==10)
{
printf("string is not accepted\n");
exit(0);
}
}
void push(char info)
{
top++;
stack[top]=info;
for(k=0;k token[k]=token[k+1];
length--;
token[length]='\0';
}
void get_rhs()
{
e=0;
plength=strlen(production[y]);
nt=production[i][0];
while(production[y][e]!='>')
e++;
e++;
q=0;
while(e {
rhs[q]=production[y][e];
e++;
q++;
}
if((rhs[1]=='A')&&(handle[1]=='+')||(handle[1]=='*')||(handle[1]=='-')||(handle[1]=='^')||(handle[1]=='/'))
rhs[1]=handle[1];
rhs[q]='\0';
rhslength=strlen(rhs);
}
void reduce()
{
for(y=0;y<9;y++)
{
get_rhs();
if(strcmp(rhs,handle)==0)
{
top++;
stack[top]=nt;
break;
}
}
if(y==9)
{
printf("string is not accepted\n");
exit(0);
}
stack[top+1]='\0';
}
void check_for_precedence()
{
aa=stack[top];
z=top;
while(aa=='E'||aa=='A')
{
z--;
aa=stack[z];
}
bb=token[i];
find_row_for_a(aa);
find_column_for_b(bb);
if(table[row][column]=='<'||table[row][column]=='=')
push(bb);
else if(table[row][column]=='>')
{
u=0;
while(top>=0)
{
if (stack[top]=='E'||stack[top]=='A')
{
while (stack[top]=='E'||stack[top]=='A')
{
term[u]=stack[top];
top--;
u++;
}}
term[u]=stack[top];
tm=stack[top];
top--;
while (stack[top]=='E'||stack[top]=='A')
{
u++;
term[u]=stack[top];
top--;
}
temp=stack[top];
find_row_for_a(temp);
find_column_for_b(tm);
if(table[row][column]=='<')
{
valid=1;
break;
}
u++;
}
if(!valid)
{
printf("String is not accepted\n");
exit(0);
}
len=u+1;
v=len-1;
for(w=0;w {
handle[w]=term[v];
v--;
}
handle[len]='\0';
reduce();
}
else
{
printf("String is not accepted\n");
exit(0);
}
}
void check()
{
while(length>=1)
{
check_for_precedence();
begin=1;
if((stack[0]=='$')&&(stack[1]=='E')&&(stack[2]=='\0')&&(token[0]=='$')&&(token[1]=='\0'))
{
printf("String is accepted\n");
exit(0);
}
if(stack[0]=='$'&&stack[1]!='E'&&stack[2]=='\0'&&token[0]=='$'&&token[1]=='\0')
{
printf("String is not accepted\n");
exit(0);
}
}}
main()
{
clrscr();
printf("\t\tOPERATOR PRECEDENCE FOR THE GRAMMAR\n\n\n\n");
printf("\t\tE->EAE|i\n\n\n");
printf("\t\tA->+|-|*|/+^\n\n\n");
printf("\n\tEnter the string(Add $ to mark end of string)\n");
scanf("%s",&token);
i=0;
length=strlen(token);
begin=0;
top=0;
stack[top]='$';
if((stack[top]=='$')&&(strcmp(token,"$")==0))
{
printf("String is accepted\n");
exit(0);
}
check();
return(0);
getch();
}
OUTPUT
OPERATOR PRECEDENCE FOR THE GRAMMAR
E->EAE|i
A->+|-|*|/+^
Enter the string(Add $ to mark end of string) i+i*i^i$
String is accepted
FIRST AND FOLLOW
#include
#include
#include
#include
char first[10][10],line[20],c[2],d[10],e[10],nt[10],follow[10][10];
FILE *pt1;
int f=0,l=0;
void setfirst()
{
do{
int i=0,j=0;
nt[f]=line[i++];
if(line[i]!='-')
nt[++f]=line[i++];
while(line[i]!='>')
i++;
i++;
first[f][j++]=line[i++];
while((line[i]!='\n')&&(line[i]!='/'))
i++;
while(line[i]!='\n')
{
i++;
first[f][j++]=line[i++];
if((line[i]=='d')&&(line[i]!='\n'))
first[f][j++]=line[i++];
}
f++;
fgets(line,20,pt1);
}
while(!feof(pt1));
fclose(pt1);
pt1=fopen("prod.txt","r");
}
void searchfirst(char v,int q,int y)
{
int i,j,k;
for(i=0;i if(v==nt[i])
{
if(q==1)
i=i+2;
if(y==0)
strcpy(e,first[i]);
else
strcpy(e,follow[i]);
break;
}
}
int search(char v,int q)
{
int i;
for(i=0;i if(v==nt[i])
{
if(q==1)
i=i+2;
return i;
}
return(-1);
}
void rule1()
{
int k=3,j,c,s=0,p,i;
char b,B;
if(line[1]!='-')
k=4;
if(line[k]=='(')
{
k++;
c=search(line[k],0);
for(s=0;follow[c][s]!='\0';s++)
follow[c][++s]=line[k+1];
follow[c][s]='\0';
}
else
{
if(!isalnum(line[k]))
k++;
if(!isalnum(line[k+1]))
k=k+2;
b=line[k+1];
if(!isalnum(line[k+2]))
s=1;
c=search(line[k],0);
searchfirst(b,s,0);
for(s=0;follow[c][s]!='\0';s++)
for(j=0;j<4;j++)
if(e[j]!='e')
{
p=0;
for(i=0;iif(follow[c][i]==e[j])
p=1;
if(p==0)
follow[c][++s]=e[j];
}
follow[c][s]='\0';
}
}
void rule2()
{
char a,b;
int s=0,j=3,p=0,c,k;
a=line[0];
if(line[1]!='-')
{
s=1;j=4;
}
while((line[j]!='\n')&&(line[j]!='/'))
{
if(!isalnum(line[j]))
j++;
else
{
b=line[j];
if(!isalnum(line[j+1]))
p=1;
c=search(b,p);
if((a==b)&&(s==p))
{
j++;
break;
}
for(k=0;follow[c][k]!='\0';k++)
follow[c][k++]=a;
if(s==1)
follow[c][k++]=')';
follow[c][k++]='\0';
j++;
}
}
}
void setfollow()
{
int i;
for(i=0;i<10;i++)
follow[i][0]='$';
do
{
if (l==0)
{
follow[0][0]='$';
follow[0][1]='\0';
l=0;
}
rule1();
if(line[0]!='F')
rule2();
fgets(line,25,pt1);
}
while(!feof(pt1));
}
void displayfirst()
{
int i,j,k,p;
for(i=f-1;i>=0;i--)
{
p=k=0;
if(!isalnum(nt[i]))
p=1;
if(p==1)
printf("FIRST(%c%c)=\t",nt[i-1],nt[i]);
else
printf("FIRST(%c)=\t",nt[i]);
for(j=0;j<4;j++)
{
if(first[i][j]=='i')
printf("%c\t",first[i][j++]);
if(!isupper(first[i][j]))
printf("%c\t",first[i][j]);
else
{
if(first[i][j+1]=='\'')
k=1;
searchfirst(first[i][j],k,0);
strcpy(first[i],e);
for(k=0;k<4;k++)
if(first[i][k]=='i')
printf("%c\t",first[i][k]);
else
printf("%c\t",first[i][k]);
break;
}
}
if(p==1)
i--;
printf("\n");
}
}
void displayfollow()
{
int i,j,k,p;
strcpy(follow[2],follow[0]);
strcpy(follow[5],follow[3]);
strcat(follow[6],"+");
for(i=0;i {
p=k=0;
if((!isalnum(nt[i+1]))&&((i+1)!=f))
p=1;
if(p==1)
{
printf("FOLLOW(%c%c)=\t",nt[i],nt[i+1]);
i++;
}
else
printf("FOLLOW(%c)=\t",nt[i]);
for(j=0;j<4;j++)
{
if(follow[i][j]=='i')
printf("%c\t",follow[i][j++]);
if(!isupper(follow[i][j]))
printf("%c\t",follow[i][j]);
else
{
d[0]=follow[i][j-1];
d[1]='\0';
if(follow[i][j+1]==')')
k=1;
searchfirst(follow[i][j],k,1);
strcat (d,e);
for(k=0;k<4;k++)
printf("%c",e[k]);
strcpy(follow[i],d);
break;
}
}
printf("\n");
}
}
void main()
{
int i,j;
pt1=fopen("prod.txt","r");
clrscr();
fgets(line,20,pt1);
while(!feof(pt1))
{
printf("\t\tFIRST AND FOLLOW\n");
printf("\t\t******************\n\n\n\n");
printf("\t\tSEE THE GRAMMAR IN PROD.TXT\n\n");
printf("\t\tFIRST\n\n");
setfirst();
displayfirst();
fgets(line,25,pt1);
setfollow();
printf("\n\n");
printf("\t\tFOLLOW\n\n");
setfollow();
displayfollow();
fgets(line,25,pt1);
getch();
}
}
TEXT FILES
PROD.TXT
E->TE'
E'->+TE'/e
T->FT'
T'->*FT'/e
F->(E)/i
OUTPUT
FIRST AND FOLLOW
******************
SEE THE GRAMMAR IN PROD.TXT
FIRST
FIRST(F)= ( i
FIRST(T')= * e
FIRST(T)= ( i
FIRST(E')= + e
FIRST(E)= ( i
FOLLOW
FOLLOW(E)= $ )
FOLLOW(E')= $ )
FOLLOW(T)= $ + )
FOLLOW(T')= $ + )
FOLLOW(F)= $ * ) +
#include
#include
#include
#include
#include
char line[30],LABEL[10],OPECODE[10],OPERAND[10];
char NAME[7],START[7];
int index=0,count,LOCCTR,BEG,PGMLEN;
FILE *fp,*opcode;
struct symtab
{
char name[10];
char value[10];
}SYMTAB[20];
struct optab
{
char name[10];
char value[10];
}OPTAB[20];
int ssize=0,osize=0;
struct inter
{
char loc[5];
char label[10];
char opecode[10];
char operand[10];
}INTER;
struct header
{
char t;
char name[6];
char start[6];
char len[6];
}HEADER={'H'};
struct text
{
char t;
char code[19];
char start[6];
char len[6];
}TEXT={'T'};
struct end
{
char t;
char first[6];
}END={'E'};
void READ()
{
int i=0,j=0,k=0;
strset(line,'\0');
index=0;
do
{ line[i++]=getc(fp);
if(feof(fp))
{
line[i-1]='\n';
break;
}
}
while (line[i-1]!='\n');
i=0;
while (line[i]==' ')
i++;
k++;
while (line[i]!=' ')
LABEL[j++]=line[i++];
LABEL[j]='\0';
while(line[i]==' ')
i++;
j=0;
k++;
while(line[i]!=' '&&line[i]!='\n'&&line[i]!=',')
OPECODE[j++]=line[i++];
if(line[i]==',')
index=1;
OPECODE[j]='\0';
if(line[i]==' ')
{
while(line[i]==' ')
i++;
j=0;
k++;
while(line[i]!=' '&&line[i]!='\n'&&line[i]!=',')
OPERAND[j++]=line[i++];
OPERAND[j]='\0';
if(line[i]==',')
index=1;
}
count=k;
if(k==2)
{
strcpy(OPERAND,OPECODE);
strcpy(OPECODE,LABEL);
strset(LABEL,'\0');
}
}
int operand_value(char x[10])
{
int i=0,l=strlen(x),p=1;
while(l>0)
{
i+=(x[--l]-48)*p;
p*=10;
}
return i;
}
int SSEARCH(char x[])
{
int i=0;
for(i=0;i
if(strcmpi(SYMTAB[i].name,x)==0)
return i;
}
return -1;
}
int OSEARCH(char x[])
{
int i=0;
for(i=0;i
if(strcmpi(OPTAB[i].name,x)==0)
return i;
}
return -1;
}
void initial()
{
int i=0,j,k;
char c;
opcode=fopen("e:\\s6cs\\cs_32_34\\opcode.txt","r");
while(!feof(opcode))
{
strset(OPTAB[i].name,'\0');
strset(OPTAB[i].value,'\0');
c=getc(opcode);
j=0;
while(c!=32)
{
OPTAB[i].name[j++]=c;
c=getc(opcode);
if(feof(opcode))
break;
}
while(c==32)
c=getc(opcode);
j=0;
while(c!='\n')
{
OPTAB[i].value[j++]=c;
c=getc(opcode);
if(feof(opcode))
break;
}
i++;
osize++;
}
fclose(opcode);
}
char * dec_hex(int num)
{
int i=0,j=0,rem=0;
char * str;
char str1[10];
str=(char*)malloc(sizeof(str1));
while(num>0)
{
rem=num%16;
num=num/16;
if(rem<10)
str1[i++]=rem+48;
else if(rem==10)
str1[i++]='A';
else if(rem==11)
str1[i++]='B';
else if(rem==12)
str1[i++]='C';
else if(rem==13)
str1[i++]='D';
else if(rem==14)
str1[i++]='E';
else if(rem==15)
str1[i++]='F';
}
while(i<4)
str1[i++]='0';
str1[i]='\0';
while(i>0)
{
str[j++]=str1[--i];
}
str[j]='\0';
return str;
}
int hex_dec(char x[])
{
int i=0,l=strlen(x),p=1;
char c;
while(l>0)
{
c=x[--l];
if(c>64)
c-=55;
else
c-=48;
i+=c*p;
p*=16;
}
return i;
}
void INSERT()
{
char *t;
t=dec_hex(LOCCTR);
strcpy(SYMTAB[ssize].name,LABEL);
strcpy(SYMTAB[ssize++].value,t);
}
void writeinter(FILE *f)
{
strset(INTER.operand,'\0');
strset(INTER.opecode,'\0');
strset(INTER.label,'\0');
strcpy(INTER.operand,OPERAND);
strcpy(INTER.opecode,OPECODE);
strcpy(INTER.label,LABEL);
fwrite(&INTER,sizeof(INTER),1,f);
fputc('\n',f);
}
void pass1()
{
FILE *finter;
int sym=0,op=0;
finter=fopen("INTER.txt","w");
READ();
if(strcmpi(OPECODE,"START")==0)
{
LOCCTR=BEG=hex_dec(OPERAND);
strcpy(NAME,LABEL);
strcpy(START,OPERAND);
index=0;
writeinter(finter);
READ();
}
else LOCCTR=0;
while(strcmpi(OPECODE,"END")!=0)
{
if(strcmpi(LABEL,".")!=0)
{
if(count==3)
{
sym=SSEARCH(LABEL);
printf("DUPLICATE ERROR");
INSERT();
}
strcpy(INTER.loc,dec_hex(LOCCTR));
op=OSEARCH(OPECODE);
if(op>=0)
LOCCTR+=3;
else if(strcmpi(OPECODE,"WORD")==0)
LOCCTR+=3;
else if(strcmpi(OPECODE,"RESW")==0)
LOCCTR+=3*operand_value(OPERAND);
else if(strcmpi(OPECODE,"RESB")==0)
LOCCTR+=operand_value(OPERAND);
else if(strcmpi(OPECODE,"BYTE")==0)
LOCCTR+=strlen(OPERAND);
else
printf("INVALID OPECODE");
}
writeinter(finter);
READ();
}
writeinter(finter);
PGMLEN=LOCCTR-BEG;
fclose(finter);
}
void pass2()
{
FILE *finter,*fout;
int sym=0,op=0,size=0;
char addr[5],code[7],opf[15];
finter=fopen("INTER.txt","r");
printf("NAME OF OUTPUT FILE:- ");
gets(opf);
fout=fopen(opf,"w");
fread(&INTER,sizeof(INTER),1,finter);
fseek(finter,2,1);
if(strcmpi(INTER.opecode,"START")==0)
{
strcpy(HEADER.name,NAME);
strcpy(HEADER.start,START);
strcpy(HEADER.len,dec_hex(PGMLEN));
fread(&INTER,sizeof(INTER),1,finter);
fseek(finter,2,1);
}
fwrite(&HEADER,sizeof(HEADER),1,fout);
putc('\n',fout);
strcpy(TEXT.start,INTER.loc);
while(strcmpi(INTER.opecode,"END")!=0)
{
strset(code,'\0');
strset(addr,'\0');
op=OSEARCH(INTER.opecode);
if(op>0)
{
if(strlen(INTER.operand)>=1)
{
sym=SSEARCH(INTER.operand);
if(sym>=0)
{
strcpy(addr,SYMTAB[sym].value);
}
}
strcpy(code,OPTAB[op].value);
strcat(code,addr);
}
else if(strcmpi(INTER.opecode,"BYTE")==0||
strcmpi(INTER.opecode,"BYTE")==0);
if(size==3)
{
strcpy(TEXT.len,"0009");
fwrite(&TEXT,sizeof(TEXT),1,fout);
putc('\n',fout);
size=0;
strcpy(TEXT.start,INTER.loc);
strset(TEXT.code,'\0');
}
strcat(TEXT.code,code);
if(op>=0)
size++;
fread(&INTER,sizeof(INTER),1,finter);
fseek(finter,2,1);
}
size*=3;
strcpy(TEXT.len,dec_hex(size));
fwrite(&TEXT,sizeof(TEXT),1,fout);
putc('\n',fout);
strcpy(END.first,START);
fwrite(&END,sizeof(END),1,fout);
fclose(fout);
}
void main()
{
int sym,op;
char text[20],ip[15];
clrscr();
initial();
printf("NAME OF CODE FILE:- ");
gets(ip);
fp=fopen(ip,"r");
index=0;
pass1();
printf("\n\n\n YOUR PROGRAM %s ",ip);
printf("IS SUCCESSFULLY ASSEMBLED\n\n");
pass2();
fclose(fp);
}
INPUT FILES
PROGRAM.TXT
COPY START 0050
LDA ZERO
STA INDEX
ADDLP LDX INDEX
LDA ALPHA
ADD BETA
STA GAMMA
LDA INDEX
ADD THREE
STA INDEX
COMP K300
JLT ADDLP
INDEX RESW 1
ALPHA RESW 1
BETA RESW 1
GAMMA RESW 1
ZERO WORD 0
THREE WORD THREE
K300 WORD 300
END START
OPCODE.TXT
ADD 18
AND 40
COMP 20
DIV 24
JMP 3C
JEQ 30
JGT 34
JLT 38
JSUB 48
LDA 00
LDCH 50
LDL 08
LDX 04
MUL 20
OR 44
RD D8
RSUB 4C
STA 0C
STCH 54
STL 14
STX 10
SUB 1C
TIX 2C
WD DC
OUTPUT
NAME OF CODE FILE:- PROG.TXT
YOUR PROGRAM PROG.TXT IS SUCCESSFULLY ASSEMBLED
NAME OF OUTPUT FILE:- OUT.TXT
OUTPUT FILES
INTER.TXT
COPY START 0050
0050 LDA ZERO
0053 STA INDEX
0056 ADDLP LDX INDEX
0059 LDA ALPHA
005C ADD BETA
005F STA GAMMA
0062 LDA INDEX
0065 ADD THREE
0068 STA INDEX
006B COMP K300
006E JLT ADDLP
0071 INDEX RESW 1
0074 ALPHA RESW 1
0077 BETA RESW 1
007A GAMMA RESW 1
007D ZERO WORD 0
0080 THREE WORD THREE
0083 K300 WORD 300
0083 END START
OUT.TXT
HCOPY 0050 0036
T00007D0C0071040071 0050 0009
T0000740C007A 0059 0009
T0000710C0071 0062 0009
T200083380056 006B 0006
E0050
LEXICAL ANALYSER
#include
#include
#include
FILE *ptr1,*ptr2,*ptr3;
char line[50],kword[10][20],op[10][10],cons[10][10],iden[10][10],word[20],others[10][10];
int i,j,k,kw=0,ow=0,iw=0,cw=0,opw=0,flag1=0;
int opcheck()
{
char c; int v=0;
ptr3=fopen("opfile1.txt","r");
while(c!='$')
{
c=fgetc(ptr3);
if(line[i]==c)
{
v=1;
break;
}
}
fclose(ptr3);
return (v);
}
void check()
{
int lim=0;
int flag=0,num,check1,check2;
char key[10];
flag1=0;
ptr2=fopen("keyfile.txt","r");
while(!feof(ptr2))
{
fscanf(ptr2,"%s",key);
if(strcmp(word,key)==0)
{
flag=1;
check2=0;
for(num=0;num
check1=strcmp(kword[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
strcpy(kword[kw++],word);
break;
}
}
fclose(ptr2);
if(flag==0)
{
for(k=0;word[k]!='\0';k++)
if (!isdigit(word[k]))
flag1=1;
if(flag1==0)
{
check2=0;
for(num=0;num
check1=strcmp(cons[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
strcpy(cons[cw++],word);
}
else
if(!isdigit(word[0]))
{
check2=0;
for(num=0;num
check1=strcmp(iden[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
strcpy(iden[iw++],word);
}
else
printf("invalid token%s",word);
}
}
int opcheck1()
{
char c[10];
int v=0;
ptr3=fopen("opfile.txt","r");
while(!feof(ptr3))
{
fgets(c,3,ptr3);
if(strcmp(word,c)==0)
{
v=1;
break;
}}
fclose(ptr3);
return (v);
}
void tokenise()
{
int flag=0,num,check1,check2;
i=0;
while(line[i]!='\n')
{
j=0;
while(line[i]==' ')
i++;
while(isalnum(line[i]))
word[j++]=line[i++];
word[j]='\0';
if (strcmp(word,"\0")!=0)
{
check();
strcpy(word,"\0");
continue;
}
j=0;
while (line[i]==' ')
i++;
while(opcheck()==1)
word[j++]=line[i++];
word[j]='\0';
if( strcmp(word,"\0")!=0)
{
if(word[i]!='\0')
if(opcheck1()!=1)
flag=1;
if(flag==0)
{
check2=0;
for(num=0;num
check1=strcmp(op[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
strcpy(op[opw++],word);
}
strcpy(word,"\0");
continue;
}
j=0;
while (line[i]==' ')
i++;
while(ispunct(line[i]))
word[j++]=line[i++];
word[j]='\0';
if (strcmp(word,"\0")!=0)
{
check2=0;
for(num=0;num
check1=strcmp(others[num],word);
if(check1==0)
{
check2=1;
break;
}
}
if(check2!=1)
{
strcpy(others[ow++],word);
strcpy(word,"\0");
continue;
}
}
}
}
void main()
{
int j;
clrscr();
ptr1=fopen("program.txt","r");
fgets(line,50,ptr1);
while(!feof(ptr1))
{
tokenise();
fgets(line,50,ptr1);
}
fclose(ptr1);
printf(" \n\nKEYWORDS\n\n");
for(i=0;i
printf(" \n\nIDENTIFIERS\n\n");
for(i=0;i
printf(" \n\nCONSTANTS\n\n");
for(i=0;i
printf(" \n\nOPERATORS\n\n");
for(i=0;i
printf(" \n\nOTHERS\n\n");
for(i=0;i
getch();
}
INPUT FILES
KEYFILE.TXT
main ,
if ,
goto ,
continue ,
else ,
do ,
for ,
float ,
case ,
break ,
void ,
int ,
char ,
while ,
printf ,
scanf ,
switch ;
OPFILE.TXT
!= ,
++ ,
-- ,
+ ,
- ,
* ,
/ ,
== ,
<= ,
>= ,
&& ,
|| ,
< ,
> ;
OPFILE1.TXT
+-*/<>=%$&(){}
PROGRAM.TXT
void main( )
{
int i,a=10;
int b=2;
char c[15] ;
if(a<=15)
{
a=a-1;
a=a+2;
}
b=b*3;
b=b/4;
for(i=0;i<=10;i++)
{
printf( "%d" ,a) ;
}
}
OUTPUT
KEYWORDS
void main int char if for printf
IDENTIFIERS
i a b c d
CONSTANTS
10 2 15 1 3 4 0
OPERATORS
= <= - + * / ++
OTHERS
( ) { , ; [ ] } "% "
ABSOLUTE LOADER
#include
#include
#include
#include
#include
#include
void main()
{
FILE *objfile,*ofile;
char line[80],locctr[7],number[7],value[7],t[7]="\0",obj[10];
char *temp="\0";
int i,j,k,len,a[6],l,m;
long loc=0,num=0,x;
clrscr();
printf("\n\t*******************************************************\n");
printf("\n\t\t\tABSOLUTE LOADER");
printf("\n\t*******************************************************\n");
printf("\n\nEnter the name of object file:\t");
gets(obj);
//objfile = fopen(obj,"r");
objfile = fopen("st.txt","r");
fgets(line,80,objfile);
ofile = fopen("memory.txt","w");
for(i=2;line[i] != '^';i++)
i++;
for(j=0;line[i] != '^';j++,i++)
locctr[j] = line[i];
locctr[j] = '\0';
len=strlen(locctr);
for(k=0;k
if(locctr[k]>57)
a[k] = locctr[k] - 55;
else
a[k] = locctr[k] - 48;
}
for(k=len-1;k>=0;k--)
loc += a[k] * pow(16,len-1-k);
i++;
for(j=0;line[i] != '\0';j++,i++);
fgets(line,80,objfile);
while(line[0] != 'E')
{
for(j=0,i=2;line[i] != '^'&&line[i] != '\n';i++,j++)
number[j] = line[i];
number[j] = '\0';
len=strlen(number);
for(k=0;k
if(number[k]>57)
a[k] = number[k] - 55;
else
a[k] = number[k] - 48;
}
for(k=len-1;k>=0;k--)
num += a[k] * pow(16,len-1-k);
while((num - loc) > 0)
{
k=0;l=0;
t[0] = '\0';
temp = t;
x = loc;
while(x>0)
{
a[l] = x%16;
l++;
x /= 16;
}
l--;
while(k<(5-1))
{
*temp= '0';
temp++;
k++;
}
for(m=1;m>=0;m--)
{
if(a[m]>9)
*temp = a[m] + 55;
else
*temp = a[m] + 48;
temp++;
}
*temp= '\0';
fputs(t,ofile);
fputs(" **",ofile);
fputs("\n",ofile);
loc++;
}
i = 12;
while(line[i] != '\0' && line[i] != '\n')
{
for(j=0;j<2;i++)
{
if(line[i] != '^')
{
value[j] = line[i];
j++;
}
}
value[j] = '\0';
k=0;
l=0;
t[0] = '\0';
temp = t;
x = loc;
while(x>0)
{
a[l] = x%16;
l++;
x /= 16;
}
l--;
while(k<(5-1))
{
*temp = '0';
temp++;
k++;
}
for(m=l;m>=0;m--)
{
if(a[m]>9)
*temp= a[m] + 55;
else
*temp= a[m] + 48;
temp++;
}
*temp= '\0';
fputs(t,ofile);
fputs(" ",ofile);
fputs(value,ofile);
fputs("\n",ofile);
loc++;
}
fgets(line,80,objfile);
}
printf("\n\n\n\t\t");
textcolor(YELLOW+BLINK);
cprintf("The object file is loaded into the memory");
getch();
fclose(objfile);
fclose(ofile);
}
INPUT FILE
OBJCODE.TXT
H^MERGE^000064^000020
T^000066^1E^180065^34006F^180065^280064^200064^DC007E^280065^38006F
180064^C90064
E^000064
OUTPUT
*******************************************************
ABSOLUTE LOADER
*******************************************************
Enter the name of object file: OBJCODE.TXT
The object file is loaded into the memory
OUTPUT FILE
MEMORY.TXT
000000 **
000001 **
000002 **
000003 **
000004 **
000005 **
000006 **
000007 **
000008 **
000009 **
00000A **
00000B **
00000C **
00000D **
00000E **
00000F **
000010 **
000011 **
000012 **
000013 **
000014 **
000015 **
000016 **
000017 **
000018 **
000019 **
00001A **
00001B **
00001C **
00001D **
00001E **
00001F **
000020 **
000021 **
000022 **
000023 **
000024 **
000025 **
000026 **
000027 **
000028 **
000029 **
00002A **
00002B **
00002C **
00002D **
00002E **
00002F **
000030 **
000031 **
000032 **
000033 **
000034 **
000035 **
000036 **
000037 **
000038 **
000039 **
00003A **
00003B **
00003C **
00003D **
00003E **
00003F **
000040 **
000041 **
000042 **
000043 **
000044 **
000045 **
000046 **
000047 **
000048 **
000049 **
00004A **
00004B **
00004C **
00004D **
00004E **
00004F **
000050 **
000051 **
000052 **
000053 **
000054 **
000055 **
000056 **
000057 **
000058 **
000059 **
00005A **
00005B **
00005C **
00005D **
00005E **
00005F **
000060 **
000061 **
000062 **
000063 **
000064 **
000065 **
000066 18
000067 00
000068 65
000069 34
00006A 00
00006B 6F
00006C 18
00006D 00
00006E 65
00006F 28
000070 00
000071 64
000072 20
000073 00
000074 64
000075 DC
000076 00
000077 7E
000078 28
000079 00
00007A 65
00007B 38
00007C 00
00007D 6F
00007E **
00007F **
000080 **
000081 **
000082 **
000083 **
000084 **
000085 **
000086 **
000087 **
000088 **
000089 **
00008A **
00008B **
00008C **
00008D **
00008E **
00008F **
000090 **
000091 **
000092 **
000093 **
000094 **
000095 **
000096 **
000097 **
000098 **
000099 **
00009A **
00009B **
00009C **
00009D **
00009E **
00009F **
0000A0 **
0000A1 **
0000A2 **
0000A3 **
0000A4 **
0000A5 **
0000A6 **
0000A7 **
0000A8 **
0000A9 **
0000AA **
0000AB **
0000AC **
0000AD **
0000AE **
0000AF **
0000B0 **
0000B1 **
0000B2 **
0000B3 **
0000B4 **
0000B5 **
0000B6 **
0000B7 **
0000B8 **
0000B9 **
0000BA **
0000BB **
0000BC **
0000BD **
0000BE **
0000BF **
0000C0 **
0000C1 **
0000C2 **
0000C3 **
0000C4 **
0000C5 **
0000C6 **
0000C7 **
0000C8 **
0000C9 **
0000CA 4
MACRO PROCESSOR
#include
#include
#include
#include
#include
#include
FILE *fp,*fo;
int expand=0;
struct line
{
char lab[20];
char opc[20];
char opr[25];
}DEFTAB[30];
int idef=0;
struct nametab
{
char name[20];
int st;
int end;
int ast;
int aend;
}NAMETAB[10],NAM;
int nam=0;
char ARGPOS[5][10];
int arg=0;
struct arg
{
char name[25];
char pos[25];
}ARGTAB[10];
struct line GETLINE();
void PROCESSLINE(struct line LINE);
void replace(char src[25],char pat[15],char newpat[15])
{
char *w,t[25];
int i=0,j,k;
w=strstr(src,pat);
strcpy(t,w);
if(w!='\0')
{
*w='\0';
strcat (src,newpat);
}
while(pat[i]==t[i])
i++;
j=strlen(src);
while(t[i-1]!='\0')
src[j++]=t[i++];
strcpy(t,src);
strset(src,'\0');
for( k=0;k<=5;k++)
src[i]='\0';
strcpy(src,t);
}
struct line GETLINE()
{
struct line LINE={"\0","\0","\0"};
char lin[40]={"\0"};
int i=1,j;
char c=getc(fp);
lin[0]=c;
while(lin[i-1]!='\n')
{
lin[i++]=getc(fp);
if(feof(fp))
break;
}
lin[i-1]='\n';
i=0;
while(lin[i++]==' ');
i--;
j=0;
while(lin[i]!=' ' && lin[i]!='\n')
LINE.lab[j++]=lin[i++];
while(lin[i++]==' ');
i--;
j=0;
while(lin[i]!=' ' && lin[i]!='\n')
LINE.opc[j++]=lin[i++];
while(lin[i++]==' ');
i--;
j=0;
while(lin[i]!=' ' && lin[i]!='\n')
LINE.opr[j++]=lin[i++];
if(strcmp(LINE.opr,"\0")==0)
{
strset(LINE.opr,'\0');
strcpy(LINE.opr,LINE.opc);
strset(LINE.opc,'\0');
strcpy(LINE.opc,LINE.lab);
strset(LINE.lab,'\0');
}
return LINE;
}
int NAM_SEARCH(char x[])
{
int i;
for (i=0;i
{
NAM=NAMETAB[i];
return i;
}
return(-1);
}
void EXPAND(struct nametab nam, struct line LINE)
{
char x[20];
int rdef=nam.st;
int i=0,j=0,k;
expand=1;
arg=nam.ast;
strcpy(x,LINE.lab);
putc('.',fo);
fwrite(&LINE,sizeof(LINE),1,fo);
putc('\n',fo);
while(LINE.opr[i]!='\0')
{
strset(ARGTAB[arg].name,'\0');
while(LINE.opr[i]!=','&&LINE.opr[i]!='\0')
ARGTAB[arg].name[j++]=LINE.opr[i++];
j=0;
arg++;
i++;
}
LINE=DEFTAB[rdef++];
while(strcmp(LINE.opc,"MEND")!=0)
{
LINE=DEFTAB[rdef++];
if(rdef==nam.st+2)
strcpy(LINE.lab,x);
for(k=nam.ast;k
if(strcmp(LINE.opc,"MEND")!=0)
PROCESSLINE(LINE);
}
expand=0;
}
void DEFINE(struct line LINE)
{
int i=0,j=0,k;
int level=1;
strcpy(NAMETAB[nam].name,LINE.lab);
NAMETAB[nam].st=idef;
NAMETAB[nam].ast=arg;
while(LINE.opr[i]!='\0')
{
while(LINE.opr[i]!=','&& LINE.opr[i]!='\0')
ARGTAB[arg].name[j++]=LINE.opr[i++];
j=0;
arg++;
i++;
}
for (k=NAMETAB[nam].ast;k
ARGTAB[k].pos[0]='?';
ARGTAB[k].pos[2]='\0';
ARGTAB[k].pos[1]=toascii(k+1+48);
}
DEFTAB[idef++]=LINE;
while(level>0)
{
LINE=GETLINE();
for(k=NAMETAB[nam].ast;k
DEFTAB[idef++]=LINE;
if(strcmp(LINE.opc,"MACRO")==0)
level++;
else if (strcmp(LINE.opc,"MEND")==0)
level--;
}
NAMETAB[nam].end=idef;
NAMETAB[nam++].aend=arg;
}
void PROCESSLINE(struct line LIN)
{
int x;
struct line LINE={"\0","\0","\0"};
strcpy(LINE.lab,LIN.lab);
strcpy(LINE.opc,LIN.opc);
strcpy(LINE.opr,LIN.opr);
x=NAM_SEARCH(LINE.opc);
if (x>=0)
EXPAND(NAM,LINE);
else if(strcmp(LINE.opc,"MACRO")==0)
DEFINE(LINE);
else
{
fwrite(&LINE,sizeof(LINE),1,fo);
putc('\n',fo);
}
}
void main()
{
struct line LINE={"\0","\0","\0"};
char ip[20],op[20];
clrscr();
printf("\n\t*******************************************************\n");
printf("\n\t\t\tMACRO PROCESSOR");
printf("\n\t*******************************************************\n");
printf("\n\tENTER NAME OF INPUT FILE: ");
gets(ip);
printf("\n\tENTER NAME OF OUTPUT FILE: ");
gets(op);
fp=fopen(ip,"r");
fo=fopen(op,"w");
expand=0;
while(strcmp(LINE.opc,"END")!=0)
{
LINE=GETLINE();
PROCESSLINE(LINE);
}
}
INPUT FILE
MACRO.TXT
COPY START 1000
READ MACRO &INDEV,&BUFADR,&RECLTH
CLEAR X
CLEAR A
CLEAR S
+LDT #4096
TD =X'&INDEV'
JEQ *-3
RD =X'&INDEV'
COMPR A,S
JEQ *+11
STCH &BUFADR,X
TIXR T
JLT *-19
STX &RECLTH
MEND
WRITE MACRO &OUTDEV,&BUFADR,&RECLTH
CLEAR X
LDT &RECLTH
LDCH &BUFADR,X
TD =X'&OUTDEV'
JEQ *-3
WD =X'&OUTDEV'
TIXR T
JLT *-14
MEND
FIRST STL RETADR
CLOP READ F1,BUFER,LENGTH
LDA LENGTH
COMP #0
JEQ ENDFIL
WRITE 05,BUFFER,LENGTH
J CLOOP
ENDFIL WRITE 05,EOF,THREE
J @RETADR
EOF BYTE C'EOF'
THREE WORD 3
RETADR RESW 1
LENGTH RESW 1
BUFFER RESB 4096
END FIRST
OUTPUT
*******************************************************
MACRO PROCESSOR
*******************************************************
ENTER NAME OF INPUT FILE: MACRO.TXT
ENTER NAME OF OUTPUT FILE: MOUT.TXT
OUTPUT FILE
MOUT.TXT
COPY START 1000
FIRST STL RETADR
.CLOP READ F1,BUFER,LENGTH
CLOP CLEAR X
CLEAR A
CLEAR S
+LDT #4096
TD =X'F1'
JEQ *-3
RD =X'F1'
COMPR A,S
JEQ *+11
STCH BUFER,X
TIXR T
JLT *-19
STX LENGTH
LDA LENGTH
COMP #0
JEQ ENDFIL
. WRITE 05,BUFFER,LENGTH
CLEAR X
LDT LENGTH
LDCH BUFFER,X
TD =X'05'
JEQ *-3
WD =X'05'
TIXR T
JLT *-14
J CLOOP
.ENDFIL WRITE 05,EOF,THREE
ENDFIL CLEAR X
LDT THREE
LDCH EOF,X
TD =X'05'
JEQ *-3
WD =X'05'
TIXR T
JLT *-14
J @RETADR
EOF BYTE C'EOF'
THREE WORD 3
RETADR RESW 1
LENGTH RESW 1
BUFFER RESB 4096
END FIRST
NFA TO DFA CONVERSION
#include
#include
int total;
typedef struct
{
int current,next;
char input;
} transition;
transition nfa[20],dfa[20];
typedef struct
{
int eps[20];
int n,flag;
} closure;
closure e_closure(closure x)
{
int stack[20],top,a,i,j;
closure y;
for(top=0;top
top--;
y = x;
while(top != -1)
{
a = stack[top--];
for(i=0;i
if(nfa[i].current == a && nfa[i].input == 'E')
{
for(j=0;j
if(nfa[i].next == y.eps[j])
break;
}
if(j == y.n)
{
stack[++top] = nfa[i].next;
y.eps[y.n] = nfa[i].next;
(y.n)++;
}
}
}
}
return y;
}
int checking(closure x,closure y)
{
int flag = 1,i;
if(x.n != y.n)
flag = 0;
for(i=0;flag && i
if(x.eps[i]!=y.eps[i])
flag = 0;
}
return flag;
}
void main()
{
int i,j=0,check=1,k,l,m,z=0,ipn,o,final;
char input[10],x;
closure a[20],temp;
clrscr();
printf("Enter the number of transitions : ");
scanf("%d",&total);
printf("Enter the number of input symbols (without E) : ");
scanf("%d",&ipn);
printf("Enter the input symbols (except E) : ");
for(i=0;i
scanf("%c",&x);
scanf("%c",&input[i]);
}
printf("Enter the transition table\n");
for(i=0;i
printf("Current state : ");
scanf("%d",&nfa[i].current);
printf("Input symbol : ");
scanf("%c",&x);
scanf("%c",&nfa[i].input);
printf("Next state : ");
scanf("%d",&nfa[i].next);
}
printf("Enter the final state: ");
scanf("%d",&final);
temp.eps[0] = nfa[0].current;
temp.n = 1;
a[j] = e_closure(temp);
a[j].flag = 0;
j++;
k = 0;
while(check)
{
a[k].flag = 1;
for(i=0;i
temp.n = 0;
for(m=0;m {
for(l=0;l
if(nfa[l].current == a[k].eps[m] && nfa[l].input == input[i])
{
temp.eps[temp.n] = nfa[l].next;
(temp.n)++;
}
}
}
temp = e_closure(temp);
for(m=0;m
break;
if(m == j)
{
temp.flag = 0;
a[j++] = temp;
}
for(l=0;l
if(dfa[l].current == k && dfa[l].input == input[i])
break;
}
if(l == z)
{
dfa[z].current = k;
dfa[z].input = input[i];
dfa[z].next = m;
z++;
}
}
for(k=0;k
break;
if(k == j)
check = 0;
}
clrscr();
printf("DFA transition : \n");
printf("Current\tinput\tnext\n");
for(i=0;i
printf("%d\t",dfa[i].current);
printf("%c\t",dfa[i].input);
printf("%d\n",dfa[i].next);
}
getch();
}
OUTPUT
Enter the number of transitions : 3
Enter the number of input symbols (without E) : 2
Enter the input symbols (except E) : a b
Enter the transition table
Current state :1
Input symbol : a
Next state : 1
Current state : 1
Input symbol : a
Next state : 2
Current state : 1
Input symbol : b
Next state : 2
Enter the final state: 2
DFA transition :
Current input next
0 a 1
0 b 2
1 a 1
1 b 2
2 a 3
2 b 3
3 a 3
3 b 3
OPERATOR PRECEDENCE PARSING
#include
#include
#include
#include
int i,k,w,e,q,length,rhslength,len,plength,row;
int column,i,j,u,valid,v,y,begin,top,z;
char aa,bb,temp,term[10],rhs[10],token[25],stack[20],nt,handle[10],tm;
char table[10][10]={
{' ','+','-','*','/','^','i','(',')','$'},
{'+','>','>','<','<','<','<','<','>','>'},
{'-','>','>','<','<','<','<','<','>','>'},
{'*','>','>','>','>','<','<','<','>','>'},
{'/','>','>','>','>','<','<','<','>','>'},
{'^','>','>','>','>','<','<','<','>','>'},
{'i','>','>','>','>','>',' ',' ','>','>'},
{'(','<','<','<','<','<','<','<','=',' '},
{')','>','>','>','>','>',' ',' ','>','>'},
{'$','<','<','<','<','<','<','<','>','>'}
};
char production[9][10]={
{'E','-','>','E','A','E','\0'},
{'E','-','>','(','E',')','\0'},
{'E','-','>','-','E','\0'},
{'E','-','>','i','\0'},
{'E','-','>','+','\0'},
{'E','-','>','-','\0'},
{'E','-','>','*','\0'},
{'E','-','>','/','\0'}
};
void find_row_for_a(char a)
{
for(j=0;j<10;j++)
{
if(table[j][0]==a)
break;
}
row=j;
if(j==10)
{
printf("string is not accepted\n");
exit(0);
}
}
void find_column_for_b(char b)
{
for(j=0;j<10;j++)
{
if(table[0][j]==b)
break;
}
column=j;
if(j==10)
{
printf("string is not accepted\n");
exit(0);
}
}
void push(char info)
{
top++;
stack[top]=info;
for(k=0;k
length--;
token[length]='\0';
}
void get_rhs()
{
e=0;
plength=strlen(production[y]);
nt=production[i][0];
while(production[y][e]!='>')
e++;
e++;
q=0;
while(e
rhs[q]=production[y][e];
e++;
q++;
}
if((rhs[1]=='A')&&(handle[1]=='+')||(handle[1]=='*')||(handle[1]=='-')||(handle[1]=='^')||(handle[1]=='/'))
rhs[1]=handle[1];
rhs[q]='\0';
rhslength=strlen(rhs);
}
void reduce()
{
for(y=0;y<9;y++)
{
get_rhs();
if(strcmp(rhs,handle)==0)
{
top++;
stack[top]=nt;
break;
}
}
if(y==9)
{
printf("string is not accepted\n");
exit(0);
}
stack[top+1]='\0';
}
void check_for_precedence()
{
aa=stack[top];
z=top;
while(aa=='E'||aa=='A')
{
z--;
aa=stack[z];
}
bb=token[i];
find_row_for_a(aa);
find_column_for_b(bb);
if(table[row][column]=='<'||table[row][column]=='=')
push(bb);
else if(table[row][column]=='>')
{
u=0;
while(top>=0)
{
if (stack[top]=='E'||stack[top]=='A')
{
while (stack[top]=='E'||stack[top]=='A')
{
term[u]=stack[top];
top--;
u++;
}}
term[u]=stack[top];
tm=stack[top];
top--;
while (stack[top]=='E'||stack[top]=='A')
{
u++;
term[u]=stack[top];
top--;
}
temp=stack[top];
find_row_for_a(temp);
find_column_for_b(tm);
if(table[row][column]=='<')
{
valid=1;
break;
}
u++;
}
if(!valid)
{
printf("String is not accepted\n");
exit(0);
}
len=u+1;
v=len-1;
for(w=0;w
handle[w]=term[v];
v--;
}
handle[len]='\0';
reduce();
}
else
{
printf("String is not accepted\n");
exit(0);
}
}
void check()
{
while(length>=1)
{
check_for_precedence();
begin=1;
if((stack[0]=='$')&&(stack[1]=='E')&&(stack[2]=='\0')&&(token[0]=='$')&&(token[1]=='\0'))
{
printf("String is accepted\n");
exit(0);
}
if(stack[0]=='$'&&stack[1]!='E'&&stack[2]=='\0'&&token[0]=='$'&&token[1]=='\0')
{
printf("String is not accepted\n");
exit(0);
}
}}
main()
{
clrscr();
printf("\t\tOPERATOR PRECEDENCE FOR THE GRAMMAR\n\n\n\n");
printf("\t\tE->EAE|i\n\n\n");
printf("\t\tA->+|-|*|/+^\n\n\n");
printf("\n\tEnter the string(Add $ to mark end of string)\n");
scanf("%s",&token);
i=0;
length=strlen(token);
begin=0;
top=0;
stack[top]='$';
if((stack[top]=='$')&&(strcmp(token,"$")==0))
{
printf("String is accepted\n");
exit(0);
}
check();
return(0);
getch();
}
OUTPUT
OPERATOR PRECEDENCE FOR THE GRAMMAR
E->EAE|i
A->+|-|*|/+^
Enter the string(Add $ to mark end of string) i+i*i^i$
String is accepted
FIRST AND FOLLOW
#include
#include
#include
#include
char first[10][10],line[20],c[2],d[10],e[10],nt[10],follow[10][10];
FILE *pt1;
int f=0,l=0;
void setfirst()
{
do{
int i=0,j=0;
nt[f]=line[i++];
if(line[i]!='-')
nt[++f]=line[i++];
while(line[i]!='>')
i++;
i++;
first[f][j++]=line[i++];
while((line[i]!='\n')&&(line[i]!='/'))
i++;
while(line[i]!='\n')
{
i++;
first[f][j++]=line[i++];
if((line[i]=='d')&&(line[i]!='\n'))
first[f][j++]=line[i++];
}
f++;
fgets(line,20,pt1);
}
while(!feof(pt1));
fclose(pt1);
pt1=fopen("prod.txt","r");
}
void searchfirst(char v,int q,int y)
{
int i,j,k;
for(i=0;i
{
if(q==1)
i=i+2;
if(y==0)
strcpy(e,first[i]);
else
strcpy(e,follow[i]);
break;
}
}
int search(char v,int q)
{
int i;
for(i=0;i
{
if(q==1)
i=i+2;
return i;
}
return(-1);
}
void rule1()
{
int k=3,j,c,s=0,p,i;
char b,B;
if(line[1]!='-')
k=4;
if(line[k]=='(')
{
k++;
c=search(line[k],0);
for(s=0;follow[c][s]!='\0';s++)
follow[c][++s]=line[k+1];
follow[c][s]='\0';
}
else
{
if(!isalnum(line[k]))
k++;
if(!isalnum(line[k+1]))
k=k+2;
b=line[k+1];
if(!isalnum(line[k+2]))
s=1;
c=search(line[k],0);
searchfirst(b,s,0);
for(s=0;follow[c][s]!='\0';s++)
for(j=0;j<4;j++)
if(e[j]!='e')
{
p=0;
for(i=0;i
p=1;
if(p==0)
follow[c][++s]=e[j];
}
follow[c][s]='\0';
}
}
void rule2()
{
char a,b;
int s=0,j=3,p=0,c,k;
a=line[0];
if(line[1]!='-')
{
s=1;j=4;
}
while((line[j]!='\n')&&(line[j]!='/'))
{
if(!isalnum(line[j]))
j++;
else
{
b=line[j];
if(!isalnum(line[j+1]))
p=1;
c=search(b,p);
if((a==b)&&(s==p))
{
j++;
break;
}
for(k=0;follow[c][k]!='\0';k++)
follow[c][k++]=a;
if(s==1)
follow[c][k++]=')';
follow[c][k++]='\0';
j++;
}
}
}
void setfollow()
{
int i;
for(i=0;i<10;i++)
follow[i][0]='$';
do
{
if (l==0)
{
follow[0][0]='$';
follow[0][1]='\0';
l=0;
}
rule1();
if(line[0]!='F')
rule2();
fgets(line,25,pt1);
}
while(!feof(pt1));
}
void displayfirst()
{
int i,j,k,p;
for(i=f-1;i>=0;i--)
{
p=k=0;
if(!isalnum(nt[i]))
p=1;
if(p==1)
printf("FIRST(%c%c)=\t",nt[i-1],nt[i]);
else
printf("FIRST(%c)=\t",nt[i]);
for(j=0;j<4;j++)
{
if(first[i][j]=='i')
printf("%c\t",first[i][j++]);
if(!isupper(first[i][j]))
printf("%c\t",first[i][j]);
else
{
if(first[i][j+1]=='\'')
k=1;
searchfirst(first[i][j],k,0);
strcpy(first[i],e);
for(k=0;k<4;k++)
if(first[i][k]=='i')
printf("%c\t",first[i][k]);
else
printf("%c\t",first[i][k]);
break;
}
}
if(p==1)
i--;
printf("\n");
}
}
void displayfollow()
{
int i,j,k,p;
strcpy(follow[2],follow[0]);
strcpy(follow[5],follow[3]);
strcat(follow[6],"+");
for(i=0;i
p=k=0;
if((!isalnum(nt[i+1]))&&((i+1)!=f))
p=1;
if(p==1)
{
printf("FOLLOW(%c%c)=\t",nt[i],nt[i+1]);
i++;
}
else
printf("FOLLOW(%c)=\t",nt[i]);
for(j=0;j<4;j++)
{
if(follow[i][j]=='i')
printf("%c\t",follow[i][j++]);
if(!isupper(follow[i][j]))
printf("%c\t",follow[i][j]);
else
{
d[0]=follow[i][j-1];
d[1]='\0';
if(follow[i][j+1]==')')
k=1;
searchfirst(follow[i][j],k,1);
strcat (d,e);
for(k=0;k<4;k++)
printf("%c",e[k]);
strcpy(follow[i],d);
break;
}
}
printf("\n");
}
}
void main()
{
int i,j;
pt1=fopen("prod.txt","r");
clrscr();
fgets(line,20,pt1);
while(!feof(pt1))
{
printf("\t\tFIRST AND FOLLOW\n");
printf("\t\t******************\n\n\n\n");
printf("\t\tSEE THE GRAMMAR IN PROD.TXT\n\n");
printf("\t\tFIRST\n\n");
setfirst();
displayfirst();
fgets(line,25,pt1);
setfollow();
printf("\n\n");
printf("\t\tFOLLOW\n\n");
setfollow();
displayfollow();
fgets(line,25,pt1);
getch();
}
}
TEXT FILES
PROD.TXT
E->TE'
E'->+TE'/e
T->FT'
T'->*FT'/e
F->(E)/i
OUTPUT
FIRST AND FOLLOW
******************
SEE THE GRAMMAR IN PROD.TXT
FIRST
FIRST(F)= ( i
FIRST(T')= * e
FIRST(T)= ( i
FIRST(E')= + e
FIRST(E)= ( i
FOLLOW
FOLLOW(E)= $ )
FOLLOW(E')= $ )
FOLLOW(T)= $ + )
FOLLOW(T')= $ + )
FOLLOW(F)= $ * ) +
Subscribe to:
Posts (Atom)