Saturday, January 16, 2010

intermediate code generation



Download this file : incode.c


program:

#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];
void 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++;
}
}


Download this file : incode.c




Output:






Download this file : incode.c







DDA LINE Drawing Algorithm



download this file : dda.c


program:
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"graphics.h"
#include"dos.h"
#include"math.h"
void main()
{
int i,gd,gm,delay=1;
float dx,dy,x1,x2,y1,y2,step,maxx,maxy,roundx,roundy;
float x,y,xi,yi;
char buf1[50],buf2[50],buf3[50],buf4[10],buf5[10];
clrscr();
gd=DETECT;
printf("\n Enter start co-ordinates (x1,y1) :");
scanf("%f%f",&x1,&y1);
printf("\n Enter end co-ordinates  (x2,y2) :");
scanf("%f%f",&x2,&y2);
clrscr();
x1+=100;
x2+=100;
y1+=100;
y2+=100;
initgraph(&gd,&gm,"");
dx=x2-x1;
dy=y2-y1;
maxx=getmaxx();
maxy=getmaxy();
line(100,0,100,maxy);
line(0,100,maxx,100);
outtextxy(80,90,"0");
outtextxy(80,200,"100");
outtextxy(80,300,"200");
outtextxy(80,400,"300");
outtextxy(80,500,"400");
outtextxy(180,90,"100");
outtextxy(280,90,"200");
outtextxy(380,90,"300");
outtextxy(480,90,"400");
outtextxy(580,90,"500");
sprintf(buf1," DDA LINE ALGORITHM ");
outtextxy(300,10,buf1);
sprintf(buf2,"x1=%3.0f,y1=%3.0f,x2=%3.0f,y2=%3.0f,",x1-100,y1-100,x2-100,y2-100);
outtextxy(200,20,buf2);
if(abs(dx)>abs(dy))
step=dx;
else
step=dy;
xi = dx / step;
yi = dy / step;
sprintf(buf3,"dx=%3.0f,dy=%3.0f,step=%3.0f,xi=%3.2f,yi=%3.2f",dx,dy,step,xi,yi);
outtextxy(200,30,buf3);
x=x1;
y=y1;
putpixel(x,y,delay);
circle(x,y,2);
sprintf(buf4,"(%3.0f,%3.0f)",x1-100,y1-100);
outtextxy(x1+10,y1,buf4);
for(i=1;i<=step;i++)
{
  if(delay==15)
  {
sleep(1);
delay=1;
  }
  delay++;
  x=x+xi;
  y=y+yi;
  roundx=ceil(x);
  roundy=ceil(y);
  putpixel(roundx,roundy,delay);
}
circle(roundx,roundy,2);
sprintf(buf5,"(%3.0f,%3.0f)",roundx-100,roundy-100);
outtextxy(roundx+10,roundy,buf5);

getch();
restorecrtmode();
}

download this file : dda.c


ouput:
 Enter start co-ordinates (x1,y1) : 100    125
 Enter end co-ordinates  (x2,y2) : 250    350








two dimensional transformation





Download this file : 2d.c




program:
#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"math.h"
int gd=0,gm,theta,xf,yf,x1,y1,x2,y2,xa1,xa2,ya1,ya2,a;
int midx,midy,tx,ty,choice,sx,sy,sh,xa[10],ya[10],first;
int i,n=5;
int xf,yf;
int xb[10],yb[10];
void main()
{
do
{
first=0;
org();
getch();
printf("\n1.translation\n2.rotation\n3.scaling\n4.reflection\n5.shear\n6.exit\nenter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the translation factor (x,y) :");
scanf("%d%d",&tx,&ty);
org();
rectangle(midx+tx,midy-100+ty,midx+100+tx,midy+ty);
getch();
closegraph();
break;
case 2:
printf("Enter the angle :");
scanf("%d",&a);
org();
rotation();
break;
case 3:
printf("Enter the scaling factor (x,y):");
scanf("%d%d",&sx,&sy);
org();
x1=0;
y1=-100;
x2=100;
y2=0;
xa1=x1*sx+midx;
ya1=y1*sy+midy;
xa2=x2*sx+midx;
ya2=y2*sy+midy;
rectangle(xa1,ya1,xa2,ya2);
getch();
closegraph();
break;
case 4:
org();
rectangle(midx,midy,midx+100,midy+100);
getch();
closegraph();
break;
case 5:
printf("enter the shear value");
scanf("%d",&sh);
cleardevice();
org();
for(i=0;i < n;i++)
{
xb[i]=xa[i]+sh*(ya[i]-yf);
yb[i]=ya[i];
}
for(i=0;i < n;i++)
line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]);
getch();
}
}while(choice<6);
getch();
}
org()
{
initgraph(&gd,&gm,"");
midx=getmaxx()/2;
midy=getmaxy()/2;
xf=midx;
yf=midy;
xa[0]=midx;
ya[0]=midy-100;
xa[1]=midx+100;
ya[1]=midy-100;
xa[2]=midx+100;
ya[2]=midy;
xa[3]=midx;
ya[3]=midy;
xa[4]=midx;
ya[4]=midy-100;
cleardevice();
setcolor(WHITE);
line(0,midy,2*midx,midy);
line(midx,0,midx,2*midy);
setcolor(4);
if(first==0)
{
setlinestyle(SOLID_LINE,1,1);
first=1;
}
else
{
setlinestyle(DASHED_LINE,1,1);
}
rectangle(midx,midy-100,midx+100,midy);
setlinestyle(SOLID_LINE,1,1);
return;
}
rotation()
{
theta=(a*2*3.14)/180;
for(i=0;i < n;i++)
{
xb[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);
yb[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);
}
for(i=0;i < n;i++)
line(xb[i],yb[i],xb[(i+1)%n],yb[(i+1)%n]);
getch();
cleardevice();
return;
}


Download this file : 2d.c

Output:






























midpoint circle algorithm




Download this file  : circle.c




program:
#include"graphics.h"
#include"stdio.h"
#include"conio.h"
#include"dos.h"
int step=0,r;
void main()
{
int i,j,x,y,p,a,midx,midy;
int gd=DETECT,gm,xcenter,ycenter;
printf("Enter the radius  :");
scanf("%d",&r);
initgraph(&gd,&gm,"");
midx=getmaxx()/2;
midy=getmaxy()/2;
line(midx,0,midx,2*midy);
line(0,midy,2*midx,midy);
outtextxy(midx-10,midy+10,"0");
outtextxy(midx-20,10,"-y");
outtextxy(midx-20,2*midy-10,"+y");
outtextxy(10,midy-10,"-x");
outtextxy(2*midx-20,midy-10,"+x");
xcenter=midx;
ycenter=midy;
x=0;
y=r;
p=1-r;
midpoint(xcenter,ycenter,x,y,p);
getch();
closegraph();
restorecrtmode();

}
midpoint(int xcenter,int ycenter,int x,int y,int p)
{
while(x < y)
{
plotcircle(xcenter,ycenter,x,y);
if(p<0)
p=p+2*x+1;
else
{
p=p+2*(x-y)+1;
y=y-1;
}
x=x+1;
}
if(x==y)
plotcircle(xcenter,ycenter,x,y);
return;
}
plotcircle(int xcenter,int ycenter,int x,int y)
{
delay(50);
step++;
if(step==r/2)
{
outtextxy(xcenter+x,ycenter+y,"(+x,-y)");
outtextxy(xcenter-x-50,ycenter+y,"(-x,-y)");
outtextxy(xcenter+x,ycenter-y,"(+x,+y)");
outtextxy(xcenter-x-50,ycenter-y,"(-x,+y)");
outtextxy(xcenter+y,ycenter+x,"(+y,-x)");
outtextxy(xcenter-y-50,ycenter+x,"(-y,-x)");
outtextxy(xcenter+y,ycenter-x,"(+y,+x)");
outtextxy(xcenter-y-50,ycenter-x,"(-y,+x)");
}
putpixel(xcenter+x,ycenter+y,10);
putpixel(xcenter-x,ycenter+y,10);
putpixel(xcenter+x,ycenter-y,10);
putpixel(xcenter-x,ycenter-y,10);
putpixel(xcenter+y,ycenter+x,10);
putpixel(xcenter-y,ycenter+x,10);
putpixel(xcenter+y,ycenter-x,10);
putpixel(xcenter-y,ycenter-x,10);
putpixel(xcenter+x,ycenter-x,10);
return;
}




Download this file  : circle.c



output:


Enter the radius : 150









Download this file  : circle.c

midpoint ellipse algorithm

Download this file : ellipse.c


#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"math.h"
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int rx,ry;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"");
printf("Enter the center point :");
scanf("%d%d",&xc,&yc);
printf("Enter the value for Rx and Ry :");
scanf("%d%d",&rx,&ry);
x=0;
y=ry;
disp();
p1=(ry*ry)-(rx*rx*ry)+(rx*rx)/4;
while((2.0*ry*ry*x)<=(2.0*rx*rx*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*ry*ry*x)+(ry*ry);
else
{
y--;
p1=p1+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(ry*ry);
}
disp();
x=-x;
disp();
x=-x;
}
x=rx;
y=0;
disp();
p2=(rx*rx)+2.0*(ry*ry*rx)+(ry*ry)/4;
while((2.0*ry*ry*x)>(2.0*rx*rx*y))
{
y++;
if(p2>0)
p2=p2+(rx*rx)-(2.0*rx*rx*y);
else
{
x--;
p2=p2+(2.0*ry*ry*x)-(2.0*rx*rx*y)+(rx*rx);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
delay(50);
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc-x,yc-y,10);
}


Download this file : ellipse.c

output:







Friday, January 15, 2010

shift reduce parser


#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#include"string.h"
char ip_sym[15],stack[15];
int ip_ptr=0,st_ptr=0,len,i;
char temp[2],temp2[2];
char act[15];
void check();
void main()
{
clrscr();
printf("\n\t\t SHIFT REDUCE PARSER\n");
printf("\n GRAMMER\n");
printf("\n E->E+E\n E->E/E");
printf("\n E->E*E\n E->a/b");
printf("\n enter the input symbol:\t");
gets(ip_sym);
printf("\n\t stack implementation table");
printf("\n stack\t\t input symbol\t\t action");
printf("\n______\t\t ____________\t\t ______\n");
printf("\n $\t\t%s$\t\t\t--",ip_sym);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
len=strlen(ip_sym);
for(i=0;i<=len-1;i++)


{
stack[st_ptr]=ip_sym[ip_ptr];
stack[st_ptr+1]='\0';
ip_sym[ip_ptr]=' ';
ip_ptr++;
printf("\n $%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
check();
st_ptr++;
}
st_ptr++;
check();
}
void check()
{
int flag=0;
temp2[0]=stack[st_ptr];
temp2[1]='\0';
if((!strcmpi(temp2,"a"))||(!strcmpi(temp2,"b")))
{
stack[st_ptr]='E';
if(!strcmpi(temp2,"a"))
printf("\n $%s\t\t%s$\t\t\tE->a",stack, ip_sym);
else
printf("\n $%s\t\t%s$\t\t\tE->b",stack,ip_sym);
flag=1;
}
if((!strcmpi(temp2,"+"))||(strcmpi(temp2,"*"))||(!strcmpi(temp2,"/")))
{
flag=1;
}
if((!strcmpi(stack,"E+E"))||(!strcmpi(stack,"E\E"))||(!strcmpi(stack,"E*E")))
{
strcpy(stack,"E");
st_ptr=0;
if(!strcmpi(stack,"E+E"))
printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
else
if(!strcmpi(stack,"E\E"))
printf("\n $%s\t\t %s$\t\t\tE->E\E",stack,ip_sym);
else
printf("\n $%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
flag=1;
}

if(!strcmpi(stack,"E")&&ip_ptr==len)
{
printf("\n $%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
getch();
exit(0);
}
if(flag==0)
{
printf("\n%s\t\t\t%s\t\t reject",stack,ip_sym);
exit(0);
}
return;
}

output:









Wednesday, January 13, 2010

recursive descent parser in c



Download this file : parse.c




program:
#include"stdio.h"
#include"conio.h"
#include"string.h"
#include"stdlib.h"
#include"ctype.h"


char ip_sym[15],ip_ptr=0,op[50],tmp[50];
void e_prime();
void e();
void t_prime();
void t();
void f();
void advance();
int n=0;
void e()
{
 strcpy(op,"TE'");
 printf("E=%-25s",op);
 printf("E->TE'\n");
 t();
 e_prime();
}

void e_prime()
{
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
    if(op[i]!='e')
tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='E';n++);
if(ip_sym[ip_ptr]=='+')
 {
     i=n+2;
do
{
op[i+2]=op[i];
i++;
}while(i<=l);
  op[n++]='+';
  op[n++]='T';
  op[n++]='E';
  op[n++]=39;
  printf("E=%-25s",op);
  printf("E'->+TE'\n");
  advance();
  t();
  e_prime();
 }
 else
 {
     op[n]='e';
  for(i=n+1;i<=strlen(op);i++)
op[i]=op[i+1];
 printf("E=%-25s",op);
 printf("E'->e");
 }
}
void t()
{
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
if(op[i]!='e')
tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='T';n++);

i=n+1;
do
{
op[i+2]=op[i];
i++;
}while(i < l);
op[n++]='F';
op[n++]='T';
op[n++]=39;
printf("E=%-25s",op);
printf("T->FT'\n");
f();
t_prime();
}

void t_prime()
{
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
    if(op[i]!='e')
tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='T';n++);
if(ip_sym[ip_ptr]=='*')
 {
     i=n+2;
do
{
op[i+2]=op[i];
i++;
}while(i < l);
  op[n++]='*';
  op[n++]='F';
  op[n++]='T';
  op[n++]=39;
  printf("E=%-25s",op);
  printf("T'->*FT'\n");
  advance();
  f();
  t_prime();
 }
 else
 {
   op[n]='e';
  for(i=n+1;i<=strlen(op);i++)
op[i]=op[i+1];
 printf("E=%-25s",op);
 printf("T'->e\n");
 }
}

void f()
{
int i,n=0,l;
for(i=0;i<=strlen(op);i++)
    if(op[i]!='e')
tmp[n++]=op[i];
strcpy(op,tmp);
l=strlen(op);
for(n=0;n < l && op[n]!='F';n++);
 if((ip_sym[ip_ptr]=='i')||(ip_sym[ip_ptr]=='I'))
 {
op[n]='i';
printf("E=%-25s",op);
printf("F->i\n");
advance();
 }
 else
 {
  if(ip_sym[ip_ptr]=='(')
  {
   advance();
   e();
   if(ip_sym[ip_ptr]==')')
   {
    advance();
     i=n+2;
do
{
op[i+2]=op[i];
i++;
}while(i<=l);
  op[n++]='(';
  op[n++]='E';
  op[n++]=')';
  printf("E=%-25s",op);
  printf("F->(E)\n");
   }
  }
  else
  {
   printf("\n\t syntax error");
   getch();
   exit(1);
  }
 }
}

void advance()
{
 ip_ptr++;
}

void main()
{
 int i;
 clrscr();
 printf("\nGrammar without left recursion");
 printf("\n\t\t E->TE' \n\t\t E'->+TE'|e \n\t\t T->FT' ");
 printf("\n\t\t T'->*FT'|e \n\t\t F->(E)|i");
 printf("\n Enter the input expression:");
 gets(ip_sym);
 printf("Expressions");
 printf("\t Sequence of production rules\n");
  e();
  for(i=0;i < strlen(ip_sym);i++)
 {
  if(ip_sym[i]!='+'&&ip_sym[i]!='*'&&ip_sym[i]!='('&&
     ip_sym[i]!=')'&&ip_sym[i]!='i'&&ip_sym[i]!='I')
  {
   printf("\nSyntax error");
   break;
  }
  for(i=0;i<=strlen(op);i++)
    if(op[i]!='e')
tmp[n++]=op[i];
    strcpy(op,tmp);
    printf("\nE=%-25s",op);
 }
 getch();
}



Download this file : parse.c