Are you struggling while switching from Windows to Linux just for the compilation using lex and yacc?
Use flex and bison instead.
Steps to install Flex
- Download CodeBlocks-minGW setup from here and install it.
- Download Flex setup program from here and install it.
- After successfully installation go to CodeBlocks -> minGW -> bin and copy the address of rhe bin. Then go to environment variables->system variables->path->edit->new and paste the bin address here.
- Then go to C ->Program Files -> GnuWin32-> bin and again copy the address of the bin. Then go to environment variables->system variables->path->edit->new and paste it here.
- Make sure you keep the CodeBlocks path over the flex path as shown below:
Steps to install Bison
- Download DEV-CPP from here and install it.
- Download Bison setup program from here and change its path as shown below and install it.
3. After installation go to Dev-cpp -> MinGW64 -> bin and copy the address of the bin. Then add it to environment variables->system variables->path->edit->new.
4. Go to C -> GnuWin32 ->bin and copy the address the bin. Then add it to environment variables->user variables->path->edit->new.
5. Make sure that your paths follow the following order:
I hope you guys have installed flex and bison successfully. To make sure run the following commands in command prompt.
Your files should contain following elements:
Lex file
int yywrap(){return 1;}
#include "file_name.tab.h"
in definition section.
Yacc file
#include "lex.yy.c"
in definition section.void yyerror(char *s);
in definiton section and define it outside any section. For instance:
void
yyerror(char* s)
/* yacc error handler */
{fprintf
(stderr, "%s\n", s);}
You can change the definition as per your need.
Commands to compile Lex program
Write your lex program in a text editor, save it with .l extension and run the following commands in command prompt:
- Enter into the folder where you have saved your lex file using cd command.
- flex file_name.l
- gcc lex.yy.c
- a.exe
For instance:
Commands to compile Yacc program
Write your lex and yacc program in a text editor, save it with .l and .y extensions respectively in same folder. Keep the filename for both the programs same. Then run the following commands in command prompt:
- Enter into the folder where you have saved your lex and yacc files using cd command.
- bison -d file_name.y
- flex file_name.l
- gcc file_name.tab.c -o file_name
- file_name
For instance:
Thank you guys! If you like it, dont forget to like and share.