Home » Compiler Design

LEX Code to identify and print integer and float value in given Input pattern

In this article, we going to learn how to create LEX program to analysis whether a input number is integer number or decimal number?
Submitted by Ashish Varshney, on March 24, 2018

For achieve to this task we create some rules in rule section in the code.

Example:

    Input: 56
    Output: Integer number
    Input: 0.2
    Output: Decimal Number
/*Definition Section*/

%{
#include<stdio.h>
%}


/*Rule Section*/

%%

/*Check the token, if token is match with 
pattern then it will print Decimal Number.*/
[0-9]+"."[0-9]+ {ECHO; printf("\nDecimal Number\n");}  
/*Check the token, if token is match with 
pattern then it will print Integer Number.*/
[0-9]+ {ECHO; printf("\nInteger Number\n");}			

%%
/*call the yywrap function*/
int yywrap()		
{return 1;}


/*Auxiliary function*/
/*Driver function*/

int main(void)
{
/*call the yylex function.*/
yylex();
return 0;
}


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.