我如何使用C ++在flex和bison? [英] How do I use C++ in flex and bison?

查看:287
本文介绍了我如何使用C ++在flex和bison?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个学校,我们需要使用Flex和野牛的项目。我想用C ++,使我有机会获得STL和我自己的类,我写的。我们提供了下面的Makefile:

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:

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里和parse.y有一些初步的Flex /野牛的东西产生的扫描器和分析器。我需要我自己的东西添加到这些文件。 SYMTAB {H,C}被认为是一个符号表的一个实现。 ATTR {H,C}是一些属性的魔法。我想打一个symtab.c .cc文件,所以我可以使用STL。我也有其他原因想使用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++.

我试图用一个parse.ypp文件,这样一个.cpp文件,就可以产生。但问题是,我没有得到正确的.h文件中。我改变了Makefile文件看起来是这样的:

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:

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

谁能告诉我什么,我需要添加或做的就是C ++的工作?应当指出的我加了一些东西在.Y(或.ypp)文件来处理移动从C到C ++。特别是,我不得不宣布一些东西为外部。我的主要问题是,当我运行make,scan.l里有一堆语法错误,而且他们似乎是因为它不能包括parse.tab.h(因为不生成它)。

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

推荐答案

您不需要使用Flex或野牛用C ++做任何事情,我已经做了很多次。你只需要确保你使用G ++,不GCC。

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.

您的问题与Makefile文件,而不是code。

Your problems are with the Makefile, not the code.

这篇关于我如何使用C ++在flex和bison?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆