0
votes

I am trying to implement a prototype of assembly language parser using lex/flex and yacc/bison. I want to create a symbol table which is a C++ STL vector of a struct. However, I am a newbie about lex and just know that as lex generates C code, I guess the symbol table can't be used directly in lex file as it C doesn't have STL.

Is there a way that can help me to manipulate vector symbol table from lex and access it from yacc code?

Thanks..

2
I retagged the post. Flex-lexer is for the lexical analyzer... [it even says so in the "hover details" of the Flex tag nowJeffryHouser
If I recall correctly, the output from (most versions of) Flex can be compiled as C++.Jerry Coffin
As long as you compile with the g++ compiler. The code will be treated as a C++ source file (even if the extension on the file is .c). If you want to force the output file to have (.cpp) extension you can use the -o flag with flex.Martin York
Okay thanks a lot! I will try this..tigerden

2 Answers

1
votes

I faced similar issue while building a custom compiler. There are few approaches:

  • Create a separate c++ file, implement function in c++ and export it to C:

    extern "C" { void f() { // C++ code here } }

now you can invoke it from lex/yacc generated code.

  • Generate c++ code in flex/bison, then you can directly write C++ code in grammars.

  • You still can include C++ code in grammar, but that's not a good idea for many reasons.

0
votes

flex and bison have C++-friendly equivalents, flex++ and bison++. The manual pages for those might be helpful for you.

flex++ is simply flex which outputs C++ code. bison++ is a separate project, based on bison, which generates C++ code.