How to use C ++ in flex and bison?

I have a project for a school where we need to use flex and bison. I want to use C ++ to have access to the STL and my own classes that I wrote. We were provided with the following Makefile:

CC = gcc CFLAGS = -g OBJs = parse.tab.o symtab.o attr.o lex.yy.o default: parser parser: ${OBJs} ${CC} ${CFLAGS} ${OBJs} -o parser -lfl lex.yy.c: scan.l parse.tab.h attr.h flex -i scan.l parse.tab.c: parse.y attr.h symtab.h bison -dv parse.y parse.tab.h: parse.tab.c clean: rm -f parser lex.yy.c *.o parse.tab.[ch] parse.output depend: makedepend -I. *.c 

scan.l and parse.y have some initial flex / bison properties for generating a scanner and analyzer. I need to add my own materials to these files. symtab {h, c} must be a character table implementation. attr. {h, c} - for attribute magic. I want to make a symtab.c.cc file to use STL. I also have other reasons for using C ++.

I tried using the parse.ypp file to create a .cpp file. But the problem is that I am not getting the correct .h file. I modified the Makefile to look like this:

 CC = g++ # Change gcc to g++ CFLAGS = -g OBJs = lex.yy.o parse.tab.o symtab.o attr.o default: lex.yy.c parser # added lex.yy.c so I could just keep lex stuff in C since I don't really need C++ there parser: ${OBJs} ${CC} ${CFLAGS} ${OBJs} -o parser -lfl lex.yy.o: scan.l parse.tab.h attr.h # added this rule to use gcc instead of g++ gcc -c -o lex.yy.o lex.yy.c lex.yy.c: scan.l parse.tab.h attr.h flex -i scan.l parse.tab.cpp: parse.ypp attr.h symtab.h bison -dv parse.ypp parse.tab.h: parse.tab.cpp # I want a parse.tab.h but I get parse.tab.hpp clean: rm -f parser lex.yy.c *.o parse.tab.cpp parse.tab.h parse.output depend: makedepend -I. *.c 

Can someone tell me what I need to add or do to make C ++ work? It should be noted that I added some things to the .y (or .ypp) file to deal with the transition from C to C ++. In particular, I had to declare something like extern. My main problem is that when I run make, scan.l has a bunch of syntax errors, and they seem to be related to the fact that it cannot include parse.tab.h (because it is never generated).

+4
source share
5 answers

You don't have to do anything with flex or bison to use C ++, I have done this many times. You just need to make sure that you are using g ++, not gcc.

Your problems are related to the Makefile, not the code.

+3
source
 For using flex with C++: 1: read the flex docs: 2: use flex -+ -o file.cc parser.ll 3: In the .ll file: %option c++ %option yyclass="Your_class_name" %option batch 4: In your .hh file, derive Your_class_name from public yyFlexLexer 5: you can then use your_class_instance.yylex() 
+4
source

There are some differences that you can check in detail here .

+1
source

Use either the C compiler or the C ++ compiler, but not both (until you know what you have). You will surely shoot yourself many times on both legs otherwise. Mixing gcc and g ++ is not very good.

This line is suspicious:

 lex.yy.o: scan.l parse.tab.h attr.h # added this ... gcc -c -o lex.yy.o lex.yy.c 

Also, you don't seem to use CC anywhere, using this to make life easier.

Assuming that you are not changing a single line of C code, you may be causing some errors and quite a few warnings (e.g. obsolete headers, etc.). You will also have to fix them.

0
source

If you are doing parsers in C ++, I would recommend looking at Boost Spirit . This is much nicer than bison / yacc.

From here :

Spirit is an object-oriented structure of a recursive descent parser generator implemented using template metaprogram methods. Expression patterns allow you to fully approximate the syntax of Extended Backus-Normal Form (EBNF) in C ++.

-1
source

All Articles