Open In App

LEX Code to Identify and print Integer & Float Constants and Identifier

Last Updated : 26 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads