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

Digital Assignment 8 - LEX Programming

This document contains 7 examples of LEX programs. The examples include programs to: 1. Recognize real, integer, and exponential numbers. 2. Scan even and odd numbers. 3. Count the number of lines, words, characters in a file. 4. Remove uppercase words and whitespace from a file. 5. Count vowels and consonants in a string. 6. Check if a number is prime. 7. Check if a date is valid. The document also provides 2 additional LEX code examples to check URLs and count words in a paragraph.

Uploaded by

Prat
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)
142 views

Digital Assignment 8 - LEX Programming

This document contains 7 examples of LEX programs. The examples include programs to: 1. Recognize real, integer, and exponential numbers. 2. Scan even and odd numbers. 3. Count the number of lines, words, characters in a file. 4. Remove uppercase words and whitespace from a file. 5. Count vowels and consonants in a string. 6. Check if a number is prime. 7. Check if a date is valid. The document also provides 2 additional LEX code examples to check URLs and count words in a paragraph.

Uploaded by

Prat
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/ 13

Digital Assignment 8- LEX

programming
Name Prathyush Kodhanpur

Reg No 21BCE0930

Course BCSE307L L11 + L12

Date @March 19, 2024

Faculty Bhuvaneshwari M

1. LEX program to recognise real, integer and exponential numbers

%{
#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() {

Digital Assignment 8- LEX programming 1


char myString[100];
fgets(myString, sizeof(myString), stdin);
yy_scan_string(myString);
yylex();
return 0;
}

2. LEX code to scan even and odd numbers

%{
/*
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;

Digital Assignment 8- LEX programming 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;
}

Digital Assignment 8- LEX programming 3


3. PROGRAM TO COUNT THE NUMBER OF LINES USING FILE

%{
#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 main(int argc ,char* argv[ ])


{
printf("Enter the input:\n");
yylex();
printf("The number of lines=%d\n",lc);
printf("The number of spaces=%d\n",sc);
printf("The number of words=%d\n",wc);
printf("The number of characters are=%d\n",cc);
}

int yywrap( )
{

Digital Assignment 8- LEX programming 4


return 1;
}

4. LEX program to remove UPPERCASE and WHITESPACE

%{
%}
%%
[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);
}

Digital Assignment 8- LEX programming 5


5. LEX program to count the number of vowels and consonants in a given string

%{
#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);
}

Digital Assignment 8- LEX programming 6


int yywrap( )
{
return 1;
}

6. LEX program to check whether a number is prime or not

/* Lex Program to check whether a number is Prime or Not */

%{
/* Definition section */
#include<stdio.h>
#include<stdlib.h>
int flag,c,j;

Digital Assignment 8- LEX programming 7


%}

/* 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;
}

Digital Assignment 8- LEX programming 8


7. Lex program to check if a date is valid or not. Format of Date is DD/MM/YYYY

%{
#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;

Digital Assignment 8- LEX programming 9


Examples

1. LEX code to check URL links.

%%
((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;
}

Digital Assignment 8- LEX programming 10


2. LEX program to count number of words in a paragraph

%{
#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 main(int argc ,char* argv[ ])


{
printf("Enter the input:\n");
yylex();
printf("The number of lines=%d\n",lc);
printf("The number of spaces=%d\n",sc);
printf("The number of words=%d\n",wc);
printf("The number of characters are=%d\n",cc);
}

int yywrap( )
{
return 1;
}

Digital Assignment 8- LEX programming 11


3. LEX code to extract HTML tags

%{
#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);

Digital Assignment 8- LEX programming 12


yyout = yyout;
yylex();

fclose(yyin);
fclose(yyout);
return 0;
}

Digital Assignment 8- LEX programming 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy