Showing posts with label consonant. Show all posts
Showing posts with label consonant. Show all posts

Wednesday, 11 January 2017

Lex Programs

Program to count
a.  Number of vowels and consonants in a given string.
b.  Numbers of comment lines in a given C program.
c.  Number of
     i. Positive and negative integers
     ii.  Positive and negative fractions
d. Number of characters, words, spaces and lines in a given input file. Also eliminate them and copy that program into separate file.

a)
%{
int v=0;
int c=0;
%}

%%
[aeiouAEIOU] {v++;}
[a-zA-Z] {c++;}
%%

void main()
{
printf("Enter string :\n");
yylex();
printf("vowels :%d\n",v);
printf("consonants :%d\n :",c);
}


b)


%{
#include<stdio.h>
int s=0;
int line_num = 0;
%}
%x comment
%%
"/*"         BEGIN(comment);++line_num;
<comment>[^*\n]*        /* eat anything that's not a '*' */
<comment>"*"+[^*/\n]*   /* eat up '*'s not followed by '/'s */
<comment>\n             ++line_num;
<comment>"*"+"/"        BEGIN(INITIAL);
"//".* ++line_num;
%%
main()
{
yyin=fopen("in.c","r");
yyout=fopen("in2.c","w");
yylex();
printf("No. of line=%d\n",line_num);
}

in.c
#include<stdio.h>
int main()//main file
/******
****fdg*/
//
{
int d,e;
printf("printf("b");");
scanf("enter %d",&d,e);
}

in2.c
#include<stdio.h>
int main()
{
int d,e;
printf("printf("b");");
scanf("enter %d",&d,e);
}

c)


Pnif.l
%{
#include<stdio.h>
int p_i=0;
int n_i=0;
int p_f=0;
int n_f=0;
%}
%%
\+?[0-9]+([eE]"+"?[0-9]+)? {p_i++;}
\+?[0-9]+([eE]"-"[0-9]+)? {p_f++;}
-[0-9]+([eE]"+"?[0-9]+)? {n_i++;}
-[0-9]+([eE]"-"[0-9]+)? {n_f++;}
\+?[0-9]+"."[0-9]+([eE]("+"|"-")?[0-9]+)? {p_f++;}
-[0-9]+"."[0-9]+([eE]("+"|"-")?[0-9]+)? {n_f++;}
%%
yywrap()
{
return 1;
}
void main()
{
printf("Enter the numbers \n ");
yylex();
printf("\n no of +ve integers are %d \n",p_i);
printf("\n no of -ve integers are %d \n",n_i);
printf("\n no of +ve Fractions are %d \n",p_f);
printf("\n no of -ve Fractions are %d \n",n_f);
}
 

d)
Count2.l
%{
int lineCount, wordCount, charCount,space;
%}
LETTER [a-zA-Z0-9]

%%
\n     lineCount++;
{LETTER}+    { wordCount++; charCount+=yyleng; }
" " charCount++;space++;
.      charCount++;


%%
int main () {
       yyin=fopen("a.txt","r");
       yyout=fopen("b.txt","w");
       yylex();
       printf ("Input contains \n");
       printf ("%d spaces\n%d lines\n%d words\n%d chars\n",space, lineCount, wordCount, charCount);
       return 0;
}


a.txt
hello how ARE
haha fight

2. Program to count the number of ‘scanf’ and ‘printf’ statements in a C program. Replace them with ‘readf’ and ‘writf’ statements respectively.

prinfscanf.l

%{
#include<stdio.h>
int p=0,s=0;
%}
%x print scan
%%
"printf" BEGIN(print);fprintf(yyout,"%s","writef");
<print>[(]["] fprintf(yyout,"%s",yytext);
<print>[^)] fprintf(yyout,"%s",yytext);
<print>[)] BEGIN(INITIAL); p++; fprintf(yyout,"%s",yytext);
"scanf" BEGIN(scan);fprintf(yyout,"%s","readf");
<scan>[(]["] fprintf(yyout,"%s",yytext);
<scan>[^)] fprintf(yyout,"%s",yytext);
<scan>[)] BEGIN(INITIAL); s++; fprintf(yyout,"%s",yytext);
%%
main()
{
yyin=fopen("in.c","r");
yyout=fopen("in2.c","w");
yylex();
printf("No. of scanf=%d\n",s);
printf("No. of printf=%d\n",p);
}



in.c


#include<stdio.h>
int main()
{
int d,e;
printf("printf("b");");
d=e;
scanf("enter %d",&d,e);
}




 in2.c


#include<stdio.h>
int main()
{
int d,e;
writef("printf("b");");
d=e;
readf("enter %d",&d,e);
}



 

 

 Thank You For Reading.

Please Comment the queries and doubt
Also programs you require.