Showing posts with label Count. Show all posts
Showing posts with label Count. Show all posts

Count of Words Starting with a - Lex Program - Compiler Design

Program:

// Lex file: aa.l

%{
        int count=0;
%}
alpha    [a-zA-Z]
digit      [0-9]
space    [ \t\n]
start      ^a
%%

{start}                                           {count++;}
{space}(a|A)({alpha}|{digit})*    {count++;}
.                                                      ;

%%

main()
{
    yylex();
    printf("count= %d\n",count);
}
Output:

nn@linuxmint ~ $ lex aa.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<tst.txt

count= 6
nn@linuxmint ~ $



// tst.txt

afsal ARIFA aaa www.2k8618.blogspot.com
aiswarya saranya    sooraj
arun reshmi
a www.2k8cse.cu.cc

Select Lines Ending with 'com' - Lex Program - Compiler Design

Program:

// Lex file: com.l

%{
int count=0;
%}

%%
.*com\n {count++;ECHO;}
. ;

%%

main()
{
    yylex();
    printf("\nCount= %d\n",count);
    return 0;
   
}

Output:
nn@linuxmint ~ $ lex com.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<com.txt

www.google.com
www.yahoo.com
www.2k8618.blogspot.com




Count= 3
nn@linuxmint ~ $

// com.txt

www.2k8cs.tk
www.google.com
www.yahoo.com
www.2k8618.blogspot.com
www.2k8cse.cu.cc

Count The Positive numbers, Negative numbers & Fractions - Lex Program - Compiler Design

Aim:
Write a lex program to count the number of Positive numbers, Negative numbers & Fractions.

Program:

// lex file: a.l

%{
    int postiveno=0;
    int negtiveno=0;
    int positivefractions=0;
    int negativefractions=0;
%}

DIGIT [0-9]
%%

\+?{DIGIT}+                              postiveno++;
-{DIGIT}+                                  negtiveno++;

\+?{DIGIT}*\.{DIGIT}+            positivefractions++;
-{DIGIT}*\.{DIGIT}+                negativefractions++;
. ;   
%%

main()
{
    yylex();
    printf("\nNo. of positive numbers: %d",postiveno);
    printf("\nNo. of Negative numbers: %d",negtiveno);
    printf("\nNo. of Positive fractions: %d",positivefractions);
    printf("\nNo. of Negative fractions: %d\n",negativefractions);
}

Output:

nn@linuxmint ~ $ lex a.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<a.txt


No. of positive numbers: 2
No. of Negative numbers: 3
No. of Positive fractions: 4
No. of Negative fractions: 5
nn@linuxmint ~ $

// Input file: a.txt
+12,-123,1.1,-1.1,12,-2,-3,2.1,3.2,5.1,-5.5,-6.1,-7.7,-8.8

Count the Vowels - Lex Program

Program:

// lex file: vw.l

%{
int count=0;
%}

%%

[aeiouAEIOU] {count++;ECHO;}

%%
main()
{
    yylex();
    printf("\nNumber of vowels= %d\n",count);
    return 0;
}

Output:

nn@linuxmint ~ $ lex vw.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<vw.txt
www.2k8618.blogspot.com
www.2k8cs.tk
www.google.com
www.gmail.com


Number of vowels= 10
nn@linuxmint ~ $

Count The Number of lines ending with "com" - Lex Program - Compiler Design

Program:

//Lex file: com.l

%{
int count=0;
%}
DIGIT [0-9]
ALPHA [a-zA-Z]
%%
({ALPHA}|{DIGIT})*com {count++;}
%%

main()
{
    yylex();
    printf("Count= %d\n",count);
    return 0;
   
}

Output :

n@linuxmint ~ $ lex com.l
nn@linuxmint ~ $ gcc lex.yy.c -ll
nn@linuxmint ~ $ ./a.out<com.txt
www.2k8618.blogspot.
www.2k8cs.tk
www.google.
www.gmail.

Count= 3
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...