Digital Assignment 8 - LEX Programming
Digital Assignment 8 - LEX Programming
Digital Assignment 8 - LEX Programming
programming
Name Prathyush Kodhanpur
Reg No 21BCE0930
Faculty Bhuvaneshwari M
%{
#include <stdio.h>
%}
sign [+-]?
digit [0-9]+
exp ([eE]{sign}?{digit})
%%
\+?{digit} { printf("\nNumber is positive...\n"); }
\-{digit} { printf("\nNumber is negative...\n"); }
{sign}?{digit}(\.{digit})? { printf("\nNumber is real...\n"); }
{sign}?{digit}(\.{digit})?{exp} { printf("\nNumber is exponentia
. ;
%%
int yywrap() {
return 1;
}
int main() {
%{
/*
1. Request input of an even and an odd number
2. Indicate input characteristic: Even/Odd
3. Check for input's correctness and print result
*/
#include <stdio.h>
int number_1;
int number_2;
number_sequence [0-9]+
%%
{number_sequence}[02468] {
printf("Even number [%s]\n", yytext);
return 1;
}
{number_sequence}[13579] {
printf("Odd number [%s]\n", yytext);
return 1;
}
. ;
%%
int yywrap() {
return 1;
}
int main() {
printf("Input two numbers:\n");
yylex();
yylex();
return 0;
}
%{
#include<stdio.h>
int sc=0,wc=0,lc=0,cc=0;
%}
%%
[\n] { lc++; cc+=yyleng;}
[ \t] { sc++; cc+=yyleng;}
[^\t\n ]+ { wc++; cc+=yyleng;}
%%
int yywrap( )
{
%{
%}
%%
[A-Z]+ {
printf("Uppercase word: %s\n", yytext);
}
[ \t\n,"]+ {
/* Remove space, tab, newline, comma, and double quote */
}
%%
int yywrap() {
return 1;
}
int main() {
yyin = fopen("input.txt", "r");
yylex();
fclose(yyin);
}
%{
#include<stdio.h>
int vow=0, con=0;
%}
%%
[ \t\n]+ ;
[aeiouAEIOU]+ {vow++;}
[^aeiouAEIOU] {con++;}
%%
int main( )
{
printf("Enter some input string:\n");
yylex();
printf("Number of vowels=%d\n",vow);
printf("Number of consonants=%d\n",con);
}
%{
/* Definition section */
#include<stdio.h>
#include<stdlib.h>
int flag,c,j;
/* Rule Section */
%%
[0-9]+ {c=atoi(yytext);
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");
}
}
%%
// driver code
int main()
{
yylex();
return 0;
}
%{
#include<stdio.h>
int valid=0;
%}
%%
([0-2][0-9]|3[0-1])\/(0[1-9]|1[0-2])\/([1-2][0-9][0-9][0-9]) {va
%%
main()
{
yyin=fopen("input.txt","r");
yylex();
if(valid==1) printf("It is a valid date\n");
else printf("It is not a valid date\n");
}
int yywrap()
{
return 1;
%%
((http)|(ftp))s?:\/\/[a-zA-Z0-9]{2,}(\.[a-z]{2,})+(\/[a-zA-Z0-9+
. { printf("\nURL Invalid\n"); }
%%
int main() {
printf("\nEnter URL: ");
yylex();
printf("\n");
return 0;
}
%{
#include<stdio.h>
int sc=0,wc=0,lc=0,cc=0;
%}
%%
[\n] { lc++; cc+=yyleng;}
[ \t] { sc++; cc+=yyleng;}
[^\t\n ]+ { wc++; cc+=yyleng;}
%%
int yywrap( )
{
return 1;
}
%{
#include<stdio.h>
%}
%%
\<[^>]*\> { fprintf(yyout, "%s\n", yytext); }
.|\n ; /* Ignore all other characters */
%%
int yywrap() {
return 1;
}
int main() {
FILE *yyin = fopen("input7.html", "r");
FILE *yyout = fopen("output7.txt", "w");
if (!yyin || !yyout) {
perror("Error opening file");
return 1;
}
yyrestart(yyin);
fclose(yyin);
fclose(yyout);
return 0;
}