Write A Program To Print Hello World Using Lex

 


How to write lex program ?


first write a lex program in notepad then save with .l extension. next step is open cmd(Command prompt) then open directory in which your program is saved. then 3 step to run lex program let's see

  1. flex filename.l
  2. gcc lex.yy.c
  3. a.exe

Code


%{
#include<stdlib.h>
%}
%%
("hi"|"HI")"\n" {printf("\nHello Sir,Good Morning\n");}
("bye"|"BYE")"\n" {printf("\nBye, Take Care\n");}
. {yyerror();}
%%
int main()
{
yylex();
return 0;
}
int yywrap(void)
{
return 0;
}

int yyerror(){
printf("Enter hi,HI,bye or BYE");
}


Output




in output when successful compile then just type hi,HI,bye and BYE and lex program is give you replay which is set by you.

Comments