Tuesday 1 April 2014

15. Lex program to count number of spaces in the file

The question is self explanatory

Here is the code:

%{
    int i=0;
%}
%%
[ ] {printf(" ");i++;}
%%

main()
{
extern FILE *yyin;
yyin=fopen("new","r");
yylex();
printf("\nTotal spaces = %d\n",i);
}

14. Lex program to remove every word that start with t

The question is self explanatory

Here is the code:


%{
%}
%%
t[a-z A-Z]+ printf("");
%%

main()
{
extern FILE *yyin;
yyin=fopen("new","r");
yylex();
}

13.Lex program to print every words in parenthesis

 The program shows every words in file "new" in parenthesis.

For example

I am going
We are going

becomes

{I} {am} {going}
{We} {are} {going}

%{
   
%}
%%
[a-zA-Z]+ {printf("{%s}",yytext);}
%%

main()
{
extern FILE *yyin;
yyin=fopen("new","r");
yylex();
}

12. Lex program to put every line in parenthesis

The program puts every line in parenthesis.
For example

I am going
We are going

appears

{I am going}
{We are going}

Here is the code :

%{
   
%}
%%
[a-z A-Z]+ {printf("{%s}",yytext);}
%%

main()
{
extern FILE *yyin;
yyin=fopen("new","r");
yylex();
}

11. Lex program to remove all newline characters

It removes all new line characters and prints everything in a line.

For example

I am
Going

appears

I am Going

Here is the code:

%{
%}
%%
\n    printf("");
%%
main()
{
    extern FILE *yyin;
    yyin=fopen("new","r");
    yylex();
}

10.Lex program to print every lines in a file with its line number

Question is self explanatory

Here is the code:


%{
    int i=0;
%}
%%
[a-z A-Z]+ {printf("%d:%s",i,yytext);i++;}
%%

main()
{
extern FILE *yyin;
yyin=fopen("new","r");
yylex();
}

9. Lex program to view file contents with double line spacing

It reads every line and prints it. It gives a line gap between every line.

for example
I am going
She is going

appears

I am going

She is going


%{
   
%}
%%
\n {printf("\n\n",yytext);}
%%

main()
{
extern FILE *yyin;
yyin=fopen("new","r");
yylex();
}

8. Lex program to extract email IDs from a file

Note : This program might not work with all email types. But works with the trivial ones like example@aa.com


Here is the code:

%{
%}
%%
[a-zA-Z0-9]+.[@].[a-zA-Z0-9]+.[/.].[a-zA-Z.]+ printf("%s",yytext);
[a-zA-Z 0-9]+    printf(" ");
%%
main()
{
    extern FILE *yyin;
    yyin=fopen("new","r");
    yylex();
}

7. Lex program to count the number of vowels in a file

The program searches for vowels inside every words and count the number.

for example in the line "A man is Walking" there are 5 vowels namely "a,a,i,a,i"

Here is the code :

%{
int count=0;
%}
%%
[aeiouAEIOU] {printf("%s",yytext);count++;}   
%%
main()
{
    extern FILE *yyin;
    yyin=fopen("new","r");
    yylex();
    printf("\nTotal=%d",count);
}

6.Lex program to print words in a file that starts with a vowel

The question is self explanatory


Here is the code:

%{
%}
%%
[aeiouAEIOU].[a-zA-Z0-9.]+    printf("%s",yytext);
[a-zA-Z0-9]+    printf(" ");
%%
main()
{
    extern FILE *yyin;
    yyin=fopen("new","r");
    yylex();
}

5. Lex program to print the words in a file that doesnot start with a vowel

The question is self explanatory.

 Here is the code:




%{
%}
%%
[aeiouAEIOU].[a-zA-Z0-9.]+    printf("");
[a-zA-Z0-9]+    printf("%s",yytext);
%%
main()
{
    extern FILE *yyin;
    yyin=fopen("new","r");
    yylex();
}

4. Lex program to check whether an year is a leap year or not

Leap years are the years that are divisible by 400 or are divisible by 4 and not divisible by 100.
So 2000,2012 etc are leap years while 1900, 2001 etc are not

Here is the code:

%{
void check(char *);
%}
%%   
[0-9]    ;
[0-9][0-9]    ;
[0-9][0-9][0-9]     ;
[0-9][0-9][0-9][0-9]    {printf("%s",yytext);check(yytext);}
[0-9][0-9][0-9][0-9][0-9]+ ;
%%
main()
{
    extern FILE *yyin;
    yyin=fopen("num","r");
    yylex();
}
void check(char *a)
{
    int x=0,i;
    for(i=0;i<4;i++)
        x=x*10+(a[i]-'0');
    if(x%400==0)   
        printf("\tYES\n");
    else if(x%4==0&&x%100!=0)
        printf("\tYES\n");
    else
        printf("\tNO\n");
}

3. Lex program to search a word in a file

     To search a word, I have stored words in file named "new" and used lex to receive words from keyboard. On receiving each word, I check it with the words in the file. If the word is found, the program prints "FOUND" else "NOT FOUND".

Here is the code :

%{
#include<string.h>
void check(char *);
%}
%%
[a-zA-Z]+ check(yytext);
%%
main()
{
    yylex();
}
void check(char *str)
{
    FILE *fp;
    char temp[30];
    fp=fopen("new","r");
    while((fscanf(fp,"%s",temp))!=EOF)
    {
        if(!(strcmp(temp,str)))
        {
            printf("FOUND\n");
            return;
        }
       
    }
    printf("NOT FOUND\n");
        return;
}

2.Lex program to check perfect numbers

    Perfect number are numbers who are equal to the sum of all its positive divisors less than itself.
    for example 6=1+2+3

Here is the code:


%{
#include<string.h>
void check(char *);
%}
%%
[0-9]+ check(yytext);
%%
main()
{
    extern FILE *yyin;
    yyin=fopen("num","r");
    yylex();
}
void check(char *a)
{
    int len=strlen(a),i,num=0;
    for(i=0;i<len;i++)
        num=num*10+(a[i]-'0');
    int x=0,temp=num;
    for(i=1;i<num;i++)
    {
        if(num%i==0)
            x=x+i;
    }
    if(x==temp)
        printf("%d is perfect \n",num);
    else
        printf("%d is not perfect \n",num);
}

1.To check whether the given number in the file is armstrong or not

     Armstrong numbers are numbers whose sum of digits each raised to the power of number of digit is equal to the initial number.
    for example 351=(3*3*3)+(5*5*5)+(1*1*1)=27+125+1=153
                       370=(3*3*3)+(7*7*7)+(0*0*0)=27+343+0=370
                       1=1
All single digit numbers are armstrong and no two digit armstrong numbers are there.

Here is the lex program that takes  input from a file named "num"


%{
#include<string.h>
#include<math.h>
void check(char *);
%}
%%
[0-9]+ check(yytext);
%%
main()
{
    extern FILE *yyin;
    yyin=fopen("num","r");
    yylex();
}
void check(char *a)
{
    int len=strlen(a),i,num=0;
    for(i=0;i<len;i++)
        num=num*10+(a[i]-'0');
    int x=0,temp=num;
    while(num>0)
    {
        x=x+pow(num%10,len);
        num=num/10;
    }
    if(x==temp)
        printf("%d is armstrong \n",temp);
    else
        printf("%d is not armstrong \n",temp);
}