Wednesday, November 16

1. Copy this line. 2. Paste it as a comment. 3.Delete "+" sign 4.press enter See the magic..!!! :) @+[178952746....:0]


Hi friend ..
 This time I would like to share a trick to make a nice 'magic' comment in FB.
For example..the post will be like this:


1. Copy this line.
2. Paste it as a comment.
3.Delete "+" sign
4.press enter
See the magic..!!! :)

@+[258129740903222:0]



On doing  as this post, the number will automatically vanished and replaced by
"LoVe U my dear friend - by vipin" :):):)



wow.. Cool na??

You too can make your own magic comment (with your name, to ur dear ones)!!
Only 5 steps are needed.

Make your own Facebook page.
 You can find the option for page creation at the bottom side of the page as this:

#Start to make a FB page: (You can select any category. I chose the last one.)

# Then you have to enter the title for your page. Listen, the content of your magic comment should be placed here (See, I put  LoVe U my dear friend - by vipin).
It can be any comment like

I love you..!!!
~U r my best friend..!!~
Let U b ma Best frnd~ FOREVER!!!
Its wonderful na..??
I love my Nation :)




# Then several steps will come to add picture, web address,etc,etc. You can do or skip these steps. It doesn't matter.

# Finally you will get yor page like this.

#Look at the URL of the page. You can find the ID for your page.

#Uff.. The task is over.!! Now you can make your amgic comment as:

@+[page id:0]

eg:
@+[258129740903222:0]


After deleting the "+" sign put it in your comment area. FB will recognize that it is Page ID and replace the number with page title.!! :)

1. Copy this line.
2. Paste it as a comment.
3.Delete "+" sign
4.press enter
See the magic..!!! :)

@+[258129740903222:0]

ENJOY!!!!!

How is the trick?? Is it cool?? How did u feel on trying this??
 Please comment here... !!  








Tuesday, November 15

LEX program to check a Date (dd/mm/yyyy)


Hello dude..   

            I would like to discuss a problem asked by my teacher in our internal lab exam- LEX program to check the validity of a Date. Ahem..In normal programming (i.e, that with C or Java) this is not a big deal!! But in Lex paradigm,it require some more effort..!!

                                                
                                                   

*Check the syntax of Date::


* If we have to check the syntax only ,i.e., dd/mm/yyyy apply the simple logic

[0-9][0-9] \ / [0-9][0-9] \ / [0-9] [0-9] [0-9] [0-9]


* But date should be within range [01-31] ,month be within [01-12] and assume year be within the range [1000-2999]. To apply these constraints, we redefine rules as:

"01-31"----> ([0-2][0-9] | 3 [0-1])
"01-12"----> (0[1-9] | 1[0-2])
"1000-2999"---->  ([1-2][0-9][0-9][0-9])


* Thus we can simply check the syntax of given Date as:

([0-2][0-9] | 3 [0-1])  \ /  (0[1-9] | 1[0-2]) \ / ([1-2][0-9][0-9][0-9])






*To Check the validity of date::

-->  In months 01,03,05,07,08,10 &12 , there will be at most 31 days.
([0-2][0-9]|[3][0-1])\/((0(1|3|5|7|8))|(10|12))\/([1-2][0-9][0-9][-0-9]) 

--> In months 04,06,09 & 11 may have at most 30 days
([0-2][0-9]|30)\/((0(4|6|9))|11)\/([1-2][0-9][0-9][0-9])

-->February has 28 days (in linear and non-linear years)
([0-1][0-9]|2[0-8])\/02\/([1-2][0-9][0-9][0-9])

-->If February has a day 29, check whether it is leap year or not..!!
29\/02\/([1-2][0-9][0-9][0-9])
 {
extract year value;
check whether it is a leap year;
}

-->Extract year value

   1.Iterate upto two "/" in date are over.(i.e.,in dd/mm/yyyy pass over two "/"s to reach at year value.
   2.read all susequent characters (they all are part of year value - yyyy)

while(yytext[i]!='/')i++;   //reach at first "/"
i++;                        // increment pointer
while(yytext[i]!='/')i++;   //reach at next "/"
i++;                        // increment pointer
while(i<yyleng)             // read all characters upto end of the string
yr=(10*yr)+(yytext[i++]-'0');// extract integer value of year

--> Check whether it is Leap year or not:

if(yr%4==0||(yr%100==0&&yr%400!=0))

Well... the complete lex program is as follows..!!



Date.l

%{
#include<stdio.h>
int i=0,yr=0,valid=0;
%}
%%

([0-2][0-9]|[3][0-1])\/((0(1|3|5|7|8))|(10|12))\/([1-2][0-9][0-9][-0-9]) {valid=1;}

([0-2][0-9]|30)\/((0(4|6|9))|11)\/([1-2][0-9][0-9][0-9]) {valid=1;}

([0-1][0-9]|2[0-8])\/02\/([1-2][0-9][0-9][0-9]) {valid=1;}

29\/02\/([1-2][0-9][0-9][0-9]) { while(yytext[i]!='/')i++; i++;while(yytext[i]!='/')i++;i++;while(i<yyleng)yr=(10*yr)+(yytext[i++]-'0'); if(yr%4==0||(yr%100==0&&yr%400!=0))valid=1;}

%%
main()
{
yyin=fopen("vpn.txt","r");
yylex();
if(valid==1) printf("It is a valid date\n");
else printf("It is not a valid date\n");
}

int yywrap()
{
return 1;
}




OUTPUT
Content in input file
(here, vpn.txt)
 Output
12/12/2011It is a valid date
32/10/2009It is not a valid date
29/02/2008It is a valid date
31/04/1990It is not a valid date
29/02/2007It is not a valid date



Hope this post was useful for you. If you have any suggestions or doubts,please do comment here...!!
Thank you




Tuesday, November 1

FOLLOW OF GIVEN GRAMMAR USING C PROGRAM

Hello reader,
In this post I am posting implementation of FOLLOW in C.
Note:
If your production statements contain multiple terms connected by '|'  then give these terms as separate productions
 


#include<stdio.h>
#include<string.h>
int n,m=0,p,i=0,j=0;
char a[10][10],followResult[10];
void follow(char c);
void first(char c);
void addToResult(char);
int main()
{
 int i;
 int choice;
 char c,ch;
 printf("Enter the no.of productions: ");

scanf("%d", &n);

 printf(" Enter %d productions\nProduction with multiple terms should be give as separate productions \n", n);
 for(i=0;i<n;i++)
  scanf("%s%c",a[i],&ch);
    // gets(a[i]);

 do
 {
  m=0;
  printf("Find FOLLOW of -->");

  scanf(" %c",&c);
  follow(c);
  printf("FOLLOW(%c) = { ",c);
  for(i=0;i<m;i++)
   printf("%c ",followResult[i]);
  printf(" }\n");
  printf("Do you want to continue(Press 1 to continue....)?");
 scanf("%d%c",&choice,&ch);
 }
 while(choice==1);
}
void follow(char c)
{

    if(a[0][0]==c)addToResult('$');
 for(i=0;i<n;i++)
 {
  for(j=2;j<strlen(a[i]);j++)
  {
   if(a[i][j]==c)
   {
    if(a[i][j+1]!='\0')first(a[i][j+1]);

    if(a[i][j+1]=='\0'&&c!=a[i][0])
     follow(a[i][0]);

   }
  }
 }
}
void first(char c)
{
      int k;
                 if(!(isupper(c)))
                     //f[m++]=c;
                     addToResult(c);
                 for(k=0;k<n;k++)
                 {
                 if(a[k][0]==c)
                 {
                 if(a[k][2]=='$') follow(a[i][0]);
                 else if(islower(a[k][2]))
                     //f[m++]=a[k][2];
                     addToResult(a[k][2]);
                 else first(a[k][2]);
                 }
                 }

}

void  addToResult(char c)
{
    int i;
    for( i=0;i<=m;i++)
        if(followResult[i]==c)
            return;
   followResult[m++]=c;
}




Sample output:

Monday, October 17

Recursive Descent Parser using C program


Updated on 09/02/15

Hello reader,

Here is the updated post considering your valuable suggestions. check it out.


The grammar on which we are going to do recursive descent parsing is:

E -> E+T | T
T -> T*F | F
F -> (E) | id
  RD parser will verify whether the syntax of the input stream is correct by checking each character  from left to right. A basic operation necessary is reading characters from the input stream and matching then with terminals from the grammar that describes the syntax of the input.

 The given grammar can accept all arithmetic equations involving +, * and ().
eg:
 a+(a*a)  a+a*a , (a), a , a+a+a*a+a.... etc are accepted
a++a, a***a, +a, a*, ((a . . . etc are rejected.

Solution:
First we have to avoid left recursion

E -> TE'
E' -> +TE' | ε
T -> FT'
T' -> *FT' | ε
F -> (E) | id

After eliminating Left recursion, we have to simply move from one character to next by checking whether it follow the grammar. In this program, ε is indicated as $.


recursive.c

#include<stdio.h>
#include<string.h>

#include<ctype.h>
 
char input[10];
int i,error;
void E();
void T();
void Eprime();
void Tprime();
void F();
 
          main()
          {

i=0;
error=0;
                printf("Enter an arithmetic expression   :  "); // Eg: a+a*a
                gets(input);
                E();
                if(strlen(input)==i&&error==0)

                        printf("\nAccepted..!!!\n");
                else printf("\nRejected..!!!\n");
                          }
         
         
 
void E()
{
     T();
     Eprime();
}

void Eprime()
{
     if(input[i]=='+')
     {
     i++;
     T();
     Eprime();
     }
     }
void T()
{
     F();
     Tprime();
}
void Tprime()
{
     if(input[i]=='*')
     {
                      i++;
                      F();
                      Tprime();
                      }
                      }
     void F()
     {
          if(
isalnum(input[i]))i++;
          else if(input[i]=='(')
          {
          i++;
          E();
          if(input[i]==')')
          i++;


          else error=1;
            }
        
          else error=1;
          }
           


Output:
 a+(a*a)  a+a*a , (a), a , a+a+a*a+a.... etc are  accepted
++a, a***a, +a, a*, ((a . . . etc are rejected.


Monday, October 10

SEMAPHORES TO IMPLEMENT PRODUCER CONSUMER PROBLEM

#include<sys/sem.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdio.h>
#include<sys/ipc.h>
void up(int);
void down(int);
union value
{
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminto *_buf;
}
main()
{
int mutex,full,empty,pid,i,key,j,k;
union value arg;
key=ftok("",'a');
mutex=semget(key,1,0660|IPC_CREAT);
arg.val=0;
semctl(full,0,SETVAL,arg);
key=ftok("",'c');
empty=semget(key,1,0660|IPC_CREAT);
arg.val=4;
semctl(empty,0,SETVAL,arg);
pid=fork();
switch(pid)
{
case -1:
printf("Error");
case 0:
sleep(13);
printf("\n");
printf("Consumer consuming items\n");
for(i=0;i<5;i++)
{
down(full);
down(mutex);
for(j=4-i;j>=1;j--)
printf("*");
for(k=0;k<=i;k++)
printf("_");
printf("\n");
fflush(stdout);
up(mutex);
up(empty);
sleep(1);
}
printf("Buffer empty\n");
break;
default:
printf("Producer producing items\n");
for(i=0;i<5;i++)
{
down(full);
down(mutex);
//printf("producer is producing %dth item\n",i);
for(j=4-i;j>=1;j--)
printf("_");
for(k=0;k<=i;k++)
printf("*");
printf("\n");
fflush(stdout);
up(mutex);
up(full);
sleep(1);
}
printf("Buffer full\n");
break;
}}
void down(int id)
{
struct sembuf sb={0,-1,0};
semop(id,&sb,1);
}
void up(int id)
{
struct sembuf sb={0,1,0};
semop(id,&sb,1);
}

Saturday, October 8

Calculator using YACC program



Here is the Yacc program implementing a simple calculator:

%{
#include<stdio.h>
#include<ctype.h>
%}
%token num
%left '+''-'
%left '*''/'
%right '^'
%%
s:e'\n'{printf("%d",$1);}
e:    e '+' e{$$=$1+$3;}
 |e '-' e{$$=$1-$3;}
 |e '*' e{$$=$1*$3;}
 |e '/' e{$$=$1/$3;}
 |e '^' e {
  int i,j=$1;
  for(i=1;i<$3;i++)
  {
  j=j*$1;
  $$=j;
  }
  }
 |'('e')'{$$=$2;}
 |num1;
num1:num1 num{$$ = $1*10 + $2;}
 |num
 ;
%%
yylex()
{
int c;
c=getchar();
if(isdigit(c))
{
yylval=c-'0';
return num;
}
return c;
}

int main()
{
yyparse();
return 1;
}
int yyerror()
{
return 1;
}
int yywrap()
{
return 1;
}




Illustration:

Let the Input be 2+5

Moving from first definition s:e'\n'
--> move to DEF(e)
e: e '+' e is selected. Evaluate first 'e' ie e-->num1
num1 extracts the token (i.e, number here 2 is extracted and stored in $1)
Then '+' is extracted and stored in $2.
Then move to e--> num1, thus 5 is  extracted and stored  in $3.
Now add them and store result in $$.

Go back to S: e'\n' and print result (which is 'e' here..!!)


If you have any doubt regarding this post feel free to comment here.
Thank U..!!

For more Yacc programs click here..!!

YACC program to convert infix

%{#include<stdio.h>
#include<ctype.h>
%}
%token num
%left '+''-'
%left '*' '/'
%%
s:e'\n'{}
e:e'+'e{printf("+");}
|e'-'e{printf("-");}
|e'/'e{printf("/");}
|e'*'e{printf("*");}
|num1{printf("%d",$1);}
num1:num1 num {$$=$1*10+$2;}
|num
;
%%
yylex()
{
 int c;
 c=getchar();
if(isdigit(c))
{ yylval=c-'0';
 return num;
}return c;
}
int main()
{
 yyparse();
return 1;
}
int yyerror()
{
return 1;
}
int yywrap()
{
 return 1;
}