C++ objects and flex

G

Gus Gassmann

Not sure if this is a C++ question. If not, I apologize. Is there a
group where I could direct questions that deal with flex?

My situation is this: I have a (fairly complicated) C++ object (call it
Obj, perhaps) that I need to populate by parsing a string. I use
flex/bison to parse the string, and I currently have another C++ class,
ObjReader, containing a method readObj(std::string objStr) that calls
the parser and stores the information read into the various elements of
a private member Obj* ObjReader->my_obj.

My problem is that I constantly get into memory trouble (variously leaks
and segfaults), because I ultimately want to access the data from my own
instance of Obj and often trigger (viz., construct/destruct) the reader
in a subroutine. I feel --- given what little I know of C++ and OOP ---
that I would be better off if I could make the ObjReader a member of the
Obj class.

However, I am quite unsure of how to handle the flex construct yyextra
in this context. I have tried to find examples on the web for anything
similar to what I want to do, and I have so far turned up nothing.

Any ideas?

Thanks in advance

gus
 
8

88888 Dihedral

Gus Gassmannæ–¼ 2013å¹´2月9日星期六UTC+8上åˆ1時04分50秒寫é“:
Not sure if this is a C++ question. If not, I apologize. Is there a

group where I could direct questions that deal with flex?



My situation is this: I have a (fairly complicated) C++ object (call it

Obj, perhaps) that I need to populate by parsing a string. I use

flex/bison to parse the string, and I currently have another C++ class,

ObjReader, containing a method readObj(std::string objStr) that calls

the parser and stores the information read into the various elements of

a private member Obj* ObjReader->my_obj.



My problem is that I constantly get into memory trouble (variously leaks

and segfaults), because I ultimately want to access the data from my own

instance of Obj and often trigger (viz., construct/destruct) the reader

in a subroutine. I feel --- given what little I know of C++ and OOP ---

that I would be better off if I could make the ObjReader a member of the

Obj class.



However, I am quite unsure of how to handle the flex construct yyextra

in this context. I have tried to find examples on the web for anything

similar to what I want to do, and I have so far turned up nothing.



Any ideas?



Thanks in advance



gus
OK, I think you have to deal with the message format
first. Messages could be object, too.

An object can accept predefined messages to act for
some purposes.

Transforming input data into some valid format of
messages can be done in the traditional way
as those in the interpreter or compiler.
 
J

Jorgen Grahn

Not sure if this is a C++ question. If not, I apologize. Is there a
group where I could direct questions that deal with flex?

I don't think so. Most people use flex/bison rarely.
My situation is this: I have a (fairly complicated) C++ object (call it
Obj, perhaps) that I need to populate by parsing a string. I use
flex/bison to parse the string, and I currently have another C++ class,
ObjReader, containing a method readObj(std::string objStr) that calls
the parser and stores the information read into the various elements of
a private member Obj* ObjReader->my_obj.

My problem is that I constantly get into memory trouble (variously leaks
and segfaults), because I ultimately want to access the data from my own
instance of Obj and often trigger (viz., construct/destruct) the reader
in a subroutine.

I don't understand the reference to subroutines. I think you mean a
"function", but everything you do in C or in C++ is done in a
function!
I feel --- given what little I know of C++ and OOP ---
that I would be better off if I could make the ObjReader a member of the
Obj class.

However, I am quite unsure of how to handle the flex construct yyextra
in this context. I have tried to find examples on the web for anything
similar to what I want to do, and I have so far turned up nothing.

My only advice is:
- Don't use flex in its C++ mode (flex -+) -- it didn't seem well
supported last time I checked.
- Treat the flex part of the problem as a C/flex problem, then wrap it
in C++ when you're done.
- Get familiar with tools to handle memory leaks and crashes.

/Jorgen
 
G

gus gassmann

Let me try this again, before the original message disappears from the
news server...


I don't think so. Most people use flex/bison rarely.


I don't understand the reference to subroutines. I think you mean a
"function", but everything you do in C or in C++ is done in a
function!

OK. Fair enough. I guess my Fortran roots are showing. So let me try
again, with a little bit more of the code showing.

First, I have a complicated object class Obj, with code file Obj.cpp and
header file Obj.hpp. Let's say this consists of 5000 lines of nested
objects, and let's leave it at that. I hope I can explain the rest
without going into the details.

I then have an ObjReader class that looks like this:

ObjReader.hpp:

class ObjReader
{
public:
ObjReader();
~ObjReader();

Obj *readObj(const std::string &objstring);

private:
Obj* p_obj;
}


ObjReader.cpp:

#include "ObjReader.h"

//bison function
void yygetObj(const char *objstring, Obj* obj;

//lex functions
int objlex_init(void** ptr_yy_globals);
int objlex_destroy (void* scanner );
void objset_extra (void* yyscanner );

ObjReader::ObjReader( )
{
}

ObjReader::~ObjReader()
{
if(p_obj != NULL) delete p_obj;
p_obj = NULL;
}

Obj* ObjReader::readObj(const std::string& objstring)
{
if (objstring.length() > 0)
{
const char *ch = objstring.c_str();
yygetObj( ch, my_obj);
}
return p_obj;
}//end readObj


main.cpp:

void method1(Obj* my_obj);

void main()
{
Obj* my_obj = new Obj();
method1(obj);
}

void method1(Obj* my_obj)
{
ObjReader *objreader = new ObjReader();
my_obj = objreader->readObj("some XML string");
}

I do want to get the parsing out of main, just to make the code more
structured and easier for me to read and maintain. (I did not create the
design, I just inherited it.)

My trouble is: I newed the objreader in method1, but obj is only a
pointer to the private p_obj in the objreader.

Hence I have two choices:

I do not call the ObjReader destructor in method1, and I have created a
memory leak;

I do call the ObjReader destructor in method1, and I have created a
segfault as soon as I try to do anything with my_obj outside of method1.

I suppose I can get around it by thinking passing the ObjReader into
method1, but typically there are several layers of code involved, and
sometimes even different classes that are wrapped one inside the other.
It seems unnatural to me to pass the reader, and hence I forget, leading
to all sorts of frustrations. Is there no way to make the ObjReader a
member of the Obj class? I think (hope!) that this would fix my problem.
(And yes, I think I am stuck with flex/bison for this.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top