Tag Archives: Lex

Lex : Check Valid Email

%{
#include<stdio.h>
#include<stdlib.h>
int flag=0;
%}
%%
[a-z . 0-9]+@[a-z]+".com"|".in" { flag=1; }
%%
int main()
{
yylex();
if(flag==1)
printf("Accepted");
else
printf("Not Accepted");
}

/*
Enter input
press Enter & ctrl+d to get output
*/

Output :-
abc@gmail.com
Accepted

Lex & Yacc : Acceptance of string

Ques : Write a lex and yacc program to accept strings over {0,1} from the set {00,0000,000000….. , 11,1111,111111…..}

// (yacc file)
%{
#include<stdlib.h>
#include<stdio.h>
void yyerror(char *s);
%}
%token Z O
%%
S : S A {printf("Accepted ");}
| A ;
A : Z Z
| O O ;
%%
int main()
{
yyparse();
}
void yyerror(char *s)
{
fprintf(stdout,"\nNot Accepted !\n");
}

// Lex File

%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
0 {yylval=0;return Z;}
1 {yylval=1;return O;}
[ \t] {;}
\n return 0;
. return yytext[0];
%%

Output :-
00
Accepted !
0101
Not Accepted !

Lex : Number is prime or not

%{
#include<stdio.h>
#include<stdlib.h>
int flag,c,j;
%}
%%
[0-9]+ {c=atoi(yytext);}
%%
int main()
{
yylex();
if(c==2)
{
printf("\n Prime number");
}
else if(c==0 || c==1)
{
printf("\n Not a prime number");
}
else
{
for(j=2;j<c;j++)
{
if(c%j==0)
flag=1;
}
if(flag==1)
printf("\n Not a prime number");
else if(flag==0)
printf("\n Prime number");
}
}

Conversion of arithmatic expression into postfix expression for given grammar

Grammars Are
E=E+T      E=T    T=T*F    T=F    F=NUM

// ( .y  File )

%{
#include<stdlib.h>
#include<stdio.h>
void yyerror(char *s);
%}
%token NUM
%%
E : E '+' T {printf("+");}
| T ;
T : T '*' F {printf("*");}
| F ;
F : NUM {printf("%d",yylval);};
%%
int main()
{
yyparse();
}
void yyerror(char *s)
{
fprintf(stdout,"\n%s",s);
}

// (.l File )

%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUM;}
[ \t] {;}
\n return 0;
. return yytext[0];
%%

/* To Compile :-
1) yacc -d filename.y
2) lex filename.l
3) gcc lex.yy.c y.tab.c -ll
4) ./a.out
Enter Expression like:  1+2*3
*/

Lex : Check Whether number is Even or Odd

%{
#include<stdio.h>
#include<stdlib.h>
int i;
%}
%%
[0-9]+ {i=atoi(yytext); if(i%2==0) printf("Even !"); else printf("Odd !");};
%%
int main()
{
yylex();
}
/*  To Compile :-
1) lex filename.l
2) gcc lex.yy.c -ll
3) ./a.out
Enter Input and press enter
*/

Output :-
145
Odd !
204
Even !

Conversion from binary to decimal

<br />//(Yacc File : Save as .y)

//to compile and Run
//:-> yacc -d FileName.y
//:-> lex FileName.l
//:-> gcc lex.yy.c y.tab.c -ll
//:-> ./a.output
//:-> enter input as - 101
//:-> output:-> 5

%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *s);
%}
%token ZERO ONE
%%
N: L {printf("\n%d",$$);}
L: L B {$$=$1*2+$2;}
| B {$$=$1;}
B:ZERO {$$=$1;}
|ONE {$$=$1;};
%%
int main()
{
while(yyparse());
}
yyerror(char *s)
{
fprintf(stdout,"\n%s",s);
}

// (Lex File : save as .l)

%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
0 {yylval=0;return ZERO;}
1 {yylval=1;return ONE;}

[ \t] {;}
\n return 0;
. return yytext[0];
%%

Output :-
101
5

LEX & YACC : Calculator

<br />//    Yacc  File (.y)

//calculator

%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *s);
%}
%token NAME NUM
%%
statement: NAME'='expression
|expression{printf("\n%d\n",$1);};
expression:expression'+'NUM{$$=$1+$3;}
|expression'-'NUM{$$=$1-$3;}
|expression'/'NUM{$$=$1/$3;}
|expression'*'NUM{$$=$1*$3;}
|NUM{$$=$1;};
%%
int main()
{
while(yyparse());
}
yyerror(char *s)
{
fprintf(stdout,"\n%s",s);
}

// Lex file (.l)

%{
#include<stdio.h>
#include<stdlib.h>
#include"y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {yylval=atoi(yytext);return NUM;}
[ \t] {;}
\n return 0;
. return yytext[0];
%%

/*
To Compile :
1) yacc -d FileName.y
2) lex FileName.l
3) gcc lex.yy.c y.tab.c -ll
4) ./a.out
*/

Output :-
5+6
11

LEX : Count Vowels & Consonants in a String

%{
#include<stdio.h>
int vcount=0,ccount=0;
%}
%%
[a|i|e|o|u|E|A|I|O|U] {vcount++;}
[a-z A-Z (^a|i|e|o|u|E|A|I|O|U) ] {ccount++;}
%%
int main()
{
yylex();
printf("No. of Vowels :%d\n",vcount);
printf("No. of Consonants :%d\n",ccount);
return 0;
}

/* To Compile :
1) lex FileName.l
2) gcc lex.yy.c -ll
3) ./a.out
Type String and then press ctrl+D to get the result.
*/

Output :-

hello
No. of Vowels :2
No. of Consonants :3