我如何在flex和bison中使用c++ ?

时间:2021-11-06 09:40:09

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

我有一个学校的项目,我们需要使用flex和bison。我想用c++来访问STL和我自己编写的类。我们提供了以下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 stuff to generate the scanner and parser. I need to add my own stuff to those files. symtab.{h, c} is supposed to be a implementation of a symbol table. attr.{h, c} are for some attribute magic. I want to make symtab.c a .cc file so I can use STL. I also have other reasons for wanting to use C++.

扫描。l和解析。y有一些初始的flex/bison素材来生成扫描仪和解析器。我需要把自己的东西添加到这些文件中。symtab。{h, c}应该是一个符号表的实现。attr。{h, c}为某些属性魔法。我要制作symtab。c .cc文件,这样我就可以使用STL了。我还有其他理由想要使用c++。

I tried to use a parse.ypp file, so that a .cpp file would be generated. But the problem is that I'm not getting the right .h file. I changed the Makefile to look like this:

我尝试使用解析。ypp文件,这样就会生成一个.cpp文件。但问题是我没有得到正确的。h文件。我将Makefile更改为如下所示:

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 get C++ working? It should be noted I added some stuff in the .y (or .ypp) file to deal with moving from C to C++. In particular, I had to declare some stuff as extern. My main problem is that when I run make, scan.l has a bunch of syntax errors, and they seem to be because it cannot include parse.tab.h (because it is never generated).

有人能告诉我,我需要添加什么或做什么来得到c++的工作吗?应该注意的是,我在.y(或.ypp)文件中添加了一些内容,以处理从C到c++的迁移。特别是,我必须声明一些东西作为外部。我的主要问题是,当我运行时,扫描。我有很多语法错误,它们似乎是因为它不能包含parse.tab。h(因为它从不生成)。

5 个解决方案

#1


3  

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

您不需要使用flex或bison来使用c++,我已经做过很多次了。你只需要确保使用g++,而不是gcc。

Your problems are with the Makefile, not the code.

您的问题在于Makefile,而不是代码。

#2


4  

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()

#3


1  

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

这里有一些不同之处,您可以在这里详细了解。

#4


0  

Use either a C Compiler or a C++ compiler but not both (till you know what you are upto). You are sure to shoot yourself many times on both your feet otherwise. Mixing gcc and g++ isn't good.

使用C编译器或c++编译器,但不能同时使用它们(直到您知道您要做什么)。否则,你肯定会在你的双脚上多次射击自己。混合gcc和g++并不好。

This line is suspect:

这条线是怀疑:

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 be using CC anywhere, using that'd have made life easier.

而且,您似乎并没有在任何地方使用CC,使用它会使您的生活更轻松。

Assuming you don't change a single line of the C code, you will possibly hit some errors and quite a few warnings (like deprecated headers etc). You'll have to fix them as well.

假设您不更改C代码的单个行,您可能会遇到一些错误和相当多的警告(如弃用头等)。你也必须修复它们。

#5


-1  

If you are doing parsers in C++ I would recommend to look at Boost Spirit. It is so much nicer to handle than bison/yacc.

如果您在c++中做解析器,我建议您考虑Boost Spirit。它比bison/yacc好得多。

From here:

从这里开始:

Spirit is an object-oriented recursive-descent parser generator framework implemented using template meta-programming techniques. Expression templates allow us to approximate the syntax of Extended Backus-Normal Form (EBNF) completely in C++.

Spirit是使用模板元编程技术实现的面向对象的递归下降解析器生成器框架。在c++中,表达式模板使我们能够完全地近似于扩展的Backus-Normal Form (EBNF)的语法。

#1


3  

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

您不需要使用flex或bison来使用c++,我已经做过很多次了。你只需要确保使用g++,而不是gcc。

Your problems are with the Makefile, not the code.

您的问题在于Makefile,而不是代码。

#2


4  

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()

#3


1  

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

这里有一些不同之处,您可以在这里详细了解。

#4


0  

Use either a C Compiler or a C++ compiler but not both (till you know what you are upto). You are sure to shoot yourself many times on both your feet otherwise. Mixing gcc and g++ isn't good.

使用C编译器或c++编译器,但不能同时使用它们(直到您知道您要做什么)。否则,你肯定会在你的双脚上多次射击自己。混合gcc和g++并不好。

This line is suspect:

这条线是怀疑:

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 be using CC anywhere, using that'd have made life easier.

而且,您似乎并没有在任何地方使用CC,使用它会使您的生活更轻松。

Assuming you don't change a single line of the C code, you will possibly hit some errors and quite a few warnings (like deprecated headers etc). You'll have to fix them as well.

假设您不更改C代码的单个行,您可能会遇到一些错误和相当多的警告(如弃用头等)。你也必须修复它们。

#5


-1  

If you are doing parsers in C++ I would recommend to look at Boost Spirit. It is so much nicer to handle than bison/yacc.

如果您在c++中做解析器,我建议您考虑Boost Spirit。它比bison/yacc好得多。

From here:

从这里开始:

Spirit is an object-oriented recursive-descent parser generator framework implemented using template meta-programming techniques. Expression templates allow us to approximate the syntax of Extended Backus-Normal Form (EBNF) completely in C++.

Spirit是使用模板元编程技术实现的面向对象的递归下降解析器生成器框架。在c++中,表达式模板使我们能够完全地近似于扩展的Backus-Normal Form (EBNF)的语法。