How do I use C++ in flex and bison?

后端 未结 5 1555
清歌不尽
清歌不尽 2020-12-16 06:03

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 fo

相关标签:
5条回答
  • 2020-12-16 06:46

    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.

    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.

    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.

    0 讨论(0)
  • 2020-12-16 06:54

    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.

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

    0 讨论(0)
  • 2020-12-16 07:06

    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.

    Your problems are with the Makefile, not the code.

    0 讨论(0)
  • 2020-12-16 07:07

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

    0 讨论(0)
  • 2020-12-16 07:08
    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()
    
    0 讨论(0)
提交回复
热议问题