Saturday, 30 November 2013

LEX programming in Windows



download flex from

http://www.monmouth.com/~wstreett/lex-yacc/flex.exe

write a lex program and save it as prg1.lex
%{
  // lex program to identify a given string is NUMBER or WORD
%}

%%

[0-9]+    printf("%s is NUMBER\n",yytext);

[a-zA-Z]+ printf("%s is WORD\n",yytext);

.* printf("%s is NOT a WORD or NUMBER",yytext);
%%

main()
{
 yylex();
}

int yywrap()
{
return 1;
}

save the downloaded flex.exe and prg1.l in D:\LEX
Compile using flex:

After compiling lex file a C program file is generated in the current folder.

Again compile and run the C program using Turbo C or Dec Cpp to see the result.

No comments:

Post a Comment