Skip to main content

lex program to count the number of fraction Positive and negative integer


/* decleration section in this sections we will decleared the different  value and include the header file which we are using in this program to run this program */
%{
#include<stdio.h>
int pf_count=0;
int nf_count=0;
%}

/* this sections is known as defined section in which we defined the rule and regulation of regular expression which will going to accept or not */

%%
[+]?([0-9])+(\.([0-9])+)? {

/* this is for fraction as eg: 2.03, +1 ,+0.3*/

pf_count++; printf("Positive fraction Integer:%s\n",yytext);}

[-]([0-9])+(\.([0-9]+))?   {

/* this will accept the negative fraction as eg: -3.3,-6 */

nf_count++; printf("Negative  fraction Integer:%s\n",yytext);}

%%

/* main function for calling the function */

void main(int argc[],char *argv[])
{
yylex();
printf("the total number of Postive Integer are: %d\n" ,pf_count);
printf("the total number of Negative  Integer are:%d\n",nf_count);
}
                              
download the file

 run this program we need to do some steps  as follows

1 step- save as .l extension and run "flex  filename.l "
 2 step- there would be a c program generated by flex analyser which may be "lex.yy.c"  run this file as this command "gcc lex.yy.c -o first -ll" or "cc lex.yy.c -o first -ll"

 3rd step- for output "./a.out " and enter the input after complete input press ctrl + D so that input sections will complete  ,it will display the result



Comments