C++ struct in yyparse()?

P

pallav

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
 
F

Fei Liu

pallav said:
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
 
M

Michael

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
 
P

pallav

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
 
F

Fei Liu

pallav said:
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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top