Download as pdf or txt
Download as pdf or txt
You are on page 1of 73

1.

Implement a menu driven simulator for hypothetical Simple Instruction Computer that
provides the following functionalities.
o Load – Loading of the program from file into memory
o Print – Printing the program loaded in memory.
o Run – Executing the loaded program
#include<stdio.h>
#include<stdlib.h>
int mem[100],reg[4],cond[]={0,0,0,0,0,1},opc,op1,op2,pc;
FILE *fp;
char fname[20];

void load()
{
printf("Enter file name:");
scanf("%s",fname);

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fscanf(fp,"%d",&mem[pc])!=-1)
pc++;

fclose(fp);
}

void print()
{
int i;
for(i=0;i<pc;i++)
printf("%06d\n",mem[i]);
}

void run()
{
int i;
pc = 0;
while(1)
{
opc = mem[pc]/10000;
op1 = mem[pc]%10000/1000-1;
op2 = mem[pc]%1000;

switch(opc)
{
case 0: // STOP
return;
case 1: // ADD
reg[op1]+=mem[op2];
break;
case 2: // SUB
reg[op1]-=mem[op2];
break;
case 3: // MULT
reg[op1]*=mem[op2];
break;
case 8: // DIV
reg[op1]/=mem[op2];
break;
case 4: // MOVER
reg[op1]=mem[op2];
break;
case 5: // MOVEM
mem[op2]=reg[op1];
break;
case 6: // COMP
if(reg[op1] < mem[op2])
cond[0]=1;
if(reg[op1] <= mem[op2])
cond[1]=1;
if(reg[op1] == mem[op2])
cond[2]=1;
if(reg[op1] > mem[op2])
cond[3]=1;
if(reg[op1] >= mem[op2])
cond[4]=1;
break;
case 7: // BC
if(cond[op1]==1)
pc = op2-1;

for(i=0;i<5;i++)
cond[i]=0;
break;
case 9: // READ
scanf("%d",&mem[op2]);
break;
case 10:// PRINT
printf("%d\n",mem[op2]);
}
pc++;
}
}
int main()
{
int ch;

while(1)
{
printf("1.Load\n2.Print\n3.Run\n4.Exit\n");
printf("Enter your choice (1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1:
load();
break;
case 2:
print();
break;
case 3:
run();
break;
case 4:
exit(0);
}
}

return 0;
}

2) Write a command line program for line editor. The file to be edited is taken as command line
argument; an empty file is opened for editing if no argument is supplied. It should display a „$‟
prompt to accept the line editing commands. Implement the following commands.
o a - Append
o p - Print all Lines
o s - Save
o e - Exit

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node


{
char line[80];
struct node *next;
}NODE;
NODE *first,*last;
int len,changed;

NODE * get_node(char *s)


{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->line,s);
p->next=NULL;
return p;
}

void create(char fname[])


{
NODE *p;
FILE *fp;
char buff[80];

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
p = get_node(buff);

if(first==NULL)
first = p;
else
last->next = p;

last = p;
len++;
}

fclose(fp);
}

void append()
{
NODE *p;
char buff[80];

printf("Enter text (type END to stop):");


fflush(stdin);
fgets(buff,80,stdin);

while(strcmp(buff,"END\n")!=0)
{
p = get_node(buff);
if(first==NULL)
first = p;
else
last->next = p;
last=p;
len++;

fflush(stdin);
fgets(buff,80,stdin);
}

changed=1;
}

void print()
{
NODE *p;
int i=1;

p=first;
while(p!=NULL)
{
printf("%d:%s",i,p->line);
i++;
p=p->next;
}
}

void save(char fname[])


{
FILE *fp;
NODE *p;

fp = fopen(fname,"w");

p = first;
while(p!=NULL)
{
fputs(p->line,fp);
p=p->next;
}

fclose(fp);
changed=0;
}

int main(int argc, char *argv[])


{
char buff[80],ch,fname[30]="";

if(argc==2)
{
strcpy(fname,argv[1]);
create(fname);
}

while(1)
{
printf("$");
fflush(stdin);
fgets(buff,80,stdin);

sscanf(buff,"%c",&ch);

switch(ch)
{
case 'a':
append();
break;
case 'p':
print();
break;
case 's':
if(changed==1)
{
if(strlen(fname)==0)
{
printf("Enter source file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
break;
case 'e':
if(changed==1)
{
printf("Quit without save (Y/N)?");
fflush(stdin);
fgets(buff,80,stdin);
if(buff[0]=='N')
{
if(strlen(fname)==0)
{
printf("Enter file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
}
exit(0);
default:
printf("Invalid command.\n");
}
}

return 0;
}

3) Write a command line program for line editor. The file to be edited is taken as command line
argument. An empty file is opened for editing if no argument is supplied. It should display „$‟ prompt
to accept the line editing commands. Implement the following commands:
o a - Append
o d n- Delete Nth Line
o p - Print all Lines
o e - Exit

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node


{
char line[80];
struct node *next;
}NODE;

NODE *first,*last;
int len,changed;

NODE * get_node(char *s)


{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->line,s);
p->next=NULL;
return p;
}

void create(char fname[])


{
NODE *p;
FILE *fp;
char buff[80];

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
p = get_node(buff);

if(first==NULL)
first = p;
else
last->next = p;

last = p;
len++;
}

fclose(fp);
}

void append()
{
NODE *p;
char buff[80];

printf("Enter text (type END to stop):");

fflush(stdin);
fgets(buff,80,stdin);

while(strcmp(buff,"END\n")!=0)
{
p = get_node(buff);
if(first==NULL)
first = p;
else
last->next = p;
last=p;
len++;

fflush(stdin);
fgets(buff,80,stdin);
}

changed=1;
}

void print()
{
NODE *p;
int i=1;

p=first;
while(p!=NULL)
{
printf("%d:%s",i,p->line);
i++;
p=p->next;
}
}

void del(int pos)


{
NODE *p,*q;
int i=1;

p = q = first;
while(i<pos)
{
q = p;
p = p->next;
i++;
}

if(p==first)
{
first = p->next;
}
else if(p==last)
{
q->next=NULL;
last=q;
}
else
{
q->next=p->next;
}

free(p);
len--;
changed=1;
}

int main(int argc, char *argv[])


{
char buff[80],ch,fname[30]="";
int n;

if(argc==2)
{
strcpy(fname,argv[1]);
create(fname);
}

while(1)
{
printf("$");
fflush(stdin);
fgets(buff,80,stdin);

sscanf(buff,"%c %d",&ch,&n);

switch(ch)
{
case 'a':
append();
break;
case 'p':
print();
break;
case 'd':
if(n>=1 && n<=len)
del(n);
else
printf("Invalid position: %d\n",n);
break;
case 'e':
exit(0);
default:
printf("Invalid command.\n");
}
}

return 0;
}
4) Write a command line program for line editor. The file to be edited is taken as command line
argument. An empty file is opened for editing if no argument is supplied. It should display „$‟ prompt
to accept the line editing commands. Implement the following commands.
o s - Save
o a - Append
o d n1 n2 - Delete lines from position n1 to n2
o e - Exit

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node


{
char line[80];
struct node *next;
}NODE;

NODE *first,*last;
int len,changed;

NODE * get_node(char *s)


{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->line,s);
p->next=NULL;
return p;
}

void create(char fname[])


{
NODE *p;
FILE *fp;
char buff[80];

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
p = get_node(buff);

if(first==NULL)
first = p;
else
last->next = p;

last = p;
len++;
}

fclose(fp);
}

void append()
{
NODE *p;
char buff[80];

printf("Enter text (type END to stop):");

fflush(stdin);
fgets(buff,80,stdin);

while(strcmp(buff,"END\n")!=0)
{
p = get_node(buff);
if(first==NULL)
first = p;
else
last->next = p;
last=p;
len++;

fflush(stdin);
fgets(buff,80,stdin);
}

changed=1;
}

void save(char fname[])


{
FILE *fp;
NODE *p;

fp = fopen(fname,"w");

p = first;
while(p!=NULL)
{
fputs(p->line,fp);
p=p->next;
}

fclose(fp);
changed=0;
}

void del1(int pos)


{
NODE *p,*q;
int i=1;

p = q = first;
while(i<pos)
{
q = p;
p = p->next;
i++;
}

if(p==first)
{
first = p->next;
}
else if(p==last)
{
q->next=NULL;
last=q;
}
else
{
q->next=p->next;
}

free(p);
len--;
changed=1;
}

void del(int n1, int n2)


{
int n,i;
n=n2-n1+1;
for(i=1;i<=n;i++)
del1(n1);
}

int main(int argc, char *argv[])


{
char buff[80],ch,fname[30]="";
int n1,n2;

if(argc==2)
{
strcpy(fname,argv[1]);
create(fname);
}

while(1)
{
printf("$");
fflush(stdin);
fgets(buff,80,stdin);

sscanf(buff,"%c %d %d",&ch,&n1,&n2);

switch(ch)
{
case 'a':
append();
break;
case 's':
if(changed==1)
{
if(strlen(fname)==0)
{
printf("Enter source file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
break;
case 'd':
if(n1>=1 && n2<=len && n1<=n2)
del(n1,n2);
else
printf("Invalid parameter\n");
break;
case 'e':
if(changed==1)
{
printf("Quit without save (Y/N)?");
fflush(stdin);
fgets(buff,80,stdin);
if(buff[0]=='N')
{
if(strlen(fname)==0)
{
printf("Enter file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
}
exit(0);
default:
printf("Invalid command.\n");
}
}

return 0;
}

5) Write a program to implement a DFA Driver for any given language. Accept number of states,
number of input symbols, set of input symbols, number of final states, set of final states and
transition table as input. (Input language should be given by examiner). Write menu driven program
that will have options as:
o Read DFA
o Show Transition Table
o Check Acceptance of the Given String
o Exit

#include<stdio.h>
#include<stdlib.h>
#define MAX 10

int nos,noi,nof,finals[MAX],trans[MAX][MAX];
char input_symb[MAX],input_str[100];

void read_dfa()
{
int i,j;

printf("Enter no.of states:");


scanf("%d",&nos);

printf("Enter no.of input symbols:");


scanf("%d",&noi);
printf("Enter input symbols:");

scanf("%s",input_symb);

printf("Enter no.of finals:");


scanf("%d",&nof);

printf("Enter finals states:");


for(i=0;i<nof;i++)
scanf("%d",&finals[i]);

printf("Enter transition table:\n");

for(i=0;i<nos;i++)
{
for(j=0;j<noi;j++)
{
printf("d(q%d,%c)=",
i,input_symb[j]);
scanf("%d",&trans[i][j]);
}
}
}

void show_trans()
{
int i,j;

printf("M=(Q,E,d,q0,F)\n");

printf("Q={");
for(i=0;i<nos;i++)
printf("q%d,",i);
printf("\b}\nE={");
for(i=0;i<noi;i++)
printf("%c,",input_symb[i]);
printf("\b}\nq0=q0\nF={");
for(i=0;i<nof;i++)
printf("q%d,",finals[i]);
printf("\b}\nd:\n\t");

for(i=0;i<noi;i++)
printf("%c\t",input_symb[i]);

printf("\n");

for(i=0;i<nos;i++)
{
printf("q%d\t",i);
for(j=0;j<noi;j++)
printf("q%d\t",trans[i][j]);
printf("\n");
}
}

void check()
{
int i,j,curr;

printf("Enter input string:");


scanf("%s",input_str);

curr = 0;
for(i=0;input_str[i]!='\0';i++)
{
printf("d(q%d,%s)\n",curr,input_str+i);
curr = trans[curr][input_str[i]-input_symb[0]];
}

printf("q%d\n",curr);

for(i=0;i<nof;i++)
{
if(finals[i]==curr)
{
printf("Accept\n");
return;
}
}

printf("Reject\n");
}

int main()
{
int ch;
while(1)
{
printf("1.Read DFA\n2.Show Transition Table\n3.Check acceptance of given string\n4.Exit\n");
printf("Enter choice (1-4):");
scanf("%d",&ch);

switch(ch)
{
case 1:
read_dfa();
break;
case 2:
show_trans();
break;
case 3:
check();
break;
case 4:
exit(0);
}
}

return 0;
}

6) Write a program to implement DFA driver for the language L = “Set of all string that starts with a,
ending with b” over {a,b}.

#include<stdio.h>

int nos=4;
int noi=2;
char input_symb[]={'a','b'};
int nof=1,final=2;
int d[][2]={{1,3},{1,2},{1,2},{3,3}};
char input_str[100];

int main()
{
int i,j,curr;

printf("M=(Q,E,d,q0,F)\n");
printf("Q={");
for(i=0;i<nos;i++)
printf("q%d,",i);
printf("\b}\nE={");
for(i=0;i<noi;i++)
printf("%c,",input_symb[i]);
printf("\b}\nq0=q0\nF={q%d}\nd:\n\t",final);

for(i=0;i<noi;i++)
printf("%c\t",input_symb[i]);
printf("\n");

for(i=0;i<nos;i++)
{
printf("q%d\t",i);
for(j=0;j<noi;j++)
printf("q%d\t",d[i][j]);
printf("\n");
}

do
{
printf("Enter input string:");
scanf("%s",input_str);

curr = 0;
for(i=0;input_str[i]!='\0';i++)
{
printf("d(q%d,%s)\n",curr,
input_str+i);
curr=d[curr][input_str[i]-input_symb[0]];
}

printf("q%d\n",curr);

if(curr==final)
printf("Accept\n");
else
printf("Reject\n");

printf("Continue Y(1)/N(0)?");
scanf("%d",&j);
}while(j==1);

return 0;
}

7) Write a command line program for line editor. The file to be edited is taken as command line
arguments. An empty file is opened for editing if no argument is supplied. It should display a „$‟
prompt to accept the line editing commands. Implement the following commands:
o a - Append
o p m n - Print range of lines from m to n
o i n - Insert line at position n
o e - Exit
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node


{
char line[80];
struct node *next;
}NODE;

NODE *first,*last;
int len,changed;

NODE * get_node(char *s)


{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->line,s);
p->next=NULL;
return p;
}

void create(char fname[])


{
NODE *p;
FILE *fp;
char buff[80];

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
p = get_node(buff);

if(first==NULL)
first = p;
else
last->next = p;

last = p;
len++;
}

fclose(fp);
}

void append()
{
NODE *p;
char buff[80];

printf("Enter text (type END to stop):");

fflush(stdin);
fgets(buff,80,stdin);
while(strcmp(buff,"END\n")!=0)
{
p = get_node(buff);
if(first==NULL)
first = p;
else
last->next = p;
last=p;
len++;

fflush(stdin);
fgets(buff,80,stdin);
}

changed=1;
}

void insert(int pos)


{
NODE *first1=NULL,*last1,*p,*q;
int i=0;
char buff[80];

printf("Enter text to insert (type END to stop):");

fflush(stdin);
fgets(buff,80,stdin);

while(strcmp(buff,"END\n")!=0)
{
p = get_node(buff);
if(first1==NULL)
first1=p;
else
last1->next=p;
last1=p;
i++;

fflush(stdin);
fgets(buff,80,stdin);
}
len+=i;
p = q = first;
i=1;
while(i<pos)
{
i++;
q=p;
p=p->next;
}

if(p==first)
{
last1->next=first;
first=first1;
}
else
{
q->next=first1;
last1->next=p;
}

changed=1;
}

void print(int n1, int n2)


{
int i;
NODE *p;

i=1;
p=first;
while(i<n1)
{
i++;
p=p->next;
}

i=n1;
while(i<=n2)
{
printf("%d: %s",i,p->line);
i++;
p=p->next;
}
}

int main(int argc, char *argv[])


{
char buff[80],ch,fname[30]="";
int n1,n2;

if(argc==2)
{
strcpy(fname,argv[1]);
create(fname);
}
while(1)
{
printf("$");
fflush(stdin);
fgets(buff,80,stdin);

sscanf(buff,"%c %d %d",&ch,&n1,&n2);

switch(ch)
{
case 'a':
append();
break;
case 'p':
if(n1>=1 && n2<=len && n1<=n2)
print(n1,n2);
else
printf("Invalid parameter\n");
break;
case 'i':
if(n1>=1 && n1<=len)
insert(n1);
else
printf("Invalid parameter\n");
break;
case 'e':
exit(0);
default:
printf("Invalid command.\n");
}
}

return 0;
}

8) Write a program to implement a DFA driver for the language L = “Set of all strings that containing
101 as substring” over {0,1}.

#include<stdio.h>

int nos=4;
int noi=2;
char input_symb[]={'0','1'};
int nof=1,final=3;
int d[][2]={{0,1},{2,1},{0,3},{3,3}};
char input_str[100];
int main()
{
int i,j,curr;

printf("M=(Q,E,d,q0,F)\n");
printf("Q={");
for(i=0;i<nos;i++)
printf("q%d,",i);
printf("\b}\nE={");
for(i=0;i<noi;i++)
printf("%c,",input_symb[i]);
printf("\b}\nq0=q0\nF={q%d}\nd:\n\t",final);

for(i=0;i<noi;i++)
printf("%c\t",input_symb[i]);
printf("\n");

for(i=0;i<nos;i++)
{
printf("q%d\t",i);
for(j=0;j<noi;j++)
printf("q%d\t",d[i][j]);
printf("\n");
}

do
{
printf("Enter input string:");
scanf("%s",input_str);

curr = 0;
for(i=0;input_str[i]!='\0';i++)
{
printf("d(q%d,%s)\n",curr,
input_str+i);
curr=d[curr][input_str[i]-input_symb[0]];
}

printf("q%d\n",curr);

if(curr==final)
printf("Accept\n");
else
printf("Reject\n");

printf("Continue Y(1)/N(0)?");
scanf("%d",&j);
}while(j==1);
return 0;
}

9) Consider the following Macro definition and Data structures (stored as an array of structure)
association with it as follows:

INPUT FILE:

CALC X,Y
CALC P,Q,&OP=SUB

PROGRAM:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct MDT
{
char op[10],value[30];
}mdt[]={
{"MOVER","AREG, (P, 1)"},
{"(P, 3)", "AREG, (P, 2)"},
{"MOVEM", "AREG, (P, 1)"},
{"MEND"}};

char pnt[][10]={"&A","&B","&OP"};

struct MNT
{
char mname[10];
int mdtp,kpdtp,pntp,kp,pp;
}mnt={"CALC",0,0,0,1,2};

struct KPDT
{
char key[10],def[10];
}kpdt={"&OP","ADD"};

int pnt_cnt;

void make_apt(char *s)


{
int i=0,j=0;
char temp[10];

pnt_cnt=0;
strcat(s,",");

while(s[i]!='\0' && s[i]!='=')


{
if(s[i]==',')
{
temp[j]='\0';
j=0;
strcpy(pnt[pnt_cnt++],temp);
}
else
{
temp[j++]=s[i];
}
i++;
}

while(s[i]!='\0')
{
if(s[i]=='=')
{
j=0;
}
else if(s[i]==',')
{
temp[j]='\0';
j=0;
strcpy(pnt[mnt.pp],temp);
}
else
{
temp[j++]=s[i];
}
i++;
}
}

void expand()
{
int mec;
char t1[10],t2[10],t3[10];

mec = mnt.mdtp;
while(strcmp(mdt[mec].op,"MEND")!=0)
{
if(mdt[mec].op[0]=='(')
{
sscanf(mdt[mec].op,"%s %s",t1,t2);
printf("+%s ",pnt[atoi(t2)-1]);
}
else
{
printf("+%s ",mdt[mec].op);
}

sscanf(mdt[mec].value,"%s %s %s",t1,t2,t3);
printf("%s %s\n",t1,pnt[atoi(t3)-1]);

mec++;
}
}

int main()
{
FILE *fp;
char fname[20],buff[80],t1[30],t2[30];

printf("Enter file name:");


scanf("%s",fname);
fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
sscanf(buff,"%s %s",t1,t2);
strcpy(pnt[mnt.pp],kpdt.def);
make_apt(t2);
expand();
}

fclose(fp);

return 0;
}

10) Write a macro pre-processor program that will create and display the contents of MDT, MNT and
PNTAB for the following macro definition:

INPUT FILE:

MACRO
CALC &A,&B,&REG,&OP
MOVER &REG, &A
&OP &REG, &B
MOVEM &REG, &A
MEND

PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct MNT
{
char mname[20];
int mdtp,pp;
}mnt;

struct MDT
{
char op[20],value[50];
}mdt[10];

char pnt[10][10];

int mdt_cnt,pnt_cnt;

int search(char *s)


{
int i;
for(i=0;i<pnt_cnt;i++)
if(strcmp(pnt[i],s)==0)
return i;

return -1;
}

void print_mnt()
{
printf("#\tmname\tmdtp\tpp\n");
printf("1\t%s\t%d\t%d\n",
mnt.mname,mnt.mdtp,mnt.pp);
}

void print_pnt()
{
int i;

printf("#\tpname\n");

for(i=0;i<pnt_cnt;i++)
printf("%d\t%s\n",i+1,pnt[i]);
}

void print_mdt()
{
int i;

printf("#\topcode\tvalue\n");
for(i=0;i<mdt_cnt;i++)
printf("%d\t%s\t%s\n",
i+1,mdt[i].op,mdt[i].value);
}

void make_pnt(char *s)


{
char temp[10];
int i=0,j=0;

strcat(s,",");

while(s[i]!='\0')
{
if(s[i]==',')
{
temp[j]='\0';
j=0;
strcpy(pnt[pnt_cnt++],temp);
}
else
temp[j++]=s[i];

i++;
}
}

int main()
{
FILE *fp;
char fname[20],buff[80],t1[20],t2[20],t3[20];
int i,j;

printf("Enter source file:");


scanf("%s",fname);

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
sscanf(buff,"%s %s %s",t1,t2,t3);

if(strcmp(t1,"MACRO")==0)
{
fgets(buff,80,fp);
sscanf(buff,"%s %s",t1,t2);
strcpy(mnt.mname,t1);
make_pnt(t2);
mnt.pp = pnt_cnt;
mnt.mdtp = 1;
}
else if(strcmp(t1,"MEND")==0)
{
strcpy(mdt[mdt_cnt++].op,t1);
}
else
{
if(t1[0]=='&')
{
i = search(t1);
sprintf(mdt[mdt_cnt].op,"(P, %d)",i+1);
}
else
{
strcpy(mdt[mdt_cnt].op,t1);
}

t2[strlen(t2)-1]='\0';
i = search(t2);
j = search(t3);
sprintf(mdt[mdt_cnt++].value, "(P, %d), (P, %d)",i,j+1);
}
}

fclose(fp);
print_mnt();
print_pnt();
print_mdt();

return 0;
}

Write a macro pre-processor program that will create and display the contents of MNT, MDT and
KPDTAB for the following macro definition

INPUT FILE:
MACRO
INCR &X=,&Y=,&OP=ADD,&REG=AREG
MOVER &REG, &X
&OP &REG, &Y
MOVEM &REG, &X
MEND

PROGRAM:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct MNT
{
char mname[10];
int mdtp,kpdtp,pp,kp;
}mnt;

struct MDT
{
char op[10],value[30];
}mdt[10];

struct KPDT
{
char key[10],def[10];
}kpdt[5];

char pnt[10][10];

int pnt_cnt,mdt_cnt,kpdt_cnt;

void print_mnt()
{
printf("#\tmname\tmdtp\tkpdtp\t#pp\t#kp\n");

printf("1\t%s\t%d\t%d\t%d\t%d\n",
mnt.mname,mnt.mdtp,mnt.kpdtp,mnt.pp,mnt.kp);
}

void print_mdt()
{
int i;

printf("#\topcode\tvalue\n");

for(i=0;i<mdt_cnt;i++)
printf("%d\t%s\t%s\n",
i+1,mdt[i].op,mdt[i].value);
}

void print_kpdt()
{
int i;

printf("#\tkey\tdef\n");

for(i=0;i<kpdt_cnt;i++)
printf("%d\t%s\t%s\n",
i+1,kpdt[i].key,kpdt[i].def);
}

int search(char *s)


{
int i;

for(i=0;i<pnt_cnt;i++)
if(strcmp(pnt[i],s)==0)
return i;

return -1;
}

void make_pnt_kpdt(char *s)


{
int i=0,j=0,k=0,f=0;
char temp1[10],temp2[10]="";

strcat(s,",");

while(s[i]!='\0')
{
if(s[i]=='=')
{
temp1[j]='\0';
j=0;
f=1;
}
else if(s[i]==',')
{
temp2[k]='\0';
k=0;
f=0;
strcpy(pnt[pnt_cnt++],temp1);
if(strlen(temp2)>0)
{
strcpy(kpdt[kpdt_cnt].key,temp1);
strcpy(kpdt[kpdt_cnt++].def,temp2);
}
}
else
{
if(f==0)
temp1[j++]=s[i];
else
temp2[k++]=s[i];
}

i++;
}

mnt.pp = pnt_cnt - kpdt_cnt;


mnt.kp = kpdt_cnt;
}

int main()
{
FILE *fp;
char fname[20],buff[80],t1[20],t2[20],t3[20];
int i,j;

printf("Enter source file name:");


scanf("%s",fname);

fp=fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
sscanf(buff,"%s %s %s",t1,t2,t3);

if(strcmp(t1,"MACRO")==0)
{
fgets(buff,80,fp);
sscanf(buff,"%s %s",t1,t2);
strcpy(mnt.mname,t1);
mnt.mdtp = mnt.kpdtp = 1;
make_pnt_kpdt(t2);
}
else if(strcmp(t1,"MEND")==0)
{
strcpy(mdt[mdt_cnt++].op,t1);
}
else
{
if(t1[0]=='&')
{
i = search(t1);
sprintf(mdt[mdt_cnt].op,"(P, %d)",i+1);
}
else
{
strcpy(mdt[mdt_cnt].op,t1);
}

t2[strlen(t2)-1]='\0';
i = search(t2);
j = search(t3);
sprintf(mdt[mdt_cnt++].value,"(P, %d), (P, %d)",i+1,j+1);
}
}

fclose(fp);

print_mnt();
print_kpdt();
print_mdt();

return 0;
}

11) Write an assembler that will create and display the contents of symbol table. Also construct the
Intermediate Code of type variant I for the following error-free assembly program:

INPUT FILE:

START 100
MOVEM BREG, ONE
MOVER BREG, A
MOVER BREG, B
MOVER BREG, C
PRINT A
PRINT B
PRINT C
STOP
A DS 1
B DS 1
C DS 1
ONE DC '1'
END

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node


{
char symb[20];
int pos,addr,val,len;
struct node *next;
}NODE;

int cnt=0;

NODE *first,*last;

void add_symb(char *s, int a, int v, int l)


{
NODE *p;

p = (NODE*)malloc(sizeof(NODE));
strcpy(p->symb,s);
cnt++;
p->pos = cnt;
p->addr = a;
p->val = v;
p->len = l;
p->next = NULL;

if(first==NULL)
first=p;
else
last->next=p;

last=p;
}

void display_symbtab()
{
NODE *p;

printf("#\tsymb\taddr\tval\tlen\n");

p = first;
while(p!=NULL)
{
printf("%d\t%s\t%d\t%d\t%d\n",
p->pos,p->symb,p->addr,p->val,p->len);

p=p->next;
}
}

NODE * search_symb(char *s)


{
NODE *p;

p = first;
while(p!=NULL)
{
if(strcmp(p->symb,s)==0)
return p;

p=p->next;
}

return NULL;
}

char optab[][6]={"STOP","ADD","SUB","MULT",
"MOVER","MOVEM","COMP","BC",
"DIV","READ","PRINT"};

int search_op(char *s)


{
int i;

for(i=0;i<11;i++)
{
if(strcmp(optab[i],s)==0)
return i;
}

return -1;
}

char regtab[][5]={"AREG","BREG","CREG","DREG"};

int search_reg(char *s)


{
int i;
for(i=0;i<4;i++)
{
if(strcmp(regtab[i],s)==0)
return i;
}

return -1;
}

char adtab[][7]={"START","END",
"ORIGIN","EQU","LTORG"};

int search_ad(char *s)


{
int i;
for(i=0;i<5;i++)
{
if(strcmp(adtab[i],s)==0)
return i;
}

return -1;
}

int main()
{
NODE *p;
FILE *fp,*fp1;
char fname[40],buff[80],t1[20],t2[20],t3[20],t4[20];
int n,i,j,pc;

printf("Enter source file name:");


scanf("%s",fname);

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

fp1 = fopen("temp.i","w");

while(fgets(buff,80,fp)!=NULL)
{
n = sscanf(buff,"%s %s %s %s",t1,t2,t3,t4);

switch(n)
{
case 1:
i = search_op(t1);
if(i==0) // STOP
{
fprintf(fp1,"%03d) (IS, %02d)\n",pc, i);
break;
}
i = search_ad(t1);
fprintf(fp1,"%03d) (AD, %02d)\n",pc,i+1);
break;
case 2:
i = search_ad(t1);
if(i==0) // START
{
fprintf(fp1,"%03d) (AD, %02d) (C, %s)\n", pc, i+1, t2);
pc = atoi(t2)-1;
break;
}
i = search_op(t1);
if(i==9 || i==10) // READ or PRINT
{
p = search_symb(t2);

if(p==NULL)
{
add_symb(t2,0,0,0);
fprintf(fp1,"%03d) (IS, %02d) (S, %d)\n",pc,i,cnt);
}
else
{
fprintf(fp1,"%03d) (IS, %02d) (S, %d)\n",pc,i,p->pos);
}
break;
}
p = search_symb(t1);
if(p==NULL)
{
add_symb(t1,pc,0,0);
}
else
{
p->addr = pc;
}
fprintf(fp1,"%03d) (IS, 00)\n",pc);
break;
case 3:
if(strcmp(t2,"DS")==0)
{
p = search_symb(t1);
if(p==NULL)
{
add_symb(t1,pc,0,atoi(t3));
}
else
{
p->addr = pc;
p->len = atoi(t3);
}
fprintf(fp1,"%03d) (DL, 02) (C, %s)\n",pc,t3);
pc+=atoi(t3)-1;
break;
}
if(strcmp(t2,"DC")==0)
{
p = search_symb(t1);
if(p==NULL)
{
add_symb(t1,pc,atoi(t3+1),1);
}
else
{
p->addr = pc;
p->val = atoi(t3+1);
p->len = 1;
}
fprintf(fp1,"%03d) (DL, 01) (C, %d)\n",pc, atoi(t3+1));
break;
}
i = search_op(t1);
if(i>=1 && i<=8) // ADD to DIV
{
t2[strlen(t2)-1]='\0';
j = search_reg(t2);

p = search_symb(t3);

if(p==NULL)
{
add_symb(t3,0,0,0);
fprintf(fp1,"%03d) (IS, %02d) (%d) (S, %d)\n",pc,i,j+1,cnt);
}
else
{
fprintf(fp1,"%03d) (IS, %02d) (%d) (S, %d)\n",pc,i,j+1,p->pos);
}
break;
}
i = search_op(t2);
p = search_symb(t1);
if(p==NULL)
{
add_symb(t1,pc,0,0);
}
else
{
p->addr = pc;
}
p = search_symb(t3);
if(p==NULL)
{
add_symb(t3,0,0,0);
fprintf(fp1,"%03d) (IS, %02d) (S, %d)\n",pc,i,cnt);
}
else
{
fprintf(fp1,"%03d) (IS, %02d) (S, %d)\n",pc,i,p->pos);
}
break;
case 4:
i = search_op(t2);
t3[strlen(t3)-1]='\0';
j = search_reg(t3);
p = search_symb(t1);
if(p==NULL)
{
add_symb(t1,pc,0,0);
}
else
{
p->addr = pc;
}
p = search_symb(t4);
if(p==NULL)
{
add_symb(t4,0,0,0);
fprintf(fp1,"%03d) (IS, %02d) (%d) (S, %d)\n",pc,i,j+1,cnt);
}
else
{
fprintf(fp1,"%03d) (IS, %02d) (%d) (S, %d)\n",pc,i,j+1,p->pos);
}
}
pc++;
}

fclose(fp);
fclose(fp1);

fp1 = fopen("temp.i","r");
while(fgets(buff,80,fp1)!=NULL)
printf("%s",buff);

display_symbtab();

return 0;
}

12) Write a command line program for line editor. The file to be edited is taken as command line
argument; an empty file is opened for editing if no argument is supplied. It should display a „$‟
prompt to accept the line editing commands. Implement the following commands.
o s - Save
o a - Append
o c n1 n2 - Copy line from position n1 to n2
o e - Exit

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node


{
char line[80];
struct node *next;
}NODE;

NODE *first,*last;
int len,changed;

NODE * get_node(char *s)


{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->line,s);
p->next=NULL;
return p;
}

void create(char fname[])


{
NODE *p;
FILE *fp;
char buff[80];

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
p = get_node(buff);

if(first==NULL)
first = p;
else
last->next = p;

last = p;
len++;
}

fclose(fp);
}
void append()
{
NODE *p;
char buff[80];

printf("Enter text (type END to stop):");

fflush(stdin);
fgets(buff,80,stdin);

while(strcmp(buff,"END\n")!=0)
{
p = get_node(buff);
if(first==NULL)
first = p;
else
last->next = p;
last=p;
len++;

fflush(stdin);
fgets(buff,80,stdin);
}

changed=1;
}

void save(char fname[])


{
FILE *fp;
NODE *p;

fp = fopen(fname,"w");

p = first;
while(p!=NULL)
{
fputs(p->line,fp);
p=p->next;
}

fclose(fp);
changed=0;
}

void copy(int n1, int n2)


{
NODE *p,*q,*t;
int i;
i=1;
p=first;
while(i<n1)
{
i++;
p=p->next;
}

t = get_node(p->line);

i=1;
p=q=first;
while(i<n2)
{
i++;
q=p;
p=p->next;
}

if(p==first)
{
t->next=first;
first=t;
}
else
{
q->next=t;
t->next=p;
}

len++;
changed=1;
}

int main(int argc, char *argv[])


{
char buff[80],ch,fname[30]="";
int n1,n2;

if(argc==2)
{
strcpy(fname,argv[1]);
create(fname);
}

while(1)
{
printf("$");
fflush(stdin);
fgets(buff,80,stdin);

sscanf(buff,"%c %d %d",&ch,&n1,&n2);

switch(ch)
{
case 'a':
append();
break;
case 'c':
if(n1>=1 && n1<=len && n2>=1 && n2<=len)
copy(n1,n2);
else
printf("Invalid parameter\n");
break;
case 's':
if(changed==1)
{
if(strlen(fname)==0)
{
printf("Enter source file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
break;
case 'e':
if(changed==1)
{
printf("Quit without save (Y/N)?");
fflush(stdin);
fgets(buff,80,stdin);
if(buff[0]=='N')
{
if(strlen(fname)==0)
{
printf("Enter file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
}
exit(0);
default:
printf("Invalid command.\n");
}
}

return 0;
}
13) Write a command line program for line editor. The file to be edited is taken as command line
argument; an empty file is opened for editing if no argument is supplied. It should display a „$‟
prompt to accept the line editing commands. Implement the following commands.
o s - Save
o a - Append
o m n1 n2 - Copy line from position n1 to n2
o e - Exit

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node


{
char line[80];
struct node *next;
}NODE;

NODE *first,*last;
int len,changed;

NODE * get_node(char *s)


{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->line,s);
p->next=NULL;
return p;
}

void create(char fname[])


{
NODE *p;
FILE *fp;
char buff[80];

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
p = get_node(buff);

if(first==NULL)
first = p;
else
last->next = p;

last = p;
len++;
}

fclose(fp);
}

void append()
{
NODE *p;
char buff[80];

printf("Enter text (type END to stop):");

fflush(stdin);
fgets(buff,80,stdin);

while(strcmp(buff,"END\n")!=0)
{
p = get_node(buff);
if(first==NULL)
first = p;
else
last->next = p;
last=p;
len++;

fflush(stdin);
fgets(buff,80,stdin);
}

changed=1;
}

void copy(int n1, int n2)


{
NODE *p,*q,*t;
int i;

i=1;
p=first;
while(i<n1)
{
i++;
p=p->next;
}

t = get_node(p->line);

i=1;
p=q=first;
while(i<n2)
{
i++;
q=p;
p=p->next;
}

if(p==first)
{
t->next=first;
first=t;
}
else
{
q->next=t;
t->next=p;
}

len++;
changed=1;
}

void del(int pos)


{
NODE *p,*q;
int i=1;

p = q = first;
while(i<pos)
{
q = p;
p = p->next;
i++;
}
if(p==first)
{
first = p->next;
}
else if(p==last)
{
q->next=NULL;
last=q;
}
else
{
q->next=p->next;
}

free(p);
len--;
changed=1;
}

void save(char fname[])


{
FILE *fp;
NODE *p;

fp = fopen(fname,"w");

p = first;
while(p!=NULL)
{
fputs(p->line,fp);
p=p->next;
}

fclose(fp);
changed=0;
}

int main(int argc, char *argv[])


{
char buff[80],ch,fname[30]="";
int n1,n2;

if(argc==2)
{
strcpy(fname,argv[1]);
create(fname);
}

while(1)
{
printf("$");
fflush(stdin);
fgets(buff,80,stdin);

sscanf(buff,"%c %d %d",&ch,&n1,&n2);

switch(ch)
{
case 'a':
append();
break;
case 'm':
if(n1>=1 && n1<=len && n2>=1 && n2<=len)
{
copy(n1,n2);
if(n1<=n2)
del(n1);
else
del(n1+1);
}
else
printf("Invalid parameter\n");
break;
case 's':
if(changed==1)
{
if(strlen(fname)==0)
{
printf("Enter source file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
break;
case 'e':
if(changed==1)
{
printf("Quit without save (Y/N)?");
fflush(stdin);
fgets(buff,80,stdin);
if(buff[0]=='N')
{
if(strlen(fname)==0)
{
printf("Enter file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
}
exit(0);
default:
printf("Invalid command.\n");
}
}

return 0;
}

14) Write a macro pre-processor program that will create and display the contents of MNT, PNTAB,
and KPDTAB for the following macro definition.

INPUT FILE:

MACRO
CALC &A,&B,&REG=CREG,&OP=MULT
MOVER &REG, &A
&OP &REG, &B
MOVEM &REG, &A
MEND

PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct MNT
{
char mname[20];
int mdtp,pp;
}mnt;

struct MDT
{
char op[20],value[50];
}mdt[10];

char pnt[10][10];

int mdt_cnt,pnt_cnt;

int search(char *s)


{
int i;
for(i=0;i<pnt_cnt;i++)
if(strcmp(pnt[i],s)==0)
return i;

return -1;
}

void print_mnt()
{
printf("#\tmname\tmdtp\tpp\n");
printf("1\t%s\t%d\t%d\n",
mnt.mname,mnt.mdtp,mnt.pp);
}

void print_pnt()
{
int i;

printf("#\tpname\n");

for(i=0;i<pnt_cnt;i++)
printf("%d\t%s\n",i+1,pnt[i]);
}

void print_mdt()
{
int i;

printf("#\topcode\tvalue\n");

for(i=0;i<mdt_cnt;i++)
printf("%d\t%s\t%s\n",
i+1,mdt[i].op,mdt[i].value);
}
void make_pnt(char *s)
{
char temp[10];
int i=0,j=0;

strcat(s,",");

while(s[i]!='\0')
{
if(s[i]==',')
{
temp[j]='\0';
j=0;
strcpy(pnt[pnt_cnt++],temp);
}
else
temp[j++]=s[i];

i++;
}
}

int main()
{
FILE *fp;
char fname[20],buff[80],t1[20],t2[20],t3[20];
int i,j;

printf("Enter source file:");


scanf("%s",fname);

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
sscanf(buff,"%s %s %s",t1,t2,t3);

if(strcmp(t1,"MACRO")==0)
{
fgets(buff,80,fp);
sscanf(buff,"%s %s",t1,t2);
strcpy(mnt.mname,t1);
make_pnt(t2);
mnt.pp = pnt_cnt;
mnt.mdtp = 1;
}
else if(strcmp(t1,"MEND")==0)
{
strcpy(mdt[mdt_cnt++].op,t1);
}
else
{
if(t1[0]=='&')
{
i = search(t1);
sprintf(mdt[mdt_cnt].op,"(P, %d)",i+1);
}
else
{
strcpy(mdt[mdt_cnt].op,t1);
}

t2[strlen(t2)-1]='\0';
i = search(t2);
j = search(t3);
sprintf(mdt[mdt_cnt++].value, "(P, %d), (P, %d)",i,j+1);
}
}

fclose(fp);
print_mnt();
print_pnt();
print_mdt();

return 0;
}
14) Write an assembler that will display the following errors and warnings:
o Symbol used but not defined
o Symbol defined but not used
o Re-declaration of the Symbol,,,, Present in the following assembly program:

INPUT FILE:

START 100
READ X
LOOP READ BREG, X
ADD BREG, Y
X MOVEM AREG, Z
STOP
X DS 1
Y DS 1
END

PROGRAM:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node


{
char symb[20];
int decl,used;
struct node *next;
}NODE;

NODE *first, *last;

void add_symb1(char *s)


{
NODE *p;

p=(NODE*)malloc(sizeof(NODE));
strcpy(p->symb,s);
p->decl = 1;
p->used = 0;
p->next = NULL;

if(first==NULL)
first = p;
else
last->next = p;

last = p;
}

void add_symb2(char *s)


{
NODE *p;

p = (NODE*)malloc(sizeof(NODE));
strcpy(p->symb,s);
p->decl = 0;
p->used = 1;
p->next = NULL;

if(first==NULL)
first=p;
else
last->next = p;

last=p;
}

NODE * search(char *s)


{
NODE *p;

p = first;
while(p!=NULL)
{
if(strcmp(p->symb,s)==0)
return p;

p=p->next;
}

return NULL;
}

char optab[][6]={"STOP","ADD","SUB","MULT",
"MOVER","MOVEM","COMP","BC",
"DIV","READ","PRINT"};

int search_op(char *s)


{
int i;
for(i=0;i<11;i++)
if(strcmp(optab[i],s)==0)
return i;

return -1;
}

int main()
{
NODE *p;
FILE *fp;
char buff[80],t1[20],t2[20],
t3[20],t4[20],fname[40];
int n,i;
printf("Enter source file name:");
scanf("%s",fname);

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
n = sscanf(buff,"%s %s %s %s",t1,t2,t3,t4);

switch(n)
{
case 2:
if(strcmp(t1,"START")==0)
break;

i = search_op(t1);
if(i==9 || i==10) // READ or PRINT
{
p=search(t2);
if(p==NULL)
add_symb2(t2);
else
p->used=1;

break;
}

p = search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;

break;
case 3:
i = search_op(t1);
if(i>=1 && i<=8) // ADD to DIV
{
p = search(t3);
if(p==NULL)
add_symb2(t3);
else
p->used=1;
break;
}

if(strcmp(t2,"DS")==0 ||
strcmp(t2,"DC")==0)
{
p=search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;
break;
}

p=search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;

p=search(t3);
if(p==NULL)
add_symb2(t3);
else
p->used = 1;

break;
case 4:
p = search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;

p = search(t4);
if(p==NULL)
add_symb2(t4);
else
p->used =1;
} // switch
} // while

fclose(fp);

p = first;
while(p!=NULL)
{
if(p->decl!=0 && p->used==0)
printf("Symb %s declared but not used.\n",p->symb);
if(p->used==1 && p->decl==0)
printf("Symb %s used but not declared.\n",p->symb);
if(p->decl>1)
printf("Symb %s redeclared.\n",p->symb);

p=p->next;
}

return 0;
}
15) Write an assembler that will display the following errors and warnings:
o Symbol used but not defined
o Symbol defined but not used
o Re-declaration of the Symbol, Present in the following assembly program

INPUT FILE:

START 100
READ A
A READ BREG, A
ADD BREG, B
LOOP MOVEM AREG, C
STOP
A DS 1
B DS 1
END

PROGRAM:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node


{
char symb[20];
int decl,used;
struct node *next;
}NODE;

NODE *first, *last;

void add_symb1(char *s)


{
NODE *p;

p=(NODE*)malloc(sizeof(NODE));
strcpy(p->symb,s);
p->decl = 1;
p->used = 0;
p->next = NULL;

if(first==NULL)
first = p;
else
last->next = p;

last = p;
}

void add_symb2(char *s)


{
NODE *p;

p = (NODE*)malloc(sizeof(NODE));
strcpy(p->symb,s);
p->decl = 0;
p->used = 1;
p->next = NULL;

if(first==NULL)
first=p;
else
last->next = p;

last=p;
}

NODE * search(char *s)


{
NODE *p;

p = first;
while(p!=NULL)
{
if(strcmp(p->symb,s)==0)
return p;

p=p->next;
}

return NULL;
}

char optab[][6]={"STOP","ADD","SUB","MULT",
"MOVER","MOVEM","COMP","BC",
"DIV","READ","PRINT"};

int search_op(char *s)


{
int i;
for(i=0;i<11;i++)
if(strcmp(optab[i],s)==0)
return i;

return -1;
}

int main()
{
NODE *p;
FILE *fp;
char buff[80],t1[20],t2[20],
t3[20],t4[20],fname[40];
int n,i;

printf("Enter source file name:");


scanf("%s",fname);

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
n = sscanf(buff,"%s %s %s %s",t1,t2,t3,t4);

switch(n)
{
case 2:
if(strcmp(t1,"START")==0)
break;

i = search_op(t1);
if(i==9 || i==10) // READ or PRINT
{
p=search(t2);
if(p==NULL)
add_symb2(t2);
else
p->used=1;

break;
}

p = search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;

break;
case 3:
i = search_op(t1);
if(i>=1 && i<=8) // ADD to DIV
{
p = search(t3);
if(p==NULL)
add_symb2(t3);
else
p->used=1;

break;
}

if(strcmp(t2,"DS")==0 ||
strcmp(t2,"DC")==0)
{
p=search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;
break;
}

p=search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;

p=search(t3);
if(p==NULL)
add_symb2(t3);
else
p->used = 1;

break;
case 4:
p = search(t1);
if(p==NULL)
add_symb1(t1);
else
p->decl++;

p = search(t4);
if(p==NULL)
add_symb2(t4);
else
p->used =1;
} // switch
} // while

fclose(fp);

p = first;
while(p!=NULL)
{
if(p->decl!=0 && p->used==0)
printf("Symb %s declared but not used.\n",p->symb);
if(p->used==1 && p->decl==0)
printf("Symb %s used but not declared.\n",p->symb);
if(p->decl>1)
printf("Symb %s redeclared.\n",p->symb);

p=p->next;
}

return 0;
}

16) Write a command line program for line editor. The file to be edited is taken as command line
arguments. An empty file is opened for editing if no argument is supplied. It should display a „$‟
prompt to accept the line editing commands. Implement the following commands:
o i n : Insert line at position n.
o d n1 n2 : Delete line between position n1 and n2
o s : Save
o p : Print all lines
o e : Exit
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct node


{
char line[80];
struct node *next;
}NODE;

NODE *first,*last;
int len,changed;

NODE * get_node(char *s)


{
NODE *p;
p=(NODE*)malloc(sizeof(NODE));
strcpy(p->line,s);
p->next=NULL;
return p;
}

void create(char fname[])


{
NODE *p;
FILE *fp;
char buff[80];

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
p = get_node(buff);

if(first==NULL)
first = p;
else
last->next = p;
last = p;
len++;
}

fclose(fp);
}

void print()
{
NODE *p;
int i=1;

p=first;
while(p!=NULL)
{
printf("%d:%s",i,p->line);
i++;
p=p->next;
}
}

void save(char fname[])


{
FILE *fp;
NODE *p;

fp = fopen(fname,"w");

p = first;
while(p!=NULL)
{
fputs(p->line,fp);
p=p->next;
}

fclose(fp);
changed=0;
}

void insert(int pos)


{
NODE *first1=NULL,*last1,*p,*q;
int i=0;
char buff[80];

printf("Enter text to insert (type END to stop):");


fflush(stdin);
fgets(buff,80,stdin);

while(strcmp(buff,"END\n")!=0)
{
p = get_node(buff);
if(first1==NULL)
first1=p;
else
last1->next=p;
last1=p;
i++;

fflush(stdin);
fgets(buff,80,stdin);
}
len+=i;
p = q = first;
i=1;
while(i<pos)
{
i++;
q=p;
p=p->next;
}

if(p==first)
{
last1->next=first;
first=first1;
}
else
{
q->next=first1;
last1->next=p;
}

changed=1;
}

void del1(int pos)


{
NODE *p,*q;
int i=1;

p = q = first;
while(i<pos)
{
q = p;
p = p->next;
i++;
}

if(p==first)
{
first = p->next;
}
else if(p==last)
{
q->next=NULL;
last=q;
}
else
{
q->next=p->next;
}

free(p);
len--;
changed=1;
}

void del(int n1, int n2)


{
int n,i;
n=n2-n1+1;
for(i=1;i<=n;i++)
del1(n1);
}

int main(int argc, char *argv[])


{
char buff[80],ch,fname[30]="";
int n1,n2;

if(argc==2)
{
strcpy(fname,argv[1]);
create(fname);
}

while(1)
{
printf("$");
fflush(stdin);
fgets(buff,80,stdin);

sscanf(buff,"%c %d %d",&ch,&n1,&n2);
switch(ch)
{
case 'p':
print();
break;
case 'i':
if(n1>=1 && n1<=len)
insert(n1);
else
printf("Invalid parameter\n");
break;
case 's':
if(changed==1)
{
if(strlen(fname)==0)
{
printf("Enter source file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
break;
case 'd':
if(n1>=1 && n2<=len && n1<=n2)
del(n1,n2);
else
printf("Invalid parameter\n");
break;
case 'e':
if(changed==1)
{
printf("Quit without save (Y/N)?");
fflush(stdin);
fgets(buff,80,stdin);
if(buff[0]=='N')
{
if(strlen(fname)==0)
{
printf("Enter file name:");
fflush(stdin);
fgets(fname,30,stdin);
fname[strlen(fname)-1]='\0';
}
save(fname);
}
}
exit(0);
default:
printf("Invalid command.\n");
}
}

return 0;
}

17) Consider the following Macro definition and Data structures (stored as an array of structure)
association with it as follows:

INPUT FILE:
CALC X,Y
CALC P,Q,&OP=SUB

PROGRAM

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct MDT
{
char op[10],value[30];
}mdt[]={
{"MOVER","AREG, (P, 1)"},
{"(P, 3)", "AREG, (P, 2)"},
{"MOVEM", "AREG, (P, 1)"},
{"MEND"}};

char pnt[][10]={"&A","&B","&OP"};

struct MNT
{
char mname[10];
int mdtp,kpdtp,pntp,kp,pp;
}mnt={"CALC",0,0,0,1,2};

struct KPDT
{
char key[10],def[10];
}kpdt={"&OP","ADD"};

int pnt_cnt;

void make_apt(char *s)


{
int i=0,j=0;
char temp[10];

pnt_cnt=0;
strcat(s,",");

while(s[i]!='\0' && s[i]!='=')


{
if(s[i]==',')
{
temp[j]='\0';
j=0;
strcpy(pnt[pnt_cnt++],temp);
}
else
{
temp[j++]=s[i];
}
i++;
}

while(s[i]!='\0')
{
if(s[i]=='=')
{
j=0;
}
else if(s[i]==',')
{
temp[j]='\0';
j=0;
strcpy(pnt[mnt.pp],temp);
}
else
{
temp[j++]=s[i];
}
i++;
}
}

void expand()
{
int mec;
char t1[10],t2[10],t3[10];

mec = mnt.mdtp;
while(strcmp(mdt[mec].op,"MEND")!=0)
{
if(mdt[mec].op[0]=='(')
{
sscanf(mdt[mec].op,"%s %s",t1,t2);
printf("+%s ",pnt[atoi(t2)-1]);
}
else
{
printf("+%s ",mdt[mec].op);
}

sscanf(mdt[mec].value,"%s %s %s",t1,t2,t3);
printf("%s %s\n",t1,pnt[atoi(t3)-1]);

mec++;
}
}

int main()
{
FILE *fp;
char fname[20],buff[80],t1[30],t2[30];

printf("Enter file name:");


scanf("%s",fname);

fp = fopen(fname,"r");
if(fp==NULL)
{
printf("File %s not found.\n",fname);
exit(1);
}

while(fgets(buff,80,fp)!=NULL)
{
sscanf(buff,"%s %s",t1,t2);
strcpy(pnt[mnt.pp],kpdt.def);
make_apt(t2);
expand();
}

fclose(fp);

return 0;
}

You might also like