Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ objects and flex (http://www.velocityreviews.com/forums/t957423-c-objects-and-flex.html)

Gus Gassmann 02-08-2013 05:04 PM

C++ objects and flex
 
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





88888 Dihedral 02-10-2013 12:05 PM

Re: C++ objects and flex
 
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.



Jorgen Grahn 02-11-2013 08:46 PM

Re: C++ objects and flex
 
On Fri, 2013-02-08, Gus Gassmann wrote:
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

gus gassmann 03-03-2013 03:27 AM

Re: C++ objects and flex
 
Let me try this again, before the original message disappears from the
news server...


On 11/02/2013 4:46 PM, Jorgen Grahn wrote:
> On Fri, 2013-02-08, Gus Gassmann wrote:
>> 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!


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



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




All times are GMT. The time now is 05:46 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.