C++ objects and flex

Discussion in 'C++' started by Gus Gassmann, Feb 8, 2013.

  1. Gus Gassmann

    Gus Gassmann Guest

    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
    Gus Gassmann, Feb 8, 2013
    #1
    1. Advertising

  2. 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.
    88888 Dihedral, Feb 10, 2013
    #2
    1. Advertising

  3. Gus Gassmann

    Jorgen Grahn Guest

    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 .
    Jorgen Grahn, Feb 11, 2013
    #3
  4. Gus Gassmann

    gus gassmann Guest

    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
    >
    gus gassmann, Mar 3, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    477
    Sherm Pendley
    Sep 7, 2005
  2. Replies:
    4
    Views:
    1,096
  3. Replies:
    2
    Views:
    1,543
    Peter
    Mar 2, 2007
  4. Replies:
    0
    Views:
    330
  5. Peter Nicolai Motzfeldt

    Win32Ole, ExternalInterface (Flex), return objects

    Peter Nicolai Motzfeldt, Mar 21, 2007, in forum: Ruby
    Replies:
    0
    Views:
    84
    Peter Nicolai Motzfeldt
    Mar 21, 2007
Loading...

Share This Page