C++ struct in yyparse()?

Discussion in 'C++' started by pallav, Apr 2, 2007.

  1. pallav

    pallav Guest

    hello,

    i'm sorry if this is a bit off topic but perhaps some of you here have
    experience
    with c++ with lex/yacc and can help me.

    i wrote a small lexer/parser using lex/yacc. i'm using c++ as the
    language to interface with lex/yacc generated code. i have the
    following user defined
    struct for YYSTYPE to populate the data that is parsed.

    /*!
    * This union is used by the yyparse() routine to hold the
    information
    * that is accumulated during the parsing of a technology library
    file.
    * As tokens are collected, and rules from the grammar are
    * deduced, the data can be stored by creating one of the appropriate
    * structures and populating it with values.
    */
    typedef union MY_YYSTYPE
    {
    TechLib::Element *element; /*!< An element is a gate or a
    latch. */
    TechLib::Gate *gate; /*!< A logic gate. */
    TechLib::Expression *expr; /*!< The logic expression of an
    element. */
    TechLib::Function *function; /*!< Logic function of an
    element. */
    TechLib::pin *pin; /*!< Pin information about a
    pin. */
    list<TechLib::pin *> *pinlist; /*!< A list of pins. */
    TechLib::Latch *latch; /*!< A latch. */
    TechLib::Constraint *cst; /*!< Constraints on the latch. */
    list<TechLib::Constraint *> *cstlist; /*!< List of constraints.
    */
    TechLib::Clock *clk; /*!< The latch's clock. */
    string *str; /*!< An identifier. */
    double val; /*!< A float value. */
    TechLib::pin::phaseT phase; /*!< Phase of an input. */
    TechLib::Latch::TypeT atchtype; /*!< Type of latch. */

    };

    as you can see its a bunch of pointers. in my yacc grammar i have to
    do a bunch of new/delete statements to create the objects once rules
    are
    deduced. i would like to avoid this if possible. that is, i want to
    make
    YYSTYPE hold objects (and not pointers to objects).
    one person briefly mentioned that i should put all
    the above as objects (and not as pointers) inside a struct and then
    have an anonymous union inside this struct as well which contains
    pointers. i didn't understand him very clearly so i am not sure how
    to
    go about implementing the struct definition for YYSType. i'd
    like to avoid using new/delete as much as possible to make it easier
    on myself when debugging. i would appreciate if anyone can help me.
    thanks for your time.
    pallav
    pallav, Apr 2, 2007
    #1
    1. Advertising

  2. pallav

    Fei Liu Guest

    pallav wrote:
    > hello,
    >
    > i'm sorry if this is a bit off topic but perhaps some of you here have
    > experience
    > with c++ with lex/yacc and can help me.
    >
    > i wrote a small lexer/parser using lex/yacc. i'm using c++ as the
    > language to interface with lex/yacc generated code. i have the
    > following user defined
    > struct for YYSTYPE to populate the data that is parsed.
    >
    > /*!
    > * This union is used by the yyparse() routine to hold the
    > information
    > * that is accumulated during the parsing of a technology library
    > file.
    > * As tokens are collected, and rules from the grammar are
    > * deduced, the data can be stored by creating one of the appropriate
    > * structures and populating it with values.
    > */
    > typedef union MY_YYSTYPE
    > {
    > TechLib::Element *element; /*!< An element is a gate or a
    > latch. */
    > TechLib::Gate *gate; /*!< A logic gate. */
    > TechLib::Expression *expr; /*!< The logic expression of an
    > element. */
    > TechLib::Function *function; /*!< Logic function of an
    > element. */
    > TechLib::pin *pin; /*!< Pin information about a
    > pin. */
    > list<TechLib::pin *> *pinlist; /*!< A list of pins. */
    > TechLib::Latch *latch; /*!< A latch. */
    > TechLib::Constraint *cst; /*!< Constraints on the latch. */
    > list<TechLib::Constraint *> *cstlist; /*!< List of constraints.
    > */
    > TechLib::Clock *clk; /*!< The latch's clock. */
    > string *str; /*!< An identifier. */
    > double val; /*!< A float value. */
    > TechLib::pin::phaseT phase; /*!< Phase of an input. */
    > TechLib::Latch::TypeT atchtype; /*!< Type of latch. */
    >
    > };
    >
    > as you can see its a bunch of pointers. in my yacc grammar i have to
    > do a bunch of new/delete statements to create the objects once rules
    > are
    > deduced. i would like to avoid this if possible. that is, i want to
    > make
    > YYSTYPE hold objects (and not pointers to objects).
    > one person briefly mentioned that i should put all
    > the above as objects (and not as pointers) inside a struct and then
    > have an anonymous union inside this struct as well which contains
    > pointers. i didn't understand him very clearly so i am not sure how
    > to
    > go about implementing the struct definition for YYSType. i'd
    > like to avoid using new/delete as much as possible to make it easier
    > on myself when debugging. i would appreciate if anyone can help me.
    > thanks for your time.
    > pallav
    >

    What's preventing you from using structs containing objects instead of
    pointers to objects?

    Fei
    Fei Liu, Apr 2, 2007
    #2
    1. Advertising

  3. pallav

    Michael Guest

    On Apr 2, 10:53 am, Fei Liu <> wrote:
    > pallav wrote:
    > > typedef union MY_YYSTYPE
    > > {
    > > TechLib::Element *element; /*!< An element is a gate or a
    > > latch. */
    > > TechLib::Gate *gate; /*!< A logic gate. */

    < more of the same >
    > > TechLib::Latch::TypeT atchtype; /*!< Type of latch. */

    >
    > > };

    >
    > > as you can see its a bunch of pointers. in my yacc grammar i have to
    > > do a bunch of new/delete statements to create the objects once rules
    > > are
    > > deduced. i would like to avoid this if possible. that is, i want to
    > > make
    > > YYSTYPE hold objects (and not pointers to objects).


    > What's preventing you from using structs containing objects instead of
    > pointers to objects?


    Actually, the question is what is preventing you from using a union
    (not a struct) containing objects instead of pointers to objects. And
    the answer is the C++ rule that unions may not contain any type that
    has "a non-trivial constructor, a non-trivial copy constructor, a non-
    trivial destructor, or a non-trivial copy assignment operator."

    For the OP, the code I've seen typically does what you have, with
    pointers and news/deletes. I wish there were a magic bullet, but I'm
    not aware of it if there is.

    Michael
    Michael, Apr 2, 2007
    #3
  4. pallav

    pallav Guest

    > What's preventing you from using structs containing objects instead of
    > pointers to objects?
    >
    > Fei


    Hi Fei/Michael,

    Michael is right. That's the problem I had with unions. The compiler
    didn't want
    to see data types that had constructors. I suppose I can live with new/
    delete
    unless others know of a better way.

    Thanks for your time.
    pallav
    pallav, Apr 2, 2007
    #4
  5. pallav

    Fei Liu Guest

    pallav wrote:
    >> What's preventing you from using structs containing objects instead of
    >> pointers to objects?
    >>
    >> Fei

    >
    > Hi Fei/Michael,
    >
    > Michael is right. That's the problem I had with unions. The compiler
    > didn't want
    > to see data types that had constructors. I suppose I can live with new/
    > delete
    > unless others know of a better way.
    >
    > Thanks for your time.
    > pallav
    >
    >


    I misread your original post. I skipped after reading the part you said
    'user defined struct', it is actually a user defined union. From the
    replies, it sounds like the contained objects do not have default
    constructors. If it was struct, you could get around by defining user
    defined construct for the container struct. But this technique does not
    work with union.

    Fei
    Fei Liu, Apr 2, 2007
    #5
    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. RA Scheltema
    Replies:
    3
    Views:
    382
    RA Scheltema
    Jan 6, 2004
  2. Gunnar G

    struct in struct

    Gunnar G, May 31, 2004, in forum: C++
    Replies:
    14
    Views:
    789
  3. DanielEKFA
    Replies:
    8
    Views:
    592
    DanielEKFA
    May 16, 2005
  4. James Harris
    Replies:
    4
    Views:
    1,363
    James Harris
    Oct 9, 2003
  5. Chris Fogelklou
    Replies:
    36
    Views:
    1,357
    Chris Fogelklou
    Apr 20, 2004
Loading...

Share This Page