0% found this document useful (0 votes)
18 views5 pages

Lex Programs

The document contains several Lex and Yacc programs for various tasks, including counting characters, words, vowels, consonants, positive and negative numbers, and evaluating arithmetic expressions. Each program is accompanied by a brief description of its functionality and sample output. The document serves as a guide for implementing simple text processing and arithmetic evaluation using Lex and Yacc.

Uploaded by

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

Lex Programs

The document contains several Lex and Yacc programs for various tasks, including counting characters, words, vowels, consonants, positive and negative numbers, and evaluating arithmetic expressions. Each program is accompanied by a brief description of its functionality and sample output. The document serves as a guide for implementing simple text processing and arithmetic evaluation using Lex and Yacc.

Uploaded by

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

1.

//Lex program to count the no of Characters, Words and lines


%{
#include<stdio.h>
int lines=0, words=0,s_letters=0,c_letters=0, num=0, spl_char=0,total=0;
%}
%%
\n { lines++; words++;}
[\t ' '] words++;
[A-Z] c_letters++;
[a-z] s_letters++;
[0-9] num++;
. spl_char++;
%%
main(void)
{
yyin= fopen("myfile.txt","r");
yylex();
total=s_letters+c_letters+num+spl_char;
printf(" This File contains ...");
printf("\n\t%d lines", lines);
printf("\n\t%d words",words);
printf("\n\t%d small letters", s_letters);
printf("\n\t%d capital letters",c_letters);
printf("\n\t%d digits", num);
printf("\n\t%d special characters",spl_char);
printf("\n\tIn total %d characters.\n",total);
}

int yywrap()
{
return(1);
}
Save the file with .l extention. E.g. vowels.l

lex vowels.l
cc lex.yy.c
./a.out

2.Lex program to count the number of vowels and consonants in a given string.

%{
#include<stdio.h>
int vowels=0;
int cons=0;
%}
%%
[aeiouAEIOU] {vowels++;}
[a-zA-Z] {cons++;}
%%
int yywrap()
{
return 1;
}
main()
{
printf(“Enter the string.. at end press ^d\n”);
yylex();
printf(“No of vowels=%d\nNo of consonants=%d\n”, vowels,cons);
}

3.//Lex programs to count no of positive, negative nos.

%{
int postiveno=0;
int negtiveno=0;
int positivefractions=0;
int negativefractions=0;
%}
DIGIT [0-9]
%%
\+?{DIGIT}+ postiveno++;
-{DIGIT}+ negtiveno++;
\+?{DIGIT}*\.{DIGIT}+ positivefractions++;
-{DIGIT}*\.{DIGIT}+ negativefractions++;
.;
%%
main()
{
yylex();
printf("\nNo. of positive numbers: %d",postiveno);
printf("\nNo. of Negative numbers: %d",negtiveno);
printf("\nNo. of Positive fractions: %d",positivefractions);
printf("\nNo. of Negative fractions: %d\n",negativefractions);
}
$ lex a.l
$ cc lex.yy.c
$ ./a.out

4 Sample YACC Program to evaluate the arithmetic expressions

#include<stdio.h>
%}
%token NUM
%left '-''+'
%right '*''/'
%%
start: exp {printf("%d\n",$$);}
exp:exp'+'exp {$$=$1+$3;}
|exp'-'exp {$$=$1-$3;}
|exp'*'exp {$$=$1*$3;}
|exp'/'exp
{
if($3==0)
yyerror("error");
else
{
$$=$1/$3;
}
}
|'('exp')' {$$=$2;}
|NUM {$$=$1;}
;
%%
main()
{
printf("Enter the Expr. in terms of integers\n");
if(yyparse()==0)
printf("Success\n");
}
yywrap(){}
yyerror()
{
printf("Error\n");
}
yacc calci.y
cc y.tab.c
./a.out

Sample Output

Enter the expression in terms of integers:


5*8+3
43

5. Yacc program implementing a simple calculator:


%{
#include<stdio.h>
#include<ctype.h>
%}
%token num
%left '+''-'
%left '*''/'
%right '^'
%%
s:e'\n'{printf("%d",$1);}
e: e '+' e{$$=$1+$3;}
|e '-' e{$$=$1-$3;}
|e '*' e{$$=$1*$3;}
|e '/' e{$$=$1/$3;}
|e '^' e {
int i,j=$1;
for(i=1;i<$3;i++)
{
j=j*$1;
$$=j;
}
}
|'('e')'{$$=$2;}
|num1;
num1:num1 num{$$ = $1*10 + $2;}
|num
;
%%
yylex()
{
int c;
c=getchar();
if(isdigit(c))
{
yylval=c-'0';
return num;
}
return c;
}
int main()
{
yyparse();
return 1;
}
int yyerror()
{
return 1;
}
int yywrap()
{
return 1;
}
6.//Program to evaluate the expression valid or not
%{
#include<stdio.h>
%}
%token ID NUMBER
%left '+' '-'
%left '*' '/'
%%
stmt:expr
;
expr: expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| '(' expr ')'
| NUMBER
| ID
;
%%
void main()
{
printf("enter expr : \n");
yyparse();
printf("valid exp");
exit(0);
}
void yyerror()
{
printf("invalid exp");
exit(0);
}

You might also like