0% found this document useful (0 votes)
485 views

Lex and Yacc Programs

The document contains several Lex programs with different purposes: 1) Counting vowels, consonants, types of numbers, printf/scanf statements 2) Identifying simple and compound statements, counting identifiers 3) Counting words, characters, blank spaces, lines, comment lines 4) Checking validity of arithmetic statements 5) Finding number of constants The Lex programs use regular expressions and actions to analyze input text files and count/classify elements for various programming tasks.

Uploaded by

trichy_sathish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
485 views

Lex and Yacc Programs

The document contains several Lex programs with different purposes: 1) Counting vowels, consonants, types of numbers, printf/scanf statements 2) Identifying simple and compound statements, counting identifiers 3) Counting words, characters, blank spaces, lines, comment lines 4) Checking validity of arithmetic statements 5) Finding number of constants The Lex programs use regular expressions and actions to analyze input text files and count/classify elements for various programming tasks.

Uploaded by

trichy_sathish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

lex and yacc programs

Lex program to count number of vowels and consonanta


%{

int v=0,c=0;

%}

%%

[aeiouAEIOU] v++;

[a-zA-Z] c++;

%%

main()

printf("ENTER INTPUT : \n");

yylex();

printf("VOWELS=%d\nCONSONANTS=%d\n",v,c);

Lex program to count the type of numbers


%{

int pi=0,ni=0,pf=0,nf=0;

%}

%%

\+?[0-9]+ pi++;

\+?[0-9]*\.[0-9]+ pf++;

\-[0-9]+ ni++;

\-[0-9]*\.[0-9]+ nf++;

%%

main()

{
printf("ENTER INPUT : ");

yylex();

printf("\nPOSITIVE INTEGER : %d",pi);

printf("\nNEGATIVE INTEGER : %d",ni);

printf("\nPOSITIVE FRACTION : %d",pf);

printf("\nNEGATIVE FRACTION : %d\n",nf);

Lex program to count the number of printf and scanf


statements
%{

#include "stdio.h"

int pf=0,sf=0;

%}

%%

printf {

pf++;

fprintf(yyout,"%s","writef");

scanf {

sf++;

fprintf(yyout,"%s","readf");

%%

main()

yyin=fopen("file1.l","r+");

yyout=fopen("file2.l","w+");

yylex();
printf("NUMBER OF PRINTF IS %d\n",pf);

printf("NUMBER OF SCANF IS %d\n",sf);

Lex program to find simple and compound statements


%{

}%

%%

"and"|

"or"|

"but"|

"because"|

"nevertheless" {printf("COMPOUNT SENTANCE"); exit(0); }

. ;

\n return 0;

%%

main()

prntf("\nENTER THE SENTANCE : ");

yylex();

printf("SIMPLE SENTANCE");

Lex program to count the number of identifiers


%{

#include<stdio.h>

int id=0,flag=0;

%}

%%
"int"|"char"|"float"|"double" { flag=1; printf("%s",yytext); }

";" { flag=0;printf("%s",yytext); }

[a-zA-Z][a-zA-z0-9]* { if(flag!=0) id++; printf("%s",yytext); }

[a-zA-Z0-9]*"="[0-9]+ { id++; printf("%s",yytext); }

[0] return(0);

%%

main()

printf("\n *** output\n");

yyin=fopen("f1.l","r");

yylex();

printf("\nNUMBER OF IDENTIFIERS = %d\n",id);

fclose(yyin);

int yywrap()

return(1);

Lex program to count the number of words,characters,blank


spaces and lines
%{

int c=0,w=0,l=0,s=0;

%}

%%

[\n] l++;

[' '\n\t] s++;


[^' '\t\n]+ w++; c+=yyleng;

%%

int main(int argc, char *argv[])

if(argc==2)

yyin=fopen(argv[1],"r");

yylex();

printf("\nNUMBER OF SPACES = %d",s);

printf("\nCHARACTER=%d",c);

printf("\nLINES=%d",l);

printf("\nWORD=%d\n",w);

else

printf("ERROR");

Lex program to count the number of comment lines


%{

#include<stdio.h>

int cc=0;

%}

%%

"/*"[a-zA-Z0-9' '\t\n]*"*/" cc++;

"//"[a-zA-Z0-9' '\t]* cc++;

%%

main()

{
yyin=fopen("f1.l","r");

yyout=fopen("f2.l","w");

yylex();

fclose(yyin);

fclose(yyout);

printf("\nTHE NUMBER OF COMMENT LINES = %d\n",cc);

Lex program to check the validity of arithematic statement


%{

#include<stdio.h>

int opr=0,opd=0;

int n;

%}

%%

[\+\-\*\/] { printf("OPERATORS ARE %s\n",yytext);

opr++;

[a-zA-Z]+ { printf("OPERANDS ARE %s\n",yytext);

opd++;

[0-9]+ { printf("OPERANDS ARE %s\n",yytext);

opd++;

[a-zA-Z]+\+\-\*\/[a-zA-Z]+ { n=0; }

[0-9]+\+\-\*\/[0-9]+ { n=0; }

%%

main()

{
printf("\nENTER THE EXPRESSION : \n");

yylex();

printf("\nNUMBER OF OPERATORS ARE %d",opr);

printf("\nNUMBER OF OPERANDS ARE %d",opd);

if((n==0)&&(opd==opr+1))

printf("\nVALID EXPRESSION\n");

else

printf("\nINVALID EXPRESSION\n");

Lex program to find the number of constants


%{

#include<stdio.h>

int cons=0;

%}

%%

[0-9]+ { printf("\n%s",yytext); cons++; }

. ;

%%

main(int argc,char *argv[])

if(argc==2)

yyin=fopen(argv[1],"r");

yylex();

printf("\nNUMBER OF CONSTANTS : %d\n",cons);


}

else

printf("\nERROR");

You might also like