Saturday 28 January 2012

PRINCPLES OF COMPILER LAB PROGRAM- LEXICAL ANALYSER USING SINGLE LINE



#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int i=0,g;
char ch,st[10];
clrscr();
fp=fopen("i.c","r");
while((ch=getc(fp))!=EOF)
printf("%c",ch);
rewind(fp);
printf("\n\n");
while((ch=getc(fp))!=EOF)
{
if(isalpha(ch)||isdigit(ch)||ch=='.'||ch=='['||ch=='_')
{
st[i]=ch;
i++;
g=i;
if(ch=='[')
{
while((ch=getc(fp))!=']')
{
st[i]=ch;
i++;
}
st[i]=']';
i++;
st[i]='\0';
printf("%s\t array identifier",st);
if(isdigit(st[g+1]))
{
if(isdigit(st[g+1])&&isdigit(st[i-2]))
printf("\n");
else
printf("\t----->error\n");
}
else
printf("\n");
i=0;
continue;
}
}
else
{
st[i]='\0';
if(strcmp(st,"if")==0||strcmp(st,"then")==0||strcmp(st,"while")==0||strcmp(st,"for")==0)
{
printf("%s\t keyword \n",st);
i=0;
st[i]='\0';
}
if(strcmp(st,"int")==0||strcmp(st,"float")==0||strcmp(st,"char")==0||strcmp(st,"double")==0)
{
printf("%s\t datatype\n",st);
i=0;
st[i]='\0';
}
else
{
if(isspace(ch))
{
i=0;
continue;
}
if(isdigit(st[0]))
printf("%s\t constant\n",st);
if(st[0]!='\0'&&isalpha(st[0]))
printf("%s\t identifier\n",st);
i=0;
}
if(ch=='=')
{
ch=getc(fp);
if(ch=='=')
printf("==\t relational operator\n");
else
{
printf("=\tassignment operator\n");
st[i]=ch;
i++;
}
}
if(ch==';'||ch==',')
printf("%c\t special symbol\n",ch);
else if(ch=='('||ch==')'||ch=='{'||ch=='}')
printf("%c\t special symbol\n",ch);
else if(ch=='*'||ch=='/')
printf("%c\t arithmetic operator\n",ch);
else if(ch=='+'||ch=='-')
{
st[i]=ch;
i++;
ch=getc(fp);
if(ch=='+')
{
st[i]=ch;
i++;
st[i]='\0';
printf("%s\tincrement operator\n",st);
i=0;
continue;
}
if(ch=='-')
{
st[i]=ch;
i++;
st[i]='\0';
printf("%s\tdecrement operator\n",st);
i=0;
continue;
}
if(ch=='=')
{
st[i]=ch;
i++;
st[i]='\0';
printf("%s\t shorthand operator\n",st);
i=0;
continue;
}

else
{
printf("%s\t arithmetic operator\n",st);
i=0;
st[i]=ch;
i++;
st[i]='\0';
}
}
if(ch=='>'||ch=='<')
{
st[i]=ch;
i++;
ch=getc(fp);
if(ch=='=')
{
st[i]=ch;
i++;
st[i]='\0';
printf("%s\t relational operator\n",st);
i=0;
}
else
{
printf("%s\t relational operator\n",st);
i=0;
st[i]=ch;
i++;
st[i]='\0';
}
}
}
}
//rewind(fp);
//fclose(fp);
getch();
}

OUTPUT:

a++;a--;double a[10];if(a==10);a=a+1;a+=1;

a identifier
++ increment operator
; special symbol
a identifier
-- decrement operator
; special symbol
double datatype
a[10] array identifier
; special symbol
if keyword
( special symbol
a identifier
== relational operator
10 constant
) special symbol
; special symbol
a identifier
= assignment operator
a identifier
+ arithmetic operator
1 constant
; special symbol
a identifier
+= shorthand operator
1 constant
; special symbol
********************************

MORE:

->GIVE ADS
->EARN MONY
->DOWNLOADS(softwares,wallpapers,etc)
 (CLICK THE PICTURE BELOW)


JOB VIRUS



Tuesday 3 January 2012

Grphics c codings for MM lab



//Ellips draing using midpoint algorithm


#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 a,b;
float p1,p2;
//clrscr();
initgraph(&gd,&gm,"");
scanf("%d%d",&xc,&yc);
scanf("%d%d",&a,&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
//getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
}





//Line Drawing Algorithm - Bresenham

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
      int gd = DETECT, gm;
      int dx, dy, p, end;
      float x1, x2, y1, y2, x, y;
      initgraph(&gd, &gm, "c:\tc\bgi");
      printf("Enter Value of X1: ");
      scanf("%f", &x1);
      printf("Enter Value of Y1: ");
      scanf("%f", &y1);
      printf("Enter Value of X2: ");
      scanf("%f", &x2);
      printf("Enter Value of Y2: ");
      scanf("%f", &y2);
      dx = abs(x1 - x2);
      dy = abs(y1 - y2);
      p = 2 * dy - dx;
      if(x1 > x2)
      {
            x = x2;
            y = y2;
            end = x1;
      }
      else
      {
            x = x1;
            y = y1;
            end = x2;
      }
      putpixel(x, y, 10);
      while(x < end)
      {
            x = x + 1;
            if(p < 0)
            {
                  p = p + 2 * dy;
            }
            else
            {
                  y = y + 1;
                  p = p + 2 * (dy - dx);
            }
            putpixel(x, y, 10);
      }
      getch();
      closegraph();
}







// circle drawing algorithm-midpoint

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int x,y,p,r,d,a,b;
void mid();
void pixel(int a,int b);
void main()
{
int gm,gr;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Enter the center point:\n");
scanf("%d%d",&a,&b);
printf("Enter the radius:\n");
scanf("%d",&r);
mid();
getch();
}
void mid()
{
x=0;
y=r;
p=1-r;
while(x<y)
{
pixel(a,b);
if(p<0)
x=x+1;
else
{
x=x+1;
y=y-1;
}
if(p<0)
p=p+2*x+1;
else
p=p+2*(x-y)+1;
}
}
void pixel(int a,int b)
{
putpixel(a+x,b+y,10);
putpixel(a-x,b+y,10);
putpixel(a+x,b-y,10);
putpixel(a-x,b-y,10);
putpixel(a+y,b+x,10);
putpixel(a-y,b-x,10);
putpixel(a+y,b-x,10);
putpixel(a-y,b+x,10);
}