Vous êtes sur la page 1sur 9

LEX Programs

Program 1:
Program to count number of vowels and
consonants in a given string.
%{
#include<stdio.h>
int c=0,v=0;
%}
%%
[aeiouAEIOU] v++;
[a-zA-Z] c++;
%%
main()
{
printf("Enter a string");
yylex();
printf("Number of consonants = %d\nNumber of
vowels = %d\n",c,v);
}

LEX Programs

Program 2:
Program to count number of characters, words,
spaces and lines in a given input file
%{
#include<stdio.h>
int c=0,w=0,l=0,s=0;
%}
%%
" " {s++;c++;}
[\n] {l++;c++;}
[^\n\t]+ {w++;c+=yyleng;}
%%
main()
{
char fn[25];
printf("Enter a file name ");
gets(fn);
yyin=fopen(fn,"r");
yylex();
printf("Chars=%d\nSpaces=%d\nLines=%d\nWord=
%d\n",c,s,l,w);
}

LEX Programs

Program 3:
Program to count number of
(1) Positive and negative integers
(2) Positive and negative fractions
%{
#include<stdio.h>
int pi=0,ni=0,pf=0,nf=0;
%}
%%
"+"?[0-9]+
"-"[0-9]+

{pi++;}
{ni++;}

%%
main()
{
printf("Enter a set of numbers\n");
yylex();
printf("Positive\n\tIntegers=%d\n\tFractions=
%d\n\n",pi,pf);
printf("Negetive\n\tIntegers=%d\n\tFractions=
%d\n\n",ni,nf);
}

Program 4:

LEX Programs

Program to count number of comment lines in a


given C program. Also eliminate them and copy
that program into separate file.
%{
#include<stdio.h>
int count=0;
%}
%%
"/*"[A-Za-z0-9\t\n;,!()%#*&+=/><"_-]*"*/"
{ count++; fprintf(yyout,"");}
"//".* { count++; fprintf(yyout,"");}
%%
main()
{
yyin=fopen("existing.c","r");
yyout=fopen("new.c","w");
yylex();
printf("Number of Comments=%d\n",count);
}

Program 5:

LEX Programs

Program to count number of scanf and printf


statement in a C program & replace them with
readf and writef statements.
%{
#include<stdio.h>
int sf=0,pf=0;
%}
%%
"scanf" { sf++; fprintf(yyout,"readf");}
"printf" { pf++; fprintf(yyout,"writef");}
.
{ fprintf(yyout,"%s",yytext);}
'\n' { fprintf(yyout,"\n");}
%%
main()
{
yyin=fopen("existing.c","r");
yyout=fopen("new.c","w");
yylex();
printf("Number of scanf's=%d\n
Number of Printf's=%d\n",sfs,pfs);
}

Program 6:

LEX Programs

Program to recognize a valid arithmetic


expression and identify the identifiers and
operators.
%{
#include<stdio.h>
#include<string.h>
enum state{FIRST,OPD,OPT,OB,CB};
int state=0;
int oprtct=0,opndct=0,j=0,t=0,
bracket=0,brac=0;
%}
%%
[a-zA-Z][a-zA-Z0-9]* { state=OPD; opndct++;
Strcpy(oprndct[j++],yytext); }
( { if(state==FIRST||state==OPT||state==OB)
{ brac++; bracket+=2;
state=OB;
}
else return 1;
}
) { brac--; bracket-=1;
if(brac<0) return 1;
else if(state==OPD||state==CB)
state=CB;
else
return 1;
}
+|-|*|/ {state=OPT;
oprtct++;
strcpy(oprt[t++],yytext);
}
. ;
\n return 0;
%%

int main()
{

LEX Programs

int i=0;
printf(Enter an Arithmetic expression\n);
if(yylex()==0&&(opndct-oprtct)==1&&brac==0)
{
printf(Valid expression\n);
printf(Operands are\n);
for(i=0; i<j; i++)
printf(%s\t,oprnd[i]);
printf(Operators are\n);
for(i=0; i<t; i++)
printf(%s\t,oprt[i]);
printf(Number of Operands=%d\n,opndct);
printf(Number of Operators=%d\n,oprtct);
}
else
printf(Invalid Expression);
}

Program 7:
Program to recognize an English statement as
compound or simple.

LEX Programs

%{
#include<stdio.h>
int f=0;
// a flag variable
%}
%%
[Aa][Nn][Dd]|[Oo][Rr]|[Bb][Uu][Tt] f=1;
%%
main()
{
printf("Enter a sentence\n");
yylex();
if(f==1)
printf("Compound sentence");
else
printf("Simple sentence");
}

Program 8:
Program to count number of identifiers in a
given input file

LEX Programs

%{
#include<stdio.h>
int count=0;
int state=0;
enum state {START,DATATYPE,VARIABLE,RESET};
%}
%%
"int"|"float"|"char { if(state==START || state==
RESET)
state=DATATYPE;
}
[a-zA-Z][a-zA-Z0-9]* { if(state==DATATYPE ||
state==VARIABLE_
state=VARIABLE,count++;
printf("%s",yytext);
}
";" state=RESET;
. ;
[\n] ;
%%
main()
{
yyin=fopen("existing.c","r");
yylex();
printf("Number of Identifiers=%d\n",count);
}

Vous aimerez peut-être aussi