Text editor
#include<stdio.h>
#include<conio.h>
#include<process.h>
int i,j,ec,fg,ec2,c1;
FILE *fp1,*fp2,*fp3;
char fn[10],e,ch,c,n;
void create();
void append();
void view();
void delete();
void main()
{
clrscr();
do
{
printf("\n MENU\n");
printf("\n1.CREATE \n2.VIEW \n3.DELETE \n4.EXIT\n");
printf("Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
view();
break;
case 3:
delete();
break;
default:
exit(0);
}
}
while(1);
}
void create()
{
fp1=fopen("exam.c","w");
printf("\n Enter the data and press z to stop\n");
while(1)
{
c=getchar();
if(c!='z')
{
fputc(c,fp1);
}
else
{
fclose(fp1);
printf("Enter the new file name:\n");
scanf("%s",&fn);
fp1=fopen("exam.c","r");
fp2=fopen(fn,"w");
while(!feof(fp1))
{
c=getc(fp1);
putc(c,fp2);
}
fclose(fp2);
break;
}
}
}
void view()
{
printf("Enter the filename:\n");
scanf("%s",&fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf("File not found");
goto l1;
}
while(!feof(fp1))
{
c=getc(fp1);
printf("%c",c);
}
l1:fclose(fp1);
getch();
}
void delete()
{
printf("Enter the file name");
scanf("%s",&fn);
fp1=fopen(fn,"r");
if(fp1==NULL)
{
printf("file not found");
goto end3;
}
if (remove(fn)==0)
{
printf("File deleted");
goto end2;
}
else
printf("ERROR\n");
end2:getch();
end3:fclose(fp1);
}
OUTPUT:
MENU
1.CREATE
2.VIEW
3.DELETE
4.EXIT
Enter your choice1
Enter the data and press z to stop
GOPI
SRI
z
Enter
1.CREATE
2.VIEW
3.DELETE
4.EXIT
Enter your choice2
Enter the filename:
sathis
GOPI
SRI
MENU
1.CREATE
2.VIEW
3.DELETE
4.EXIT
Enter your choice3
Enter the file namesathis
File deleted
MENU
1.CREATE
2.VIEW
3.DELETE
4.EXIT
Enter your choice4
Direct linking loader
#include<stdio.h>
#include<conio.h>
struct ex
{
char name[20];
char symbol[20];
int address;
int length;
}extab[30];
void main()
{
FILE *in,*p;
Int e=0,a,b,I,count=0,t=0,j=0;
Char c,*addr,symbol[10],name[10],*st;
Clrscr();
In=fopen(“solo.txt”,”r”);
P=fopen(“ob.txt”,”r”);
C=fgetc(p);
While(!feof(in))
{
fscanf(in,”%s%s%d%d”,extab[e].name,extab[
e].symbol,&extab[e].address,&extab[e].length
);
e++;
}
st=extab[0].address;
while(c!EOF)
{
{
fscanf(p,%s”,name);
for(j=0;j<e;j++)
{
if(strcmp(name,extab[j].name)==0)
{
t=addr=extab[j].address;
break;
}
}
}
if(c==’M’)
{
c=getc(p);
fscanf(p,”%d%s”,&a,name);
adder=t+a;
for(j=0;j<e;j++)
{
if(strcmp(name,extab[j].symbo)==0)
{
i=extab[j].address;
break;
}
}
*addr+=i/100;
addr++;
*addr+=i%100;
}
if(c==’T’)
{
for(i=0;i<=6;i++)
c=getc(p);
c=getc(p);
while(c!=’\n’)
{
a=c-48;
a*=10;
c=getc(p);
b=c-48;
*addr=a+b;
addr++;
count++;
c=getc(p);
}
}
c=getc(p);
}
for(j=0;j<count;j++)
{
printf(“Address=%u value =%d\n”,st,*st);
st++;
}
getch();
}
Input:
H proga 0 9
D lista 2 enda 3
T 01 09 000009010012180005
M 1 lista
E proga
H progb 0 18
D listb 12 endb 13
T 10 09 000009010012180005
M 7 listb
E progb
Output:
Solo.txt
Proga # 3476 9
* lista 3478 0
* enda 3479 0
progb # 3485 18
* lista 3488 0
* enda 3489 0
Code Generation
#include<stdio.h>
#include<conio.h>
#include<string.h>
int i=1,j=0,no=0,tmpch=90;
char str[100],left[15],right[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
struct exp
{
int pos;
char op;
}k[15];
int main()
{
//clrscr();
printf("\t\tINTERMEDIATE CODE GENERATION\n\n");
printf("Enter the Expression :");
scanf("%s",str);
printf("The intermediate code:\t\tExpression\n");
findopr();
explore();
getch();
}
void findopr()
{
for(i=0;str[i]!='\0';i++)
if(str[i]==':')
{
k[j].pos=i;
k[j++].op=':';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='/')
{
k[j].pos=i;
k[j++].op='/';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='*')
{
k[j].pos=i;
k[j++].op='*';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='+')
{
k[j].pos=i;
k[j++].op='+';
}
for(i=0;str[i]!='\0';i++)
if(str[i]=='-')
{
k[j].pos=i;
k[j++].op='-';
}
}
void explore()
{
i=1;
while(k[i].op!='\0')
{
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos]=tmpch--;
printf("\t%c := %s%c%s\t\t",str[k[i].pos],left,k[i].op,right);
for(j=0;j<strlen(str);j++)
if(str[j]!='$')
printf("%c",str[j]);
printf("\n");
i++;
}
fright(-1);
if(no==0)
{
fleft(strlen(str));
printf("\t%s := %s",right,left);
getch();
//exit(0);
}
printf("\t%s := %c",right,str[k[--i].pos]);
getch();
}
void fleft(int x)
{
int w=0,flag=0;
x--;
while(x!= -1 &&str[x]!= '+' &&str[x]!='*'&&str[x]!='='&&str[x]!='\0'&&str[x]!='-'&&str[x]!='/'&&str[x]!=':')
{
if(str[x]!='$'&& flag==0)
{
left[w++]=str[x];
left[w]='\0';
str[x]='$';
flag=1;
}
x--;
}
}
void fright(int x)
{
int w=0,flag=0;
x++;
while(x!= -1 && str[x]!= '+'&&str[x]!='*'&&str[x]!='\0'&&str[x]!='='&&str[x]!=':'&&str[x]!='-'&&str[x]!='/')
{
if(str[x]!='$'&& flag==0)
{
right[w++]=str[x];
right[w]='\0';
str[x]='$';
flag=1;
}
x++;
}
}
OUTPUT:
INTERMEDIATE CODE GENERATION
Enter the Expression :w:=a*b+c/d-e/f+g*h
The intermediate code: Expression
Z := c/d w:=a*b+Z-e/f+g*h
Y := e/f w:=a*b+Z-Y+g*h
X := a*b w:=X+Z-Y+g*h
W := g*h w:=X+Z-Y+W
V := X+Z w:=V-Y+W
U := Y+W w:=V-U
T := V-U w:=T
w :=