Saviour Lex
Saviour Lex
yacc -d assign20.y
lex assign20.l
CODE:
%{
#include <stdio.h>
%}
%%
[0-9]+ {printf("\nINteger");}
[a-zA-Z]+ {printf("\nAlphabets");}
%%
void main()
yylex();
int yywrap()
{
return 1;
%{
#include <stdio.h>
int n,i,sum=0;
%}
%%
[0-9]+ {n=atoi(yytext);
for(i=1;i<=n;i++)
if(n%i==0)
%%
int main()
printf("Enter input");
yylex();
%{
#include<stdio.h>
int n,i,sum=0;
%}
%%
[0-9]+ {n=atoi(yytext);
for(i=1;i<n;i++)
if(n%i==0)
sum=sum+i;
if(sum==n)
else
%%
void main()
yylex();
int yywrap()
return 1;
%{
#include<stdio.h>
int i,b=0,r,p=1,num;
%}
%%
[0-9]+ {num=atoi(yytext);
while(num>0)
r=num%2;
b+=r*p;
p*=10;
num/=2;
%%
void main()
yylex();
int yywrap()
return 1;
#include<stdio.h>
int n,r,s=0;
%}
%%
[0-9]+ {n=atoi(yytext);
while(n!=0)
r=n%10;
n=n/10;
s=s+r;
%%
void main()
yylex();
int yywrap()
return 1;
%{
#include<stdio.h>
int lc=0,sc=0,ch=0,wc=0;
%}
%%
%%
int main()
yylex();
return 0;
int yywrap()
return 1;
NOTE: TO CHECK OUTPUT PRESS (Ctrl+D once or twice) AFTER ENTERING THE SENTENCE OR
SENTENCES.
%{
#include <stdio.h>
%}
%%
%%
void main()
yylex();
int yywrap()
return 1;
#include<stdio.h>
%}
%%
%%
void main()
yylex();
int yywrap()
return 1;
%{
#include<stdio.h>
int count=0;
%}
%%
if|else|do|while|break|switch|continue|for|void|int|main|return|case {count++;}
%%
void main()
printf("\nEnter keyword:");
yylex();
}
int yywrap()
return 1;
%{
#include<stdio.h>
int c,j,flag=0;
%}
%%
[0-9]+ {c=atoi(yytext);
if(c==2)
printf("\n prime");
else
if(c% j==0)
flag=1;
if(flag==1)
printf("\n prime");
%%
void main()
yylex();
int yywrap()
return 1;
%{
#include <stdio.h>
#include<string.h>
int i=0,o=0,k,flag=0;
char id[10][10],op[10][10];
%}
%%
%%
void main()
if(flag!=1)
else
for(k=0;k<o;k++)
printf("%s\t",op[k]);
for(k=0;k<i;k++)
printf("%s\t",id[k]);
int yywrap()
return 1;
12. lex code to check whether a given string starts with vowel or not.
%{
#include <stdio.h>
%}
%%
.|\n ;
%%
int main() {
yylex();
return 0;
int yywrap(){
return 1;
13.lex code to take number as input in txt file and check number of odd and even numbers.
%{
#include<stdio.h>
int c1=0,c2=0;
%}
%%
[0-9]*[0|2|4|6|8] {c1++;}
[0-9]*[1|3|5|7|9] {c2++;}
%%
void main()
yyin = fopen("file15.txt","r");
yylex();
}
int yywrap()
return 1;
14. lex code to take words as input in txt file and check frequency of words if present.
%{
#include<stdio.h>
#include<string.h>
char word[]="love";
int c=0;
%}
%%
%%
void main(){
yyin = fopen("input16.txt","r");
yylex();
if(c==0)
printf("\nNot Found");
else
printf("\nFound %d times.",c);
int yywrap(){
return 1;
LEX CODE:
%{
#include "y.tab.h"
%}
%%
\n return 0;
%%
int yywrap()
return 1;
YACC CODE:
%{
#include <stdio.h>
int valid=1;
%}
%%
Start: letter S
S: letter S
| digit S
%%
int yyerror() {
printf("in It is not identifier");
valid = 0;
return 0;
void main()
yyparse();
if(valid)
printf("In Identitiar");
return;
yacc -d assign20.y
lex assign20.l
%{
#include <stdio.h>
%}
%start A DEAD
%%
<INITIAL>0 BEGIN A;
%%
int main() {
yylex();
return 0;
int yywrap()
return 1;
%{
#include <stdio.h>
#include <string.h>
} else {
%}
%%
.* { check_bb(yytext); }
%%
yylex();
return 0;
int yywrap(){
return 1;
18. lex code to replace ‘A’with ‘Best’ and store in another file.
%{
#include<stdio.h>
#include<string.h>
char replace_with[]="Best";
char replace[]="A";
%}
%%
[a-zA-Z]+ {if(strcmp(yytext,replace) == 0)
fprintf(yyout,"%s",replace_with);
else
fprintf(yyout,"%s",yytext);}
. {fprintf(yyout,"%s",yytext);}
%%
int yywrap(){
return 1;
void main(){
yyin =fopen("input.txt","r");
yyout = fopen("output.txt","w");
yylex();
%{
#include<stdio.h>
#include<string.h>
void check(char*);
%}
%%
[a-zA-Z]+ check(yytext);
%%
void main(){
yylex();
FILE *fp;
char temp[100];
fp = fopen("input1.txt","r");
if(!strcmp(temp,str)){
return;
int yywrap(){
return 1;
LEX CODE:
%{
/* Definition section */
#include "y.tab.h"
%}
/* Rule Section */
%%
\n {return NL;}
. {return yytext[0];}
%%
int yywrap()
return 1;
YACC CODE:
%{
/* Definition section */
#include<stdio.h>
#include<stdlib.h>
%}
%token A B NL
/* Rule Section */
%%
exit(0); }
S: A S B |
%%
printf("invalid string\n");
exit(0);
//driver code
void main()
yyparse();
%{
#include<stdio.h>
int i,j,flag=0;
%}
%%
[a-zA-Z0-9]+ {for(i=0,j=yyleng-1;i<=j;i++,j--)
if(yytext[i]==yytext[j])
flag=1;
else
flag=0;
break;
if(flag==1)
printf("\nIt is Palindrome");
else
%%
void main()
yylex();
int yywrap()
return 1;