help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multitype support in bison


From: lfinsto1
Subject: Re: Multitype support in bison
Date: Sat, 13 Oct 2007 11:06:44 +0200 (CEST)
User-agent: SquirrelMail/1.4.9a

> I'm new to Bison. I'm writing an interpreter for a lightweight C-like
> language using flex and bison. My language has to support the *two*
> types *int* and *float* used by the identifiers.
>
> My question is, do I have to write separate rules for both types in my
> .y file, where each set of rules is dedicated for a given type, or do
> I have to handle the differentiation between those types in the
> grammatical actions associated with a unified set of rules for these
> two types?

It's up to you.  I don't think it's a good idea to use different types for
the semantic value of a single symbol; `expr', in your example.  Does this
work?  It wouldn't have occurred to me to try it.

> Any ideas guys?

Bison is extremely flexible, so it's really up to you.  My approach in GNU
3DLDF is to use a set of non-terminals `numeric_primary',
`numeric_secondary', `numeric_tertiary' and `numeric_expression'.  This is
based on the grammar of Donald Knuth's METAFONT language (except he wrote
his parser from scratch).  The semantic value for these symbols is `float'
or `double' (depending on the value of a preprocessor macro).  The 3DLDF
grammar has a token `INTEGER' whose semantic value is `int', but it is
replaced by a `float' or `double' in the "chain" of actions that "turn it
into" a `numeric_<something>'.

I hope I'm not overstepping the bounds here, but an idea that I would like
to suggest is that you not write a C-like language.  There must be
thousands of them by now.  It's a typical homework assignment for people
studying computer science.  There are plenty of languages waiting to be
written with Bison that would really be useful.  It would be a shame to
write yet another "Appendix A" language.

Laurence Finston







reply via email to

[Prev in Thread] Current Thread [Next in Thread]