Friday, January 29, 2010

projection of 3d image



program:
#include "stdio.h"
#include "stdlib.h"
#include"graphics.h"
#include"conio.h"
void draw3d(int s,int x[20],int y[20],int d);
void main()
{
int gd=DETECT,gm;
int x[20],y[20],i,s,d;
initgraph(&gd,&gm,"");
printf("Enter the No of sides : ");
scanf("%d",&s);
for(i=0;i< s;i++)>
{
printf("(x%d,y%d) :",i,i);
scanf("%d%d",&x[i],&y[i]);
}
printf("Depth :");
scanf("%d",&d);
draw3d(s,x,y,d);
getch();
setcolor(14);
for(i=0;i< s-1;i++)
{
line(x[i]+200,y[i],x[i+1]+200,y[i+1]);
}
line(x[i]+200,y[i],x[0]+200,y[0]);
getch();//top view
for(i=0;i< s-1;i++)
{
line(x[i],300,x[i+1],300);
line(x[i],300+d*2,x[i+1],300+d*2);
line(x[i],300,x[i],300+d*2);
line(x[i+1],300,x[i+1],300+d*2);
}
getch();//side view
for(i=0;i< s-1;i++)
{
line(10,y[i],10,y[i+1]);
line(10+d*2,y[i],10+d*2,y[i+1]);
line(10,y[i],10+d*2,y[i]);
line(10,y[i+1],10+d*2,y[i+1]);
}
getch();
closegraph();
}
void draw3d(int s,int x[20],int y[20],int d)
{
int i,j,k=0;
for(j=0;j< 2;j++)
{
for(i=0;i< s-1;i++)>
line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k);
line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);
k=d;
}
for(i=0;i< s;i++)
line(x[i],y[i],x[i]+d,y[i]-d);
}




Output:




Thursday, January 28, 2010

CODE GENERATION



DOWNLOAD THIS FILE :


PROGRAM:

#include"stdio.h"
#include"conio.h"
#include"string.h"
#include"stdlib.h"
struct quadraple
{
int pos;
char op;
char arg1[5];
char arg2[5];
char result[5];
}quad[15];
int n=0;
void assignment(int);
void uminus(int );
void explore();
void codegen(char op[5],int);
char tuple[15][15];
int main(void)
{
FILE *src;
int nRetInd,i;
char str[15];
clrscr();
src=fopen("int.txt","r");
fscanf(src,"%s",str);
while(!feof(src))
{
strcpy(tuple[n++],str);
fscanf(src,"%s",str);
}
printf("INPUT:\nIntermiate codes:\n");
for(i=0;i< n;i++)
printf("%s\n",tuple[i]);
explore();
getch();
clrscr();
printf("OUTPUT:\n");
printf("Quadruple: \n");
printf("pos\topr\targ1\targ2\tresult\n");
for(i=0;i< n;i++)
printf("\n%d\t%c\t%s\t%s\t%s",quad[i].pos,quad[i].op,quad[i].arg1,quad[i].arg2,quad[i].result);
i=0;
printf("\n\ncode generated :\n");
while(i< n)
{
if(quad[i].op=='+')
codegen("ADD",i);
if(quad[i].op=='=')
assignment(i);
if(quad[i].op=='-')
if(!strcmp(quad[i].arg2,"\0"))
uminus(i);
else
codegen("SUB",i);
if(quad[i].op=='*')
codegen("MUL",i);
if(quad[i].op=='/')
codegen("DIV",i);
i++;
}
getch();
fcloseall();
return 0;
}
void codegen(char op[5],int t)
{
char str[25];
printf("MOV %s,R0\n",quad[t].arg1);
printf("%s %s,R0\n",op,quad[t].arg2);
printf("MOV R0,%s\n",quad[t].result);
}
void assignment(int t)
{
char str[25];
printf("MOV %s,%s\n",quad[t].result,quad[t].arg1);
}
void uminus(int t)
{
char str[25];
printf("MOV R0,0\n");
printf("SUB %s,R0\n",quad[t].arg1);
printf("MOV R0,%s\n",quad[t].result);
}

void explore()
{
int i,j,t,t1,t2;
for(i=0;i < n;i++)
{
quad[i].pos=i;
for(j=0,t=0;j < strlen(tuple[i])&&tuple[i][j]!='=';j++)
{
quad[i].result[t++]=tuple[i][j];
}
t1=j;
quad[i].result[t]='\0';
if(tuple[i][j]=='=')
{
quad[i].op='=';
}
if(tuple[i][j+1]=='+'||tuple[i][j+1]=='-'||tuple[i][j+1]=='*'||tuple[i][j+1]=='/')
{
quad[i].op=tuple[i][j+1];
t1=j+1;
}
for(j=t1+1,t=0;j< strlen(tuple[i])&&tuple[i][j]!='+'&&tuple[i][j]!='-'&&tuple[i][j]!='*'&&tuple[i][j]!='/';j++)
{
quad[i].arg1[t++]=tuple[i][j];
}
t2=j;
quad[i].arg1[t]='\0';
if(tuple[i][j]=='+'||tuple[i][j]=='-'||tuple[i][j]=='*'||tuple[i][j]=='/')
{
quad[i].op=tuple[i][j];
}
for(j=t2+1,t=0;j< strlen(tuple[i]);j++)
{
quad[i].arg2[t++]=tuple[i][j];
}
quad[i].arg2[t]='\0';
}
}

INPUT:

INT.TXT

t0=c+d
t1=t0*c
b=t0/c
c=-t1
t2=t3



DOWNLOAD THIS FILE :


OUTPUT:





















Wednesday, January 20, 2010

cohen sutherland line clipping



program:


#include"stdio.h"
#include"conio.h"
#include"graphics.h"
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x1,y1,x2,y2,m;
float start[4],end[4],code[4];
clrscr();
initgraph(&gd,&gm,"");
printf("\n\tEnter the bottom-left coordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\n\tEnter the top-right coordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
printf("\nEnter the coordinates for starting point of line: ");
scanf("%f %f",&x1,&y1);
printf("\nEnter the coordinates for ending point of line: ");
scanf("%f %f",&x2,&y2);
for(i=0;i <4;i++)

{
start[i]=0;
end[i]=0;
}
m=(y2-y1)/(x2-x1);
if(x1 <xmin) start[0]=1;
if(x1 >xmax) start[1]=1;
if(y1 >ymax) start[2]=1;
if(y1 <ymin) start[3]=1;
if(x2 <xmin) end[0]=1;
if(x2 >xmax) end[1]=1;
if(y2 >ymax) end[2]=1;
if(y2 <ymin) end[3]=1;
for(i=0;i <4;i++)


code[i]=start[i]&&end[i];

if((code[0]==0)&&(code[1]==0)&&(code[2]==0)&&(code[3]==0))
{
if((start[0]==0)&&(start[1]==0)&&(start[2]==0)&&(start[3]==0)&&(end[0]==0)&&(end[1]==0)&&(end[2]==0)&&(end[3]==0))
{
cleardevice();
printf("\n\t\tThe line is totally visible\n\t\tand not a clipping candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
else
{
cleardevice();
printf("\n\t\tLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
if((start[2]==0)&&(start[3]==1))
{
x1=x1+(ymin-y1)/m;
y1=ymin;
}
if((end[2]==0)&&(end[3]==1))
{
x2=x2+(ymin-y2)/m;
y2=ymin;
}
if((start[2]==1)&&(start[3]==0))
{
x1=x1+(ymax-y1)/m;
y1=ymax;
}
if((end[2]==1)&&(end[3]==0))
{
x2=x2+(ymax-y2)/m;
y2=ymax;
}
if((start[1]==0)&&(start[0]==1))
{
y1=y1+m*(xmin-x1);
x1=xmin;
}
if((end[1]==0)&&(end[0]==1))
{
y2=y2+m*(xmin-x2);
x2=xmin;
}
if((start[1]==1)&&(start[0]==0))
{
y1=y1+m*(xmax-x1);
x1=xmax;
}
if((end[1]==1)&&(end[0]==0))
{
y2=y2+m*(xmax-x2);
x2=xmax;
}
clrscr();
cleardevice();
printf("\n\t\tAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
getch();
}
}
else
{
clrscr();
cleardevice();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
}
getch();
closegraph();
}




ouput:




Enter the bottom-left coordinate of viewport: 100 100
Enter the top-right coordinate of viewport: 400 400
Enter the coordinates for starting point of line: 120 60
Enter the coordinates for ending point of line: 350 450










Sunday, January 17, 2010

bresenhams line drawing algorithm

program:
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"graphics.h"
#include"dos.h"
#include"math.h"

void main()
{
int driver,mode,k;
int xa,xb,ya,yb,dx,dy,x,y,i,p,dy2,dydx2;
clrscr();
printf("\n\tEnter(xa,ya)...");
scanf("%d%d",&xa,&ya);
printf("\n\tEnter(xb,yb)...");
scanf("%d%d",&xb,&yb);
x=xa;
y=ya;
driver=DETECT;
initgraph(&driver,&mode,"");
dx=abs(xb-xa);
dy=abs(yb-ya);
p=(2*dy)-dx;
dy2=2*dy;
dydx2=2*(dy-dx);
line(100,0,100,500);
line(0,100,500,100);
putpixel(x,y,2);
for(i=0;i<=dx;i++)
{
if(p<0)
{
x=x+1;
putpixel(x,y,2);
p+=dy2;
}
else
{
x=x+1;
y=y+1;
putpixel(x,y,2);
p+=dydx2;
}
}
getch();
}
output:


Enter(xa,ya)...100 100
Enter(xb,yb)...250 252




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

Tuesday, January 12, 2010

lexical analyser in c





Download this file : lexical


lexical.c

#include"stdio.h"
#include"conio.h"
void main()
{
FILE *fi,*fo,*fop,*fk;
int flag=0,i=1;
char c,t,a[15],ch[15],file[20];
clrscr();
printf("Enter the file name : ");
scanf("%s",file);
fi=fopen(file,"r");
fo=fopen("inter.c","w");
fop=fopen("oper.c","r");
fk=fopen("key.c","r");
c=getc(fi);
while(!feof(fi))
{
if(isalpha(c)||isdigit(c)||(c=='['||c==']'||c=='.'==1))
fputc(c,fo);
else
{
if(c=='\n')
fprintf(fo,"\t$\t");
else
fprintf(fo,"\t%c\t",c);
}
c=getc(fi);
}
fclose(fi);
fclose(fo);
fi=fopen("inter.c","r");
printf("\t\tLEXICAL ANALYSIS \n");
fscanf(fi,"%s",a);
printf("\nline : %d\n",i++);
while(!feof(fi))
{
if((strcmp(a,"$")==0))
{
printf("\nline : %d\n",i++);
fscanf(fi,"%s",a);
}
fscanf(fop,"%s",ch);
while(!feof(fop))
{
if(strcmp(ch,a)==0)
{
fscanf(fop,"%s",ch);
printf("\t\t%s\t:\t%s\n",a,ch);
flag=1;
}
fscanf(fop,"%s",ch);
}
rewind(fop);
fscanf(fk,"%s",ch);
while(!feof(fk))
{
if(strcmp(ch,a)==0)
{
fscanf(fk,"%s",ch);
printf("\t\t%s\t:\tkeyword\n",a);
flag=1;
}
fscanf(fk,"%s",ch);
}
rewind(fk);
if(flag==0)
{
if(isdigit(a[0]))
printf("\t\t%s\t:\tconstant\n",a);
else
printf("\t\t%s\t:\tidentifier\n",a);

}
flag=0;
fscanf(fi,"%s",a);
}
getch();
}

key.c

int
void
main
char
if
for
while
else
printf
scanf
FILE
include
stdio.h
conio.h
iostream.h

oper.c

( openpara
) closepara
{ openbrace
} closebrace
< lesser
> greater
" doublequote
' singlequote
: colon
; semicolon
# preprocessor
= equal
== assign
% percentage
^ bitwise
& reference
* star
+ add
- sub
\ backslash
/ slash

input.c

#include"stdio.h"
#include"conio.h"
void main()
{
int a=10,b,c;
a=b*c;
getch();
}
output:

Enter the file name:input.c
# : preprocessor
include : keyword
< : lesser

stdio.h : keyword
> : greater

line : 2
# : preprocessor
include : keyword
< : lesser

conio.h : keyword
> : greater

line : 3
void : keyword
main : keyword
( : openpara
) : closepara

line : 4
{ : openbrace

line : 5
int : keyword
a : identifier
= : equal
10 : constant
, : identifier
b : identifier
, : identifier
c : identifier
; : semicolon

line : 6
a : identifier
= : equal
b : identifier
* : star
c : identifier
; : semicolon

line : 7
getch : identifier
( : openpara
) : closepara
; : semicolon

line : 8
} : closebrace