Re: Not able to use %union?

Christian Schoenebeck <[email protected]>
Newsgroups gmane.comp.parsers.bison.general
Message-ID <2110832.TKVLDN12GD@silver>
On Montag, 18. Februar 2019 12:22:27 CET Christian Schoenebeck wrote:
> On Sonntag, 17. Februar 2019 21:37:26 CET Peng Yu wrote:
> > Because I want to use the previously allocated memory, I don't want to
> > call "rs_init(&yylval->str)" in any action. So YYSTYPE must be a
> > struct instead of a union. Is it a good practice to use struct instead
> > of union?
> 
> Yes, it is. The only advantage by using a union as a type is that you save
> some tiny amount of memory, but a union is also less safe and harder to
> debug. So personally I would barely ever use union as fundamental type with
> Bison.

Btw, you can also mix both approaches, e.g.:

struct _YYSTYPE {
	union {
		int inum;
		float fnum;
	};
	rapidstring str;
	Foo_t foo;
};

typedef it appropriately, I usually use C++ code, but you get the idea.

Best regards,
Christian Schoenebeck

_______________________________________________
[email protected] https://lists.gnu.org/mailman/listinfo/help-bison
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.