Inputs
javatoken.txt
import java.sql.*;
class Hello{
public static void main(String args[]){
System.out.println("HI");
//printing
}
}
class Hello{
public static void main(String args[]){
System.out.println("HI");
//printing
}
}
javatoken.l
%{
#include<stdio.h>
%}
DATATYPE int|char|bool|float|double|String
SC [;]
DOTOP [.]
OPERATOR "+"|"-"|"/"|"*"|"="
UNARY "++"|"--"
IDENTIFIER [a-zA-Z]+[a-zA-Z0-9]*
COMMENT "//"[a-zA-Z0-9]*
PREPROCESSOR "import"[a-zA-Z0-9 "." "*"]*
KEYWORD public|static|void|main|abstract|class
BLOCK_S "{"
BLOCK_E "}"
BRACKET ")"|"("
STRING "\""[a-zA-Z0-9]*"\""
%%
{DOTOP} {printf("");}
{BRACKET} {printf("\n %s: bracket",yytext);}
{DATATYPE} {printf("\n %s: datatype",yytext);}
{SC} {printf("\n %s: semi colon \n",yytext);}
{OPERATOR} {printf("\n %s: operator \n",yytext);}
{UNARY} {printf("\n %s: unary \n",yytext);}
{BLOCK_S} {printf("\n %s: block start",yytext);}
{BLOCK_E} {printf("\n %s: block end",yytext);}
{COMMENT} {printf("\n %s: comment",yytext);}
{PREPROCESSOR} {printf("\n %s: preprocessor",yytext);}
{KEYWORD} {printf("\n %s: keyword",yytext);}
{STRING} {printf("\n %s: string",yytext);}
{IDENTIFIER} {printf("\n %s: identifier",yytext);}
%%
int yywrap(){
return (1);
}
main(){
yyin=fopen("javatoken.txt","r");
yylex();
return 0;
}
#include<stdio.h>
%}
DATATYPE int|char|bool|float|double|String
SC [;]
DOTOP [.]
OPERATOR "+"|"-"|"/"|"*"|"="
UNARY "++"|"--"
IDENTIFIER [a-zA-Z]+[a-zA-Z0-9]*
COMMENT "//"[a-zA-Z0-9]*
PREPROCESSOR "import"[a-zA-Z0-9 "." "*"]*
KEYWORD public|static|void|main|abstract|class
BLOCK_S "{"
BLOCK_E "}"
BRACKET ")"|"("
STRING "\""[a-zA-Z0-9]*"\""
%%
{DOTOP} {printf("");}
{BRACKET} {printf("\n %s: bracket",yytext);}
{DATATYPE} {printf("\n %s: datatype",yytext);}
{SC} {printf("\n %s: semi colon \n",yytext);}
{OPERATOR} {printf("\n %s: operator \n",yytext);}
{UNARY} {printf("\n %s: unary \n",yytext);}
{BLOCK_S} {printf("\n %s: block start",yytext);}
{BLOCK_E} {printf("\n %s: block end",yytext);}
{COMMENT} {printf("\n %s: comment",yytext);}
{PREPROCESSOR} {printf("\n %s: preprocessor",yytext);}
{KEYWORD} {printf("\n %s: keyword",yytext);}
{STRING} {printf("\n %s: string",yytext);}
{IDENTIFIER} {printf("\n %s: identifier",yytext);}
%%
int yywrap(){
return (1);
}
main(){
yyin=fopen("javatoken.txt","r");
yylex();
return 0;
}
Commands to run a file with '.l' extension
1) lex filename.l
2) gcc lex.yy.c
3) ./a.out