Social Icons

Saturday, 3 May 2014

Symbol Table Creation


To generate tokens
%%
{letter}({letter}|{digit})*       printf(“id: %s\n”, yytext);
%%
--------------------

1.
%{
#include<string.h>
%}
%%
(int|float|double|long|short|return|include|char) { printf("\n< Keyword,%s >",yytext);func(yytext,"Keyword");}
[\"] { printf("\n< Operator,%s >",yytext);func(yytext,"Operator"); }
['"'][\\a-zA-Z0-9|' ']+['"'] { printf("\n< id,%s >",yytext); func(yytext,"String");}

\+\+ { printf("\n< %s >",yytext);func(yytext,"Operator"); }
\-\- { printf("\n< %s >",yytext); func(yytext,"Operator");}
\=\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\!\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\+\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
\-\= { printf("\n< %s >",yytext); func(yytext,"Operator");}
[/|'.']+ { printf("\n< %s >",yytext); func(yytext,"Operator");}
(printf|scanf) { printf("\n< function,%s >",yytext);func(yytext,"Function");}

[0-99]+ { printf("\n<Number,%s >",yytext);func(yytext,"Number"); }
[a-zA-Z|_][0-99|a-zA-Z|_]* { printf("\n< variable,%s>",yytext); func(yytext,"variable");}
['+'|\-|'='|':'|'%'|'&'|'('|')'|'{'|'}'|','|'#'|'!'|'@'|'$'|'*'|'<'|'>'|'?'] { printf("\n< %s >",yytext);func(yytext,"Operator");}

[';']+ { printf("\n< %s >",yytext);func(yytext,"Operator");}
\n func2();
%%
struct entry
{
char name[10];
char token[10];
}st[10];
int i=0;
func(char n[10],char t[10])
{
int j,flag=0;
for(j=0;j<i;j++)
{
if(strcmp(n,st[j].name)==0)
flag=1;
}
if(flag==0)
{
strcpy(st[i].name,n);
strcpy(st[i].token,t);
i++;
}
}
func2()
{
int k;
printf("\n===========Symbol Table==================\n");
for(k=0;k<=i;k++)
printf("%s   %s\n",st[k].name,st[k].token);
}



Friday, 2 May 2014

Bouncing Sphere in Opengl

#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>
float x=2,y=0,d=0;
void display()
{      
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
   
   
     
      glPushMatrix();
      glTranslatef(0.0,-2.0+y,0.0);
      glRotatef(x,1.0,0.0,0.0);
      glutSolidSphere(0.5,30,40);
     
      glPopMatrix();
      glFlush();

 glutPostRedisplay();
 }
void reshape(GLint w, GLint h) {

  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
 
    glOrtho(-2.5, 2.5, -2.5/aspect, 2.5/aspect, -10.0, 10.0);
  } else {
 
    glOrtho(-2.5*aspect, 2.5*aspect, -2.5, 2.5, -10.0, 10.0);
  }

}



void timer(int v)
{
x=x+0.03;
     

 if(d==0)
  {glColor3f(1.0,0.0,0.0);
   y=y+0.03;
   if(y>4)
    d=1;
  }
   if(d==1)
  {
  glColor3f(1.0,1.0,0.0);
   y=y-0.03;
   if(y<-0.8)
    d=0;
  }


 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 600);
  glutCreateWindow("Cyan Shapes in Yellow Light");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);

  glutMainLoop();
}

Solar System Using Opengl IN Graphics


#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>

//float x=0.0;
float a[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};//angle of Rotation
float speed[10]={0.0,0.06,0.05,0.04,0.03,0.02,0.01,0.008,0.007,0.006};//speed of Rotation
float radius[10]=   {5.0,1.5,1.2,1.0,2.0,1.3,1.0,1.0,1.0,1.0};//planet Size
float distancex[10]={0.0,7.0,10.0,13.0,17.0,22.0,26.0,29.0,32.0,35.0};//DIstance Between Planets

  GLfloat red[] = { 1.0, 0.0, 0.0, 1.0 };
  GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  GLfloat cyan[] = { 0.0, 1.0, 1.0, 1.0 };
  GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
  GLfloat magenta[] = { 1.0, 0.0, 1.0, 1.0 };
  GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
  GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
  GLfloat skyblue[] = { 0.2, 0.2, 1.0, 1.0 };
  GLfloat mitty[] = { 0.8, 0.8, 1.0, 1.0 };

void display()
{
       int i;
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glMatrixMode(GL_MODELVIEW);

        for(i=0;i<10;i++)
        {
            glPushMatrix();
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,white);
             glutWireTorus(0.001,distancex[i],50,50);
            if(i==4)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,red);
            else if(i==3)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,blue);
            else if(i==0)
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,yellow);
            else
            glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,mitty);
            glRotatef(a[i],0.0,0.0,1.0);
            glTranslatef(distancex[i],0.0,0.0);
             if(i==6)
            {
                glRotatef(-45,1.0,1.0,0.0);
                glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE,green);
                glutWireTorus(0.2,1.5,20,20);
            }

            glPushMatrix();
            glRotatef(a[i],0.0,1.0,0.0);
            glutSolidSphere(radius[i],30,10);
            glPopMatrix();

              glPopMatrix();
          }

      glutSwapBuffers();
}

void timer(int vaulue)
{
    int i;
    for(i=1;i<10;i++)
       {
           a[i] = a[i]+speed[i];
       }
       glutPostRedisplay();
       glutTimerFunc(1000/500,timer,1);
}

void reshape(GLint w, GLint h) {
   glViewport(0, 0, w, h);
 // glClearColor(1.0,1.0,1.0,1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
gluLookAt(5.0,-5.0,10.0,.0,0.0,0.0,0.0,0.0,1.0);
  glMatrixMode(GL_PROJECTION);
  GLfloat aspect = (GLfloat)(w) / (GLfloat)(h);
  glLoadIdentity();
  if (w <= h) {
    // width is smaller, so stretch out the height
    glOrtho(-40.0, 40.0, -40.0/aspect, 40.0/aspect, -40.0, 40.0);
  } else {
    // height is smaller, so stretch out the width
    glOrtho(-40.0*aspect, 40.0*aspect, -40.0, 40.0, -40.0, 40.0);
  }
}

void init() {
 GLfloat direction[] = { 5.0, 0.0, 0.0, 0.0 };
 GLfloat direction1[] = { -5.0, 0.0, 0.0, 0.0 };
 glMaterialf(GL_FRONT, GL_SHININESS, 50);
  glLightfv(GL_LIGHT0, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT0, GL_POSITION, direction);
  glLightfv(GL_LIGHT1, GL_DIFFUSE,yellow);
    glLightfv(GL_LIGHT1, GL_POSITION, direction1);

  glEnable(GL_LIGHTING);                // so the renderer considers light
  glEnable(GL_LIGHT0);                  // turn LIGHT0 on
  glEnable(GL_LIGHT1);
  glEnable(GL_DEPTH_TEST);              // so the renderer considers depth
}

// The usual application statup code.
int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(600, 600);
  glutCreateWindow("Solar System Simulation");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
   glutTimerFunc(1000/10,timer,1);
  init();
  glutMainLoop();
}

Bouncing Ball In Opengl

#include <GL/glut.h>
#define true 1
#define false 0
static spinning = false;
static const int FPS = 60;
float flag=1,u=0.0,d=0.0;
static GLfloat currentAngleOfRotation = 45.0;

void reshape(GLint w, GLint h) {

  GLfloat aspect = (GLfloat)w / (GLfloat)h;
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  if (w <= h) {
    glOrtho(-50.0, 50.0, -50.0/aspect, 50.0/aspect, -1.0, 1.0);
//     f1=1;
  } else {
    glOrtho(-50.0*aspect, 50.0*aspect, -50.0, 50.0, -1.0, 1.0);
 //    f2=1;
  }
}

void display() {
   float angle,x,y;
   int i;
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(currentAngleOfRotation, 0.0, 0.0, 1.0);
  glScalef(10.0,10.0,10.0);
//  printf("f1=%d f2=%d",f1,f2);
 
  glBegin(GL_POINTS);
   for(i=0;i<360;i++)
   {
    angle=2*3.14*i/360;
    x=cos(angle);
    y=sin(angle);
    glVertex2f(x+u,y+u);
   }

  glEnd();
  glFlush();
  glutSwapBuffers();
}

void timer(int v)
{

 if(d==0)
  {
  u=u+0.03;
  if(u>2.9)
  d=1;
  }
   if(d==1)
  {
  u=u-0.03;
  if(u<-2.9)
  d=0;
  }
 

 glutPostRedisplay();
 glutTimerFunc(1000/60,timer,v);

}



int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(80, 80);
  glutInitWindowSize(800, 500);
  glutCreateWindow("Spinning Square");
  glutReshapeFunc(reshape);
  glutDisplayFunc(display);
  glutTimerFunc(100, timer, 0);
 
  glutMainLoop();
}

 

Sample Text

Sample text

 
Just Programming Cse DriveReputation Management