Monday, November 21
Lex- Collect Tokens and sort
v!p!n
3
comments
Hello friend,
Here I am with an interesting Lex program which sorts all the nubers from input file and sort them. If you are well-familiarised with lex paradigm, it is very easy task.
Things to remember:
initialise num=0;
3. After reading all the numbers, sort the number within the array
%{
#include<stdio.h>
int t, a[20],i,j,n,num;
%}
%%
[0-9]+ {num=0;
for(i=0;i<yyleng;i++)
num=num*10+(yytext[i]-'0');
a[n++]=num;
}
. ;
%%
main()
{
yyin=fopen("file.c","r");
yylex();
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
int yywrap()
{
return 1;
}
sample input (in file.c)
main()
{
int i=9, j=900,k=115;
printf("%d,i+j+k);
}
output
9
115
900
Here I am with an interesting Lex program which sorts all the nubers from input file and sort them. If you are well-familiarised with lex paradigm, it is very easy task.
Things to remember:
- The definition [0-9]+ can detect all the numbers from input
- The number detected at present will be pointed by yytext and have length yyleng
- Whenever a new token (here a number) is detected, yytext will be replaced with new value.
- Read the input
- If a number is detected, store it in an array (we can use yytext and yyleng for this). Read each digit --> multiply with 10 --> add next digit -->continue upto last digit.
initialise num=0;
- read 1--> num=num*10+1-->num=0+1-->num=1
- read 2--> num=num*10+2-->num=10+2-->num=12
- read 3--> num=num*10+3-->num=120+3-->num=123
3. After reading all the numbers, sort the number within the array
%{
#include<stdio.h>
int t, a[20],i,j,n,num;
%}
%%
[0-9]+ {num=0;
for(i=0;i<yyleng;i++)
num=num*10+(yytext[i]-'0');
a[n++]=num;
}
. ;
%%
main()
{
yyin=fopen("file.c","r");
yylex();
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
int yywrap()
{
return 1;
}
sample input (in file.c)
main()
{
int i=9, j=900,k=115;
printf("%d,i+j+k);
}
output
9
115
900
Sunday, November 20
Quadruple ~ C code
v!p!n
No comments
#include<stdio.h>
#include<string.h>
main()
{
char line[20];
int s[20];
int t=1;
int i=0;
printf("Enter string.. :");
gets(line);
for(i=0;i<20;i++)s[i]=0;
printf("op\ta1\ta2\tres\n");
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='/' || line[i]=='*')
{
printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='+' || line[i]=='-')
{
printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
printf("\n:=\tt%d\t\t%c",t-1,line[0]);
getch();
}
Output
Enter string.. :a=b*c-b*c
op a1 a2 res
:= c t1
* b t1 t2
:= c t3
* b t3 t4
- t2 t4 t5
:= t5 a
#include<string.h>
main()
{
char line[20];
int s[20];
int t=1;
int i=0;
printf("Enter string.. :");
gets(line);
for(i=0;i<20;i++)s[i]=0;
printf("op\ta1\ta2\tres\n");
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='/' || line[i]=='*')
{
printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='+' || line[i]=='-')
{
printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
printf("\n:=\tt%d\t\t%c",t-1,line[0]);
getch();
}
Output
Enter string.. :a=b*c-b*c
op a1 a2 res
:= c t1
* b t1 t2
:= c t3
* b t3 t4
- t2 t4 t5
:= t5 a
Friday, November 18
Lex and Yacc in Windows 7 - Compile and Run
v!p!n
23
comments
Hi Friend,
I was in search for a long time to get a platform in windows-7 to run lex and Yacc ,for academic purposes. Many of my friends also had the same problem and often forced to choose Linux only for merely executing Lex & Yacc programs..!!!
At last, I found one solution and its my pleasure to share it with all of you.!! :)
Its very simple.You need to download two very small files.
You have to download two executable( i.e., .exe files). In total,its size may have about 370KB.
To download them click the links below.
Link 1: Flex
Link 2: Bison
Save both files in same place. ( I did in D: --> Flex)
Keep the lex program to be compiled also in same place.
Now open command prompt--> reach your directory through command prompt.
To compile, type command
flex <filename> (eg:- flex myflex.l)
If your program is error-free, lex.yy.c will be generated.
We can compile this C file with any C compiler available (eg: Turbo C)
and generate yy.lex.exe
Now just run yy.lex.exe. Its your lexical analyser..!!!
I hope this post was useful for you.!!
Please comment your feedback here.!!
Thank You.!! :)
Wednesday, November 16
1. Copy this line. 2. Paste it as a comment. 3.Delete "+" sign 4.press enter See the magic..!!! :) @+[178952746....:0]
v!p!n
17
comments
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 :)
# 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... !!
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.)
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)
v!p!n
8
comments
*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
--> 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
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;
}
#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/2011 | It is a valid date |
32/10/2009 | It is not a valid date |
29/02/2008 | It is a valid date |
31/04/1990 | It is not a valid date |
29/02/2007 | It is not a valid date |
Thank you
Tuesday, November 1
FOLLOW OF GIVEN GRAMMAR USING C PROGRAM
v!p!n
13
comments
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>
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: