CODING :-
%{
/* need this for the call to atof() below */
#include <math.h>
%}
DELIM [ \t\n]
WHITESPACE {DELIM}+
DIGIT [0-9]
LETTER [a-zA-Z]
ID {LETTER}({LETTER}|{DIGIT})*
NUMBER {DIGIT}+(\.{DIGIT}+)?(e[+\-]?{DIGIT}+)?
%%
{DIGIT}+ {
printf( "An integer: %s (%d)\n", yytext,
atoi( yytext ) );
}
{DIGIT}+"."{DIGIT}* {
printf( "A float: %s (%g)\n", yytext,
atof( yytext ) );
}
if|else|return|main|include|int|float|char {
printf( "A keyword: %s\n", yytext );
}
{ID} printf( "An identifier: %s\n", yytext );
"+"|"-"|";"|"="|"("|")"|"{"|"}"|"<"|">"|"*"|"/" printf( "An operator: %s\n", yytext );
"{"[^}\n]*"}" /* eat up one-line comments */
[ \t\n]+ /* eat up whitespace */
. printf( "Unrecognized character: %s\n", yytext );
%%
main( argc, argv )
int argc;
char **argv;
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
yylex();
}
File.c
main()
{
int a1, a2;
float f1=9.09;
a1=10;
if(a1>10)
a2=a1;
}
OUTPUT SCREEN:-
[S@localhost ~]$ flex lex.l
[S@localhost ~]$ gcc -c lex.yy.c
[S@localhost ~]$ gcc -o final lex.yy.o -lfl
[S@localhost ~]$ ./final file.c
A keyword: main
An operator: (
An operator: )
An operator: {
A keyword: int
An identifier: a1
Unrecognized character: ,
An identifier: a2
An operator: ;
A keyword: float
An identifier: f1
An operator: =
A float: 9.09 (9.09)
An operator: ;
An identifier: a1
An operator: =
An integer: 10 (10)
An operator: ;
A keyword: if
An operator: (
An identifier: a1
An operator: >
An integer: 10 (10)
An operator: )
An identifier: a2
An operator: =
An identifier: a1
An operator: ;
An operator: }
[S@localhost ~]$
Any Suggession for printf(Welcome); statement
ReplyDelete