/* 1a. Write a program in LEX to count the number of vowels and consonants in a
given string. */
%{
/* No. of Vowels, Consonants & Blank characters */
int nvow=0,ncons=0,nblank=0;
%}
%%
[aeiouAEIOU] nvow++;
/* Increment the no. of Vowels */
[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] ncons++; /* Increment the no. of Consonants */
[ ] nblank++;
/* Increment the no. of BlankSpace */
%%
#include"stdio.h"
int main()
{
printf("\nEnter the string : ");
yylex();
printf("\n\nNo. of Vowels = %d",nvow);
printf("\n\nNo. of Consonants = %d",ncons);
printf("\n\nNo. of Blank Spaces = %d\n",nblank);
return 0;
}
/* 1b. Write a YACC program to identify a valid expression */
%{
#include"stdio.h"
#include"ctype.h"
%}
%%
%token P
%token R
%%
list:
|list '\n'
|list exp '\n' { printf("\nValid expression.\n"); exit(0);}
|error '\n' { yyerror(" : Invalid expression.\n"); yyerrok; exit(0); }
;
exp:
P
|exp R P
;
%%
main()
{
yyparse();
}
yylex()
{
int c;
while((c=getchar())==' ' || c=='\t');
if(isdigit(c)||isalpha(c))
return P;
if(c=='+'||c=='-'||c=='*'||c=='/')
return R;
return c;
}
yyerror(char *s)
{
printf("%s",s);
}