help-bison
[Top][All Lists]
Advanced

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

Re: Multitype support in bison


From: Hans Aberg
Subject: Re: Multitype support in bison
Date: Sun, 14 Oct 2007 10:58:23 +0200

On 13 Oct 2007, at 01:10, Ilyes Gouta wrote:

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.

You might look into the "Grammar Summary" of C++/C. Standards can be gotten from ANSI at a low prices. You might also netsearch for "n1943.pdf" (free version of a C revision).

There is also info about a C++ Yacc grammar at:
http://www.parashift.com/c++-faq-lite/compiler- dependencies.html#faq-38.11

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?

Here is an example of the grammar, I coded for bison:

expr: CONSTINT                      { $<ival>$ = $<ival>1; }
    | CONSTFLOAT                   { $<fval>$ = $<fval>1; }
    | IDENTIFIER                   { $<ival>$ = getvalue($<id>1); }
    | expr ADD expr                { $<ival>$ = $<ival>1 + $<ival>3; }
    | expr SUB expr                { $<ival>$ = $<ival>1 - $<ival>3; }
;

If I opt for the second case, I would, for example, modify the
function getvalue to distinguish between the two types given the
identifier's name, stored in id.

So these standards have only one set of rules for all types. You might hev to write more complicated actions, to do type-checking there, though.

Any ideas guys?

And when you come to loops and jumps, you must build code closures in the actions, that can be executed afterwards.

  Hans Aberg






reply via email to

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