You are on page 1of 54

#include<stdio.

h>

#include<conio.h>

#include<math.h>

#include<stdlib.h>

#define NULL 0

struct mot

char mnemo[6];

int opcode,no_opnds,loi;

};

struct pot

char ps_opcode[6];

int opnds;

};

struct alp

char label[7],mne[7],opnd[30];

};

struct st

{
char sym[10],type[10];

int sym_add;

};

struct of

int lc,mac_code,add;

};

struct addnvalue

int addr,value;

};

struct addnvalue add_const[15];

struct mot m[12];

struct pot p[9];

struct alp a[25];

struct st s[15];

struct of o[25];

int searchmot(char str[]);

int searchpot(char str[]);

int searchst(char str[]);


int str2num(char str[]);

int getvalue(int add);

void pass1();

void pass2();

int motptr,poptr,endpos,nmot,npot,nacode,nadata,lc,error,flagm,flagp;

int naot,nast,stptr,firstloc,flagst;

void main()

int i,j,k,c,len,x;

char str[8],str1[8],str2[30],ch;

FILE *fp;

clrscr();

fp=fopen("mot.txt","r");

if(fp==NULL)

printf("File does not exist!");

exit(0);

else

i=0;
while(!feof(fp))

fscanf(fp,"%s%d%d%d",m[i].mnemo,&m[i].opcode,&m[i].no_opnds,&m[i].loi);

i++;

nmot=i;

fclose(fp);

fp=fopen("pot.txt","r");

if(fp==NULL)

printf("File does not exist!");

exit(0);

else

i=0;

while(!feof(fp))

fscanf(fp,"%s%d",p[i].ps_opcode,&p[i].opnds);

i++;

npot=i;
fclose(fp);

fp=fopen("alp.txt","r");

if(fp==NULL)

printf("File does not exist!\n");

else

i=0;

do

fscanf(fp,"%s",str);

flagm=0;

flagp=0;

len=strlen(str);

if(str[len-1]==':')

for(j=0;j<len-1;j++)

a[i].label[j]=str[j];

fscanf(fp,"%s",str);

else

strcpy(a[i].label,NULL);
flagm=searchmot(str);

flagp=searchpot(str);

if(flagm==1||flagp==1)

if(strcmp(str,"STOP")==0)

strcpy(a[i].mne,str);

strcpy(a[i].opnd,NULL);

else if(strcmp(str,"ENDP")==0)

strcpy(a[i].mne,str);

strcpy(a[i].opnd,NULL);

endpos=i;

break;

else

strcpy(a[i].mne,str);

fscanf(fp,"%s",str);

strcpy(a[i].opnd,str);

i++;

}while(!feof(fp));
nacode=endpos;

i=endpos+1;

while(!feof(fp))

fscanf(fp,"%s %s %s",a[i].label,a[i].mne,a[i].opnd);

if(strcmp(a[i].mne,"END")==0)

strcpy(a[i].opnd,NULL);

break;

i++;

nadata=i;

fclose(fp);

i=0;

while(i!=nadata)

if(strcmp(a[i].opnd,"?")!=0)

naot=i;

i++;

else

break;
}

pass1();

fp=fopen("SymT.txt","w+");

j=0;

while(strcmp(s[j].sym,"END")!=0)

fputs(s[j].sym,fp);

fputs(s[j].type,fp);

fprintf(fp,"%d\n",s[j].sym_add);

j++;

fclose(fp);

if(error==0)

pass2();

getch();

int searchmot(char str1[])

int i,x;

for(i=0;i<nmot;i++)

{
x=strcmp(m[i].mnemo,str1);

if(x==0)

motptr=i;

return 1;

return 0;

int searchpot(char str1[])

int i,y;

for(i=0;i<npot;i++)

y=strcmp(p[i].ps_opcode,str1);

if(y==0)

poptr=i;

return 1;

return 0;

}
int searchst(char str1[])

int i,y,z,len,j,x;

char str[30],str2[30];

for(i=0;i<15;i++)

z=strcmp(s[i].sym,str1);

if(z==0)

stptr=i;

return 1;

return 0;

void pass1()

int i,k,y,add,len,j,num=0;

FILE *fp;

char str2[30];

motptr=0;

poptr=0;

stptr=0;

lc=0;
error=0;

i=0;

k=0;

add=0;

while(i!=nadata)

if(strcmp(a[i].mne,"END")==0)

if(error==0)

pass2();

else

printf("\nUnsuccessful Assembly!!");

else if((strcmp(a[i].mne,"START")==0)||(strcmp(a[i].mne,"ORG")==0))

flagp=searchpot(a[i].mne);

if(flagp==1)

if(strcmp(a[i].opnd,"NULL")==0)

lc=0;

else

firstloc=atoi(a[i].opnd);

lc=firstloc;

}
}

else if(strcmp(a[i].mne,"ENDP")==0)

flagp=searchpot(a[i].mne);

if(flagp==1)

motptr=0;

poptr=0;

stptr=0;

else if(strcmp(a[i].mne,"DB")==0)

flagp=searchpot(a[i].mne);

if(flagp==1)

flagst=searchst(a[i].label);

if(flagst==1)

s[stptr].sym_add=lc;

if(strcmp(a[i].opnd,"?")==0)

strcpy(s[stptr].type,"VAR");

lc=lc+1;
}

else if(strcmp(a[i].mne,"DW")==0)

flagp=searchpot(a[i].mne);

if(flagp==1)

flagst=searchpot(a[i].label);

if(flagst==1)

s[stptr].sym_add=lc;

if(strcmp(a[i].opnd,"?")==0)

strcpy(s[k].type,"VAR");

lc=lc+2;

else

num=atoi(a[i].opnd);

s[stptr].sym_add=lc;

add_const[add].addr=lc;

add_const[add].value=num;

strcpy(s[k].type,"CONST");
add++;

lc+=2;

else if(strcmp(a[i].mne,"CONST")==0)

flagp=searchpot(a[i].mne);

if(flagp==1)

flagst=searchst(a[i].label);

if(flagst==1)

s[stptr].sym_add=lc;

strcpy(s[stptr].type,"CONST");

add_const[add].addr=lc;

add_const[add].value

=atoi(a[i].opnd);

add++;

lc+=1;

else if((strcmp(a[i].label,"NULL")!=0)&&
(strcmp(a[i].mne,"NULL")==0)&&

(strcmp(a[i].opnd,"NULL")==0))

y=searchst(a[i].label);

if(y==0)

strcpy(s[k].sym,a[i].label);

strcpy(s[k].type,"LABEL");

s[k].sym_add=lc;

k++;

else

printf("\nDuplicate label!!");

error=1;

else if((strcmp(a[i].label,"NULL")==0)&&

(strcmp(a[i].mne,"NULL")!=0)&&

(strcmp(a[i].opnd,"NULL")==0))

flagm=searchmot(a[i].mne);

lc=lc+m[motptr].loi;

else if((strcmp(a[i].label,"NULL")==0)&&
(strcmp(a[i].mne,"NULL")!=0)&&

(strcmp(a[i].opnd,"NULL")!=0))

flagm=searchmot(a[i].mne);

if(flagm==1)

flagst=searchst(a[i].opnd);

if(flagst==0)

strcpy(s[k].sym,a[i].opnd);

strcpy(s[k].type,"IDN");

s[k].sym_add=NULL;

k++;

lc=lc+m[motptr].loi;

else if((strcmp(a[i].label,"NULL")!=0)&&

(strcmp(a[i].mne,"NULL")!=0)&&

(strcmp(a[i].opnd,"NULL")==0))

flagst=searchst(a[i].label);

if(flagst==0)

strcpy(s[k].sym,a[i].label);
strcpy(s[k].type,"LABEL");

s[k].sym_add=lc;

else

strcpy(str2,s[stptr].type);

if(strcmp(str2,"IDN")==0)

strcpy(s[stptr].type,"LABEL");

s[stptr].sym_add=lc;

flagm=searchmot(a[i].mne);

if(flagm==1)

lc+=m[motptr].loi;

else

error=1;

else if((strcmp(a[i].label,"NULL")!=0)&&

(strcmp(a[i].mne,"NULL")!=0)&&

(strcmp(a[i].opnd,"NULL")!=0))

flagst=searchst(a[i].label);

if(flagst==0)

{
strcpy(s[k].sym,a[i].label);

strcpy(s[k].type,"LABEL");

s[k].sym_add=lc;

k++;

else

if(strcmp(s[stptr].type,"LABEL")!=0)

strcpy(s[stptr].type,"LABEL");

s[k].sym_add=lc;

flagm=searchmot(a[i].mne);

if(flagm==1)

flagst=searchst(a[i].opnd);

if(flagst==1)

{}

else

strcpy(s[k].sym,a[i].opnd);

strcpy(s[k].type,"IDN");

s[k].sym_add=lc;

k++;
}

lc+=m[motptr].loi;

i++;

nast=k-1;

fp=fopen("ST2.txt","w+");

j=0;

while(strcmp("END",s[j].sym)!=0)

fprintf(fp,"%s %s %d\n",s[j].sym,s[j].type,s[j].sym_add);

j++;

strcmp(s[j].sym,"");

strcmp(s[j].type," ");

fclose(fp);

void pass2()

FILE *fp;

int i,k;

fp=fopen("OUTPUTNEW.txt","w+");
motptr=0;

poptr=0;

stptr=0;

lc=0;

error=0;

i=0;

k=0;

while(i!=nadata)

if(strcmp(a[i].mne,"STOP")==0)

flagm=searchmot(a[i].mne);

if(flagm==1)

o[k].lc=lc;

o[k].mac_code=m[motptr].opcode;

o[k].add=NULL;

k++;

printf("\nASSEMLY COMPLETE!");

else if((strcmp(a[i].mne,"START")==0)||

(strcmp(a[i].mne,"ORG")==0))

flagp=searchpot(a[i].mne);
if(flagp==1)

if(strcmp(a[i].opnd,"NULL")==0)

lc=0;

else

firstloc=atoi(a[i].opnd);

lc=firstloc;

else if(strcmp(a[i].mne,"ENDP")==0)

flagp=searchpot(a[i].mne);

if(flagp==1)

motptr=0;

poptr=0;

stptr=0;

else if(strcmp(a[i].mne,"DB")==0)

flagp=searchpot(a[i].mne);

if(flagp==1)
{

flagst=searchpot(a[i].label);

if(flagst==1)

o[k].lc=lc;

o[k].add=s[stptr].sym_add;

lc+=1;

k++;

else if(strcmp(a[i].mne,"DW")==0)

flagp=searchpot(a[i].mne);

if(flagp==1)

flagst=searchst(a[i].label);

if(flagst==1)

o[k].lc=lc;

o[k].add=s[stptr].sym_add;

lc+=2;

k++;

}
}

else if(strcmp(a[i].mne,"CONST")==0)

flagp=searchpot(a[i].mne);

if(flagp==1)

flagst=searchst(a[i].label);

if(flagst==1)

o[k].lc=NULL;

o[k].mac_code=getvalue(s[stptr].sym_add);

o[k].add=s[stptr].sym_add;

k++;

else if((strcmp(a[i].label,"NULL")==0)&&

(strcmp(a[i].mne,"NULL")!=0)&&

(strcmp(a[i].opnd,"NULL")!=0))

flagm=searchmot(a[i].mne);

if(flagm==1)

o[k].lc=lc;

o[k].mac_code=m[motptr].opcode;
flagst=searchst(a[i].opnd);

if(flagst==0)

printf("\nUndefined symbol!!");

else

o[k].add=s[stptr].sym_add;

k++;

lc+=m[motptr].loi;

else if((strcmp(a[i].label,"NULL")!=0)&&

(strcmp(a[i].mne,"NULL")!=0)&&

(strcmp(a[i].opnd,"NULL")==0))

flagm=searchmot(a[i].mne);

if(flagm==1)

o[k].lc=lc;

o[k].mac_code=m[motptr].opcode;

k++;

lc+=m[motptr].loi;

else

error=1;
}

else if((strcmp(a[i].label,"NULL")!=0)&&

(strcmp(a[i].mne,"NULL")!=0)&&

(strcmp(a[i].opnd,"NULL")!=0))

flagm=searchmot(a[i].mne);

if(flagm==1)

o[k].lc=lc;

o[k].mac_code=m[motptr].opcode;

flagst=searchst(a[i].opnd);

if(flagst==1)

o[k].add=s[stptr].sym_add;

k++;

else

printf("\nUndefined symbol!!");

lc+=m[motptr].loi;

i++;

for(i=0;i<naot-1;i++)

printf("%d %d %d \n",o[i].lc,o[i].mac_code,o[i].add);
}

int getvalue(int add)

int i=0;

while(i!=nast)

if(add_const[i].addr==add)

return(add_const[i].value);

i++;

return 0;

}
START 2000

READ N

LOAD ZERO

STORE COUNT

STORE SUM

LOOP: READ X

LOAD X

ADD SUM

STORE SUM

LOAD COUNT

ADD ONE

STORE COUNT

SUB N

JZ OUTER

JMP LOOP

OUTER: WRITE SUM

STOP

ENDP

ZERO CONST 0

ONE CONST 1

SUM DB ?

COUNT DB ?

N DB ?

X DB ?

END
POT

DB 2

DW 2

EQU 2

CONST 2

START 1

ORG 1

LTORG 1

ENDP 0

END 0
MOT

ADD 1 1 2

SUB 2 1 2

MULT 3 1 2

JMP 4 1 2

JNEG 5 1 2

JPOS 6 1 2

JZ 7 1 2

LOAD 8 1 2

STORE 9 1 2

READ 10 1 2

WRITE 11 1 2

STOP 12 0 1

JNZ 13 1 2
SYMBOL TABLE

N VAR 2035

ZERO CONST 2031

COUNT VAR 2034

SUM VAR 2033

LOOP LABEL 2008

X VAR 2036

ONE CONST 2032

OUTER LABEL 2024


/* Implementation of simple macroprocessor */

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct deftab
{
char lab[10];
char opc[10];
char oper[10];
}d[10];
void main()
{
char label[10],oc[10],newlabel[10],op[10],newoperand[10];
char macroname[10];
int i,lines;
int index1=1;
int index2=1;
FILE *f1,*f2,*f3,*f4;
clrscr();
f1=fopen("nilin.txt","r");
f2=fopen("niloutput.txt","w");
f3=fopen("nilmdt.txt","w");
f4=fopen("nilmnt.txt","w");
fscanf(f1,"%s%s%s",label,oc,op);
while(strcmp(oc,"END")!=0)
{
if(strcmp(oc,"MACRO")==0)
{
strcpy(macroname,label);
if(index1==1)
fprintf(f4,"INDEX\tMacroname\tMDT INDEX\n");
fprintf(f4,"%d\t%s\t\t%d",index1,macroname,index2);
index1++;
fscanf(f1,"%s%s%s",label,oc,op);
lines=0;
while(strcmp(oc,"MEND")!=0)
{
if(index2==1)
fprintf(f3,"INDEX\tMacroDefinition\n");
fprintf(f3,"%d\t%s\t%s\t%s\n",index2,label,oc,op);
strcpy(d[lines].lab,label);
strcpy(d[lines].opc,oc);
strcpy(d[lines].oper,op);
fscanf(f1,"%s%s%s",label,oc,op);
lines++;
index2++;
}
if(strcmp(oc,"MEND")==0)
fprintf(f3,"%d\t**\tMEND\t**",index2);
}
else
if(strcmp(oc,macroname)==0)
{
printf("Lines = %d\n",lines);
for(i=0;i<lines;i++)
{
fprintf(f2,"%s\t%s\t%s\n",d[i].lab,d[i].opc,d[i].oper);
printf("LAB = %s\nOPC = %s\noper = %s\n\n",d[i].lab,d[i].opc,d[i].oper);
}
}
else
fprintf(f2,"%s\t%s\t%s\n",label,oc,op);
fscanf(f1,"%s%s%s",label,oc,op);
}
fprintf(f2,"%s\t%s\t%s\n",label,oc,op);
fclose(f1);
fclose(f2);
fclose(f3);
printf("FINISHED");
getch();
}
Input file
CALC START 1000
SUM MACRO **
** LDA #5
** ADD #10
** STA 2000
** MEND **
** LDA LENGTH
** COMP ZERO
** JEQ LOOP
** SUM **
LENGTH WORD 5
ZERO WORD 0
LOOP SUM **
** END **

Macro Name Table


INDEX Macroname MDT INDEX
1 SUM 1

Macro Definition Table


INDEX Macro Definition
1 ** LDA #5
2 ** ADD #10
3 ** STA 2000
4 ** MEND **

Expanded Code
CALC START 1000
** LDA LENGTH
** COMP ZERO
** JEQ LOOP
** LDA #5
** ADD #10
** STA 2000
LENGTH WORD 5
ZERO WORD 0
** LDA #5
** ADD #10
** STA 2000
** END **
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

struct header
{
long start_add;
int length;
long data[10];
}h;
struct memory
{
long memloc;
int length;
long data[10];
}m;
void main()
{
clrscr();
int n,start_add,x,f,max_byte;
char ch;
start:
cout<<"\nEnter your option";
cout<<"\n1.Enter the contents\n2.load new file\n3.Exit\n";
cin>>n;
switch(n)
{
case 1:cout<<"\n \t OUTPUT\n";
for(int i=0;i<max_byte;i++)
{
cout<<f<<"\t->\t"<<m.data[i]<<"\n";
f++;
}
goto start;
case 2:clrscr();
cout<<"\nEnter the starting addr: ";
cin>>h.start_add;
cout<<"length : ";
cin>>h.length;
max_byte=h.length;
for(int r=0;r<h.length;r++)
{
cout<<"\nEnter the byte"<<r+1<<":\t";
cin>>h.data[r];
}
cout<<"\n\nEnter the starting address for loader ";
cin>>start_add;
m.memloc=start_add;
m.length=max_byte;
f=m.memloc;
for(int j=0;j<max_byte;j++)
{
m.data[j]=h.data[j];
}
goto start;
case 3:exit(0);
default:cout<<"\nWrong choice do u want to continue (y / n) : ";
cin>>ch;
if(ch=='Y' || ch=='y')
goto start;
else
exit(0);
}
getch();
}
/* Program on lexical analysis */

#include<stdio.h>
#include<conio.h>
#define MAX 30

void main()
{
char str[MAX];
int state=0;
int i=0, j, startid=0, endid, startcon, endcon;

clrscr();

for(j=0; j<MAX; j++)


str[j]=NULL; //Initialise NULL

printf("\n\n*** Program on Lexical Analysis ***");


printf("\nEnter the string: ");
gets(str); //Accept input string
str[strlen(str)]=' ';

printf("\nAnalysis:");

while(str[i]!=NULL)
{
while(str[i]==' ') //To eliminate spaces
i++;
switch(state)
{
case 0: if(str[i]=='i') state=1; //if
else if(str[i]=='w') state=3; //while
else if(str[i]=='d') state=8; //do
else if(str[i]=='e') state=10; //else
else if(str[i]=='f') state=14; //for
else if(isalpha(str[i]) || str[i]=='_')
{
state=17;
startid=i;
} //identifiers

else if(str[i]=='<') state=19;


//relational '<' or '<='

else if(str[i]=='>') state=21;


//relational '>' or '>='

else if(str[i]=='=') state=23;


//relational '==' or assignment '='

else if(isdigit(str[i]))
{
state=25; startcon=i;
}
//constant

else if(str[i]=='(') state=26;


//special characters '('

else if(str[i]==')') state=27;


//special characters ')'

else if(str[i]==';') state=28;


//special characters ';'

else if(str[i]=='+') state=29;


//operator '+'

else if(str[i]=='-') state=30;


//operator '-'

break;

//States for 'if'


case 1: if(str[i]=='f') state=2;
else { state=17; startid=i-1; i--; }
break;
case 2: if(str[i]=='(' || str[i]==NULL)
{
printf("\nif : Keyword");
state=0;
i--;
}
else { state=17; startid=i-2; i--; }
break;

//States for 'while'


case 3: if(str[i]=='h') state=4;
else { state=17; startid=i-1; i--; }
break;
case 4: if(str[i]=='i') state=5;
else { state=17; startid=i-2; i--; }
break;
case 5: if(str[i]=='l') state=6;
else { state=17; startid=i-3; i--; }
break;
case 6: if(str[i]=='e') state=7;
else { state=17; startid=i-4; i--; }
break;
case 7: if(str[i]=='(' || str[i]==NULL)
{
printf("\nwhile : Keyword");
state=0;
i--;
}
else { state=17; startid=i-5; i--; }
break;
//States for 'do'
case 8: if(str[i]=='o') state=9;
else { state=17; startid=i-1; i--; }
break;
case 9: if(str[i]=='{' || str[i]==' ' || str[i]==NULL ||
str[i]=='(')
{
printf("\ndo : Keyword");
state=0;
i--;
}
break;

//States for 'else'


case 10: if(str[i]=='l') state=11;
else { state=17; startid=i-1; i--; }
break;
case 11: if(str[i]=='s') state=12;
else { state=17; startid=i-2; i--; }
break;
case 12: if(str[i]=='e') state=13;
else { state=17; startid=i-3; i--; }
break;
case 13: if(str[i]=='{' || str[i]==NULL)
{
printf("\nelse : Keyword");
state=0;
i--;
}
else { state=17; startid=i-4; i--; }
break;

//States for 'for'


case 14: if(str[i]=='o') state=15;
else { state=17; startid=i-1; i--; }
break;
case 15: if(str[i]=='r') state=16;
else { state=17; startid=i-2; i--; }
break;
case 16: if(str[i]=='(' || str[i]==NULL)
{
printf("\nfor : Keyword");
state=0;
i--;
}
else { state=17; startid=i-3; i--; }
break;

//States for identifiers


case 17:

if(isalnum(str[i]) || str[i]=='_')
{
state=18; i++;
}
else if(str[i]==NULL||str[i]=='<'||str[i]=='>'||str[i]=='('||str[i]==')'||
str[i]==';'||str[i]=='='||str[i]=='+'||str[i]=='-') state=18;
i--;
break;

case 18:

if(str[i]==NULL || str[i]=='<' || str[i]=='>' || str[i]=='(' ||


str[i]==')' || str[i]==';' || str[i]=='=' || str[i]=='+' ||str[i]=='-')
{
endid=i-1;
printf("\n ");
for(j=startid; j<=endid; j++)
printf("%c", str[j]);
printf("\t : Identifier\n");
state=0;
i--;
}
break;

//States for relational operator '<' & '<='


case 19: if(str[i]=='=') state=20;
else if(isalnum(str[i]) || str[i]=='_')
{
printf("\t< : Relational operator\n");
i--;
state=0;
}
break;
case 20: if(isalnum(str[i]) || str[i]=='_')
{
printf("\t<= : Relational operator\n");
i--;
state=0;
}
break;

//States for relational operator '>' & '>='


case 21: if(str[i]=='=') state=22;
else if(isalnum(str[i]) || str[i]=='_')
{
printf("\t> : Relational operator\n");
i--;
state=0;
}
break;
case 22: if(isalnum(str[i]) || str[i]=='_')
{
printf("\t>= : Relational operator\n");
i--;
state=0;
}
break;
//States for relational operator '==' & assignment operator '='
case 23: if(str[i]=='=') state=24;
else
{
printf("\t= : Assignment operator\n");
i--;
state=0;
}
break;
case 24: if(isalnum(str[i]))
{
printf("\t== : Relational operator\n");
state=0;
i--;
}
break;

//States for constants


case 25: if(isalpha(str[i]))
{
printf("\n*** ERROR ***");
puts(str);
for(j=0; j<i; j++)
printf(" ");
printf("^");
printf("Error at position %d Alphabet cannot follow
digit", i);
state=99;
}
else if(str[i]=='(' || str[i]==')' || str[i]=='<' || str[i]=='>' ||
str[i]==NULL || str[i]==';' || str[i]=='=')
{
endcon=i-1;
printf(" ");
for(j=startcon; j<=endcon; j++)
printf("%c", str[j]);
printf(" : Constant");
state=0;
i--;
}
break;

//State for special character '('


case 26: printf("( : Special character");
startid=i;
state=0;
i--;
break;

//State for special character ')'


case 27: printf(") : Special character");
state=0;
i--;
break;

//State for special character ';'


case 28: printf("; : Special character");
state=0;
i--;
break;

//State for operator '+'


case 29: printf("+ : Operator");
state=0;
i--;
break;

//State for operator '-'


case 30: printf("+ : Operator");
state=0;
i--;
break;

//Error State
case 99: goto END;
}
i++;
}
printf("\nEnd of program");
END:
getch();
}

/* Output
Correct input
-------------

*** Program on Lexical Analysis ***

Enter the string: for(x1=0; x1<=10; x1++);

Analysis:

for : Keyword
( : Special character
x1 : Identifier
= : Assignment operator
0 : Constant
; : Special character
x1 : Identifier
<= : Relational operator
10 : Constant
; : Special character
x1 : Identifier
+ : Operator
+ : Operator
) : Special character
; : Special character

*/
/*SHIFT REDUCE PARSER*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct Grammer
{
char prod[10],newProd[10],ps[20],is[20];
}SR[10];
int reduce(int i,int n,char a[10],char b[10],int flag)
{
int j,k,m,l,count=0;
char s1[10];
strcpy(s1,"");
k=0;
l=strlen(a);
strcpy(s1,a);
strcat(s1,'\0');
flag=0;
count=0;
while(l>0)
{
for(m=0;m<n;m++)
{
if(strcmp(s1,SR[m].newProd)==0)
{
if(flag==1)
flag=0;
flag=1;
k=count;
for(j=k;a[j]!='\0';j++)
a[j]=' ';
a[k]=SR[m].prod[0];
a[k+1]=a[k+2]='\0';
printf("\n$%s\tReduced%s",a,b,SR[m].prod);
}
}
l--;
count++;
if(flag==0||l>=0)
{
k=0;
while(s1[k]!='\0')
{
s1[k]=s1[k+1];
k++;
}
}
}
if(flag==1)
return reduce(i,n,a,b,flag);
else return i;
}
void main()
{
int i,n,j,k,l,m,p,flag,no_tok;
char input[10],s1[10],ch[2];
clrscr();
flag=0;
strcpy(s1,"");
printf("\nEnter the no of tokens: ");
scanf("%d",&no_tok);
printf("\nEnter the no of productions: ");
scanf("%d",&n);
printf("\n Enter the productions: ");
for(i=0;i<n;i++)
scanf("%s",SR[i].prod);
printf("\nEnter the input string: ");
scanf("%s",input);
for(i=0;i<n;i++)
{
k=0;
for(j=3;SR[i].prod[j]!='\0';j++)
SR[i].newProd[k++]=SR[i].prod[j];
}
strcpy(SR[0].is,input);
printf("\nOperational \tINPUT\tACTION");
printf("\nStack\t\t\tstack");
printf("\n$%s\t\t%s",SR[0].ps,SR[0].is);
i=j=k=0;
while(1)
{
ch[0]=SR[i].is[0];
ch[1]='\0';
strcat(SR[i+1].ps,ch);
strcat(SR[i+1].ps,'\0');
k=0;
while(SR[i].is[k]!='\0')
{
SR[i+1].is[k]=SR[i].is[k+1];
k++;
}
printf("\n$%s\t\t%s\tShifted",SR[i+1].ps,SR[i+1].is);
i=reduce(i,n,SR[i+1].ps,SR[i+1].is,flag);
i++;k=0;
while(SR[i].ps[k]!='\0')
{
SR[i+1].ps[k]=SR[i].ps[k];
k++;
}
if(SR[i].ps[0]==SR[0].prod[0]&&SR[i].is[0]=='\0'&&strcmp(SR[i].ps,"e")==0)
{
printf("\nAccepted");
break;
}
else
if(i>no_tok-1)
{
printf("\nRejected");
break;
}
}
getch();
}
Output :
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<iostream.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
FILE *fp1,*fp2;
char fname[10],op[10],ch;
char operand1[18],operand2[8],result[8];
int i=0;
clrscr();
cout<<"a=b*(c+d)";
printf("\n\nEnter filename of the equivalent intermediate code:");
scanf("%s",&fname);
fp1=fopen("FNAME.TXT","r");
fp2=fopen("TARGET.TXT","w");
if(fp1==NULL || fp2==NULL)
{
printf("Error opening file");
getch();
exit(0);
}
while(!feof(fp1))
{
fprintf(fp2,"\n");
fscanf(fp1,"%s",op);
i++;
switch(op[0])
{
case '*':
fscanf(fp1,"%s%s%s",operand1,operand2,result);
fprintf(fp2,"\n\t MOV ax,%s",operand1);
fprintf(fp2,"\n\t MOV bx,%s",operand2);
fprintf(fp2,"\n\t MUL ax,bx");
fprintf(fp2,"\n\t MOV %s,ax",result);
break;
case '+':
fscanf(fp1,"%s%s%s",operand1,operand2,result);
fprintf(fp2,"\n\t MOV ax,%s",operand1);
fprintf(fp2,"\n\t MOV bx,%s",operand2);
fprintf(fp2,"\n\t ADD ax,bx");
fprintf(fp2,"\n\t MOV %s,ax",result);
break;
case '-':
fscanf(fp1,"%s%s%s",operand1,operand2,result);
fprintf(fp2,"\n\t MOV ax,%s",operand1);
fprintf(fp2,"\n\t MOV bx,%s",operand2);
fprintf(fp2,"\n\t SUB ax,bx");
fprintf(fp2,"\n\t MOV %s,ax",result);
break;
case '/':
fscanf(fp1,"%s%s%s",operand1,operand2,result);
fprintf(fp2,"\n\t MOV ax,%s",operand1);
fprintf(fp2,"\n\t MOV bx,%s",operand2);
fprintf(fp2,"\n\t DIV ax,bx");
fprintf(fp2,"\n\t MOV %s,ax",result);
break;

case '=':
fscanf(fp1,"%s%s%s",operand1,operand2,result);
fprintf(fp2,"\n\t MOV %s,%s",result,operand1);
break;

}
}
fclose(fp2);
fclose(fp1);
/*fopen("TARGET.TXT","r");
if(fp2==NULL)
{
printf("ERROR OPENING THE FILE");
getch();
exit(0);
}
do
{
ch=fgetc(fp2);
printf("%c",ch);
}
while(ch!=EOF);
fclose(fp2);*/
getch();
}
MOV ax,C
MOV bx,D
ADD ax,bx
MOV T2,ax

MOV ax,T1
MOV bx,T2
MUL ax,bx
MOV T3,ax

MOV A,T3
B T1
+ C D T2
* T1 T2 T3
= T3 A

You might also like