allLexCodes
allLexCodes
Program
%{
#include <stdio.h>
%}
%%
[a-zA-Z] { printf("Recognized letter: %s\n", yytext); }
. { }
%%
int main() {
yylex();
return 0;
}
int yywrap() {
return 1;
}
Output
Q2) Write a Lex Program to recognize letters and show invalid for others.
Program
%{
#include <stdio.h>
%}
%%
[a-zA-Z] { printf("Recognized letter: %s\n", yytext); }
. { printf("Invalid character: %s\n", yytext); }
%%
int main() {
yylex();
return 0;
}
int yywrap() {
return 1;
}
Output
Q3) Write a Lex Program to recognize alphabets, numbers, and special character.
Program
%{
#include <stdio.h>
%}
%%
[a-zA-Z] { printf("Recognized alphabet: %s\n", yytext); }
[0-9] { printf("Recognized number: %s\n", yytext); }
[^a-zA-Z0-9\n] { printf("Recognized special character: %s\n", yytext); }
\n {}
%%
int main() {
yylex();
return 0;
}
Output
Q4) Write a Lex Program to check whether a given input is a valid integer.
Program
%{
#include <stdio.h>
#include <stdlib.h>
%}
%%
[+ ]?[0-9]+ { printf("Positive number: %s\n", yytext); }
[-]?[0-9]+ {printf("Negative number: %s\n", yytext); }
. {}
%%
int main() {
printf("Enter a string to check if it's a valid integer:\n");
yylex();
return 0;
}
Output
Q5) Write a Lex Program to convert upper case letter to lowercase.
Program
%{
#include <stdio.h>
%}
%%
[A-Z] {
printf("%c", yytext[0] + 'a' - 'A');
}
.|\n {
printf("%s", yytext);
}
%%
int main() {
printf("Enter text to convert uppercase letters to lowercase:\n");
yylex();
return 0;
}
Output
Q6) Write a Lex Program to convert lower case letter to uppercase.
Program
%{
#include <stdio.h>
%}
%%
[a-z] { printf("%c", yytext[0] - 'a' + 'A'); }
.|\n { printf("%s", yytext); }
%%
int main() {
printf("Enter text to convert lowercase letters to uppercase:\n");
yylex();
return 0;
}
Output
Q7) Write a lex program to take file as an input and determine an alphabet, number and
arithmetic and logical operators from the file.
Program
%{
#include <stdio.h>
#include <ctype.h>
%}
%%
[a-zA-Z] { printf("Alphabet: %s\n", yytext); }
[0-9]+ { printf("Number: %s\n", yytext); }
[-+*/] { printf("Arithmetic Operator: %s\n", yytext); }
&& { printf("Logical Operator: &&\n"); }
\|\| { printf("Logical Operator: ||\n"); }
.|\n {}
%%
int main() {
char filename[100];
printf("Enter the filename: ");
scanf("%s", filename);
FILE *file = fopen(filename, "r");
if (!file) {
printf("Error: Unable to open file %s\n", filename);
return 1;
}
yyin = file;
yylex();
fclose(file);
return 0;
}
Input File Contents
inputFile.txt
Hello && 123
Output
Q8) Write a Lex program that takes an input from the C program and print out all the
keywords in capital letters.
Program
%{
#include <stdio.h>
int i;
%}
keyword main|int|scanf|printf|if|else|include|void
%%
{keyword} { for(i=0;i<yyleng;i++)printf("%c",toupper(yytext[i])); }
. { printf("%s", yytext); }
%%
int main()
{
yyin=fopen("num.c","r");
yylex();
}
Input File
num.c
#include<stdio.h>
void main()
{
int num1;
printf("Enter Number :: ");
scanf("%d", &num1);
}
Output
Q9) Write a Lex program to identify a valid C identifier.
Program
%{
#include <stdio.h>
%}
%%
^[a-zA-Z_][a-zA-Z0-9_]* printf("Valid Identifier");
^[^a-zA-Z_] printf("Invalid Identifier");
.;
%%
void main()
{
yylex();
}
Output
Q10) Write a Lex program to identify floating point numbers.
Program
%{
#include<stdio.h>
%}
%%
-[0-9]* {printf("The %s is a negative integer number \n", yytext);}
[0-9]* {printf("The %s is a integer number \n", yytext);}
-[0-9]*(.)[0-9]* {printf("The %s is a negative floating nunber \n", yytext);}
[0-9]*(.)[0-9]* {printf("The %s is a floating nunber \n", yytext);}
.* {printf("Invalid Number\n");}
%%
void main()
{
printf("Enter the Number :: ");
yylex();
}
Output
Q11) Write a lex program to identify a valid if statement in c.
Program
%{
#include<stdio.h>
%}
%%
"if"/[\(.*\)] {printf("The given statement is valid if statement \n");}
. {printf(" ");}
%%
void main()
{
printf("Enter the if statement :: ");
yylex();
}
Output
Q12) Write a LEX program that takes a file as an input and counts the uppercase, lowercase,
lines, words and special characters
Program
%{
#include<stdio.h>
int upperCase=0, lowerCase=0, lines=0, words=0, specialChar=0;
%}
%%
[A-Z] {upperCase++;}
[a-z] {lowerCase++;}
[ \t] {words++;}
[\n] {lines++;words++;}
. {specialChar++;}
%%
void main()
{
yyin=fopen("File.txt", "r");
yylex();
printf("\nWords :: %d\nLines :: %d\nUpperCase :: %d\nLowerCase :: %d\nSpecialWord ::
%d\n", words, lines, upperCase, lowerCase, specialChar);
}
Input File
Output
Q13) Write a lex program that takes a file as an input and remove all the white spaces and
new line and store it in a different file
Program
%{
#include<stdio.h>
%}
%%
[ \n\t]+ {fprintf(yyout,"");}
. {fprintf(yyout,"%s",yytext);}
%%
void main()
{
yyin=fopen("File.txt", "r");
yyout=fopen("resFile.txt", "w");
yylex();
}
Input File
Output File