Lex and Yacc program that calculates number of zeros in a given string:

LEX file:

%{
#include "y.tab.c"
extern int yylval;
%}

%%
[0] {yylval=atoi(yytext); return zero;}
[1] {yylval=atoi(yytext); return one;}
%%

Yacc file:

%{
#include<stdio.h>
#include<string.h>
%}

%token zero one

%%
s : e {printf("No of zeros: %d",$1);}
   ;
e : e t {$$=$1+$2;}
   | t {$$=$1;}
   ;
t  : zero {$$=1;}
   | one {$$=0;}
   ;
%%
 main()
{
yyparse();
}
yyerror(char * arg)
{
printf("Invalid expression");
}

Comments