LEX Code to Identify and print Integer & Float Constants and Identifier
Last Updated :
23 Jul, 2025
In this article, we will discuss how you can solve the problem, and also you will see how you can design problems related to DFA in LEX Code to Identify and print Integer & Float Constants and identifiers. Let's discuss it one by one.
Problem Overview :
Design a DFA in LEX Code to Identify and print Integer & Float Constants and identifiers.
Note -
Regular Expression for Integer, float, and Identifier is as follows.
Integer - [0-9]+
Float - [0-9]+[.][0-9]+
Identifier - [A-Za-z_][A-Za-z0-9_]+
Example -
Input : 35
Output : Integer
Input : 3.98
Output : Float
Input : kashyap
Output : Identifier
Input : 123Singh
Output : Invalid
Approach :
LEX provides us with an INITIAL state by default. So to make a DFA, use this initial state as the initial state of the DFA. Define four more states A, B, C, and DEAD. DEAD is the dead state that would be used if an invalid or wrong input is encountered and it prints “Invalid”. A is used when an integer is encountered, which prints “Integer” and B is used when a floating constant is encountered which prints “Float” and C is used when an identifier is encountered, which prints “Identifier”. A new line character (\n) marks the end of the input so transition to the INITIAL state and print the output or print "Not Accepted" or "Invalid."

Note:-
To compile the lex program we need to have a Unix system that has flex installed into it. Then we need to save the file with .l extension. For Example- filename.l Then after saving the program closes the lex file and then open the terminal and write the following commands.
lex filename.l
cc lex.yy.c
./a.out
Method 1
LEX Code -
C
// Declaration Section
%{
%}
%s A B C DEAD // Declaring states
// Rules Section
%%
<INITIAL>[0-9]+ BEGIN A;
<INITIAL>[0-9]+[.][0-9]+ BEGIN B;
<INITIAL>[A-Za-z_][A-Za-z0-9_]* BEGIN C;
<INITIAL>[^\n] BEGIN DEAD;
<INITIAL>\n BEGIN INITIAL; {printf("Not Accepted\n");}
<A>[^\n] BEGIN DEAD;
<A>\n BEGIN INITIAL; {printf("Integer\n");}
<B>[^\n] BEGIN DEAD;
<B>\n BEGIN INITIAL; {printf("Float\n");}
<C>[^\n] BEGIN DEAD;
<C>\n BEGIN INITIAL; {printf("Identifier\n");}
<DEAD>[^\n] BEGIN DEAD;
<DEAD>\n BEGIN INITIAL; {printf("Invalid\n");}
%%
// Auxiliary Functions
int yywrap()
{
return 1;
}
int main()
{
printf("Enter String\n");
yylex();
return 0;
}
Output :

Method 2
If you want to print the identified tokens as well then you can apply the following Lex code to print the matched integer, float and identifier.
LEX Code-
C
//Declaration Section
%{
#include<stdlib.h>
int num_int; // stores integer
char *str; // stores identifier
double num_float; // stores float
%}
%s A B C DEAD //Declaring States
//Rules Section
%%
<INITIAL> [0-9]+ BEGIN A; {num_int = atoi(yytext);}
<INITIAL> [0-9]+"."[0-9]+ BEGIN B; {num_float = atof(yytext);}
<INITIAL> [a-zA-Z_][a-zA-Z0-9_]* BEGIN C; {str = yytext;}
<INITIAL> [^\n] BEGIN DEAD;
<INITIAL> [\n] BEGIN INITIAL; {printf("Not Accepted\n");}
<A> [^\n] BEGIN DEAD;
<A> \n BEGIN INITIAL; {printf("%d Integer Accepted\n", num_int);}
<B> [^\n] BEGIN DEAD;
<B> \n BEGIN INITIAL; {printf("%lf Float Accepted\n", num_float);}
<C> [^\n] BEGIN DEAD;
<C> \n BEGIN INITIAL; {printf("%sIdentifier Accepted\n", str);}
<DEAD> [^\n] BEGIN DEAD;
<DEAD> \n BEGIN INITIAL; {printf("Invalid\n");}
%%
//Auxiliary Functions
int yywrap()
{
return 1;
}
int main(){
printf("Enter String:\n");
yylex();
return 0;
}
Output:

Similar Reads
LEX program to print the longest string and to find average of given numbers Lex :The Lex program has purpose of generating lexical analyzer. Lex analyzers are a program that transform input streams into sequence of tokens. A C program implements the lexical analyzer to read input stream and produce output as the source code. Commands to be used - lex file_name.l // To produ
2 min read
Python bit functions on int (bit_length, to_bytes and from_bytes) The int type implements the numbers.Integral abstract base class. 1. int.bit_length() Returns the number of bits required to represent an integer in binary, excluding the sign and leading zeros. Code to demonstrate Python3 1== num = 7 print(num.bit_length()) num = -7 print(num.bit_length()) Output:
1 min read
Data Type Ranges and Their Macros in C++ Most of the times, in competitive programming, there is a need to assign the variable, the maximum or minimum value that data type can hold but remembering such a large and precise number comes out to be a difficult job. Therefore, C++ has certain macros to represent these numbers, so that these can
3 min read
std::to_string in C++ In C++, the std::to_string function is used to convert numerical values into the string. It is defined inside <string> header and provides a simple and convenient way to convert numbers of any type to strings.In this article, we will learn how to use std::to_string() in C++.Syntaxstd::to_strin
1 min read
C# Identifiers In programming languages, identifiers are used for identification purposes. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name, or label. Example: public class GFG { static public void Main () { int
2 min read
Symbol Table in Compiler Every compiler uses a symbol table to track all variables, functions, and identifiers in a program. It stores information such as the name, type, scope, and memory location of each identifier. Built during the early stages of compilation, the symbol table supports error checking, scope management, a
8 min read