LEX Code to Identify and print Integer & Float Constants and Identifier
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.
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_]+
Input : 35 Output : Integer Input : 3.98 Output : Float Input : kashyap Output : Identifier Input : 123Singh Output : Invalid
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.”
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
LEX Code –
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.