Home »
Compiler Design
LEX Code for Tokenizing (Identify and print OPERATORS, SEPARATORS, KEYWORDS, IDENTIFIERS) for the given C fragment
In this article, we going to learn how to create LEX program to analysis among the given C program which are operators, separators, keywords, identifiers and print on the console?
Submitted by Ashish Varshney, on March 24, 2018
C code
int p=1,d=0,r=4;
float m=0.0, n=200.0;
while (p <= 3)
{
if(d==0)
{ m= m+n*r+4.5; d++; }
else
{ r++; m=m+r+1000.0; }
p++;
}
For achieve to this task we create some rules in rule section in the code.
/*Definition Section*/
%{
#include<stdio.h>
%}
/*Rule Section*/
%%
/*Check the token, if token is match with pattern
then it will print KEYWORD.*/
auto|double|int|struct|break|else|long|switch
|case|enum|register|typedef|char|extern|return
|union|continue|for|signed|void|do|if|static
|while|default|goto|sizeof|volatile|const|float
|short {ECHO; printf("\nKEYWORD\n");}
/*Check the token, if token is match with pattern
then it will print SEPERATOR.*/
[{};,()] {ECHO; printf("\tSEPERATOR\t");}
/*Check the token, if token is match with pattern
then it will print OPERATOR.*/
[+-/=*%] {ECHO; printf("\tOPERATOR\t");}
/*Check the token, if token is match with pattern
then it will print IDENTIFIER.*/
([a-zA-Z][0-9])+|[a-zA-Z]* {ECHO; printf("\tIdentifier\t");}
/*No action*/
.|\n ;
%%
/*call the yywrap function*/
int yywrap()
{
return 1;
}
/*Auxiliary function*/
/*Driver function*/
int main(void)
{
/*call the yylex function.*/
yylex();
return 0;
}
Output