Author Topic: Working with flex and bison  (Read 3182 times)

Offline vahalia

  • Single posting newcomer
  • *
  • Posts: 7
Working with flex and bison
« on: March 06, 2015, 03:37:42 am »
I have a simple C program with about a dozen files that builds effortlessly on Linux using command line tools. I have moved it to CodeBlocks on Windows 8 since I want to run it on Windows. I have had nothing but trouble getting it to build. The main complication is that I use flex and bison. I have two files, calc.l and calc.y that I need to build as part of the program. The first problem is that CB runs flex on calc.l, generating the file lex.yy.c, then tries to compile it. This file should not compile standalone but should be included in calc.y (which it is). How do I get CB not to try to compile lex.yy.c? Since it is autogenerated, it does not allow me to disable the compile option. Also, bison generates a C++ file by default -- calc.parser.cc.  I go into the compiler settings and rename the output file to calc.parser.c, since I want a C file. However, the auto-generated file list still shows the .cc extension and does not allow me to change it. I have a few other problems as well but for starters can someone please help with the above?

Offline bill00000111

  • Single posting newcomer
  • *
  • Posts: 4
Re: Working with flex and bison
« Reply #1 on: May 03, 2015, 12:03:36 pm »
This relates to bison and flex input files that contain C code.

Going to  "Settings->Compiler->Other settings. Then look for "Advanced options" in lower right." allows you to specify a command line macro to execute on a file with a particular extension included in your project and also specify if there are any files generated that need to be compiled. There is info about that here http://wiki.codeblocks.org/index.php?title=Adding_support_for_non_C/C%2B%2B_files_to_the_build_system

The already existing command line macros for bison and flex input files (extensions .y and .l), generate files with extension .cc - which resulted in scope errors when trying to compile the output of bison with gcc.   Note that the bison manual suggests that bison files should have extensions .y++ or .ycc when generating C++ code.

To get around this I added two more macros to generate C files, however this requires that the bison and flex input files have extensions .yc and .lc (which is a bit too inventive):

For bison files with extension .yc
Command line macro:
bison -t -v -d $file -o $file_dir/$file_name.tab.c

Generated files:
$file_dir/$file_name.tab.c
$file_dir/$file_name.tab.h

For flex files with extension .lc
Command line macro:
flex -o$file_dir/$file_name.lex.c $file

Generated files:
$file_dir/$file_name.lex.c

----------------------------

It seems that you can't just then build, you need to bootstrap everything by compiling any .yc and then .lc files individually (Build->CompileCurrentFile). Once the implementation and header files have been created the Build system seems to work fine.