Re: Constant expression evaluation?

Clint Olsen <clint@0lsen.net>
18 May 2003 01:27:13 -0400

          From comp.compilers

Related articles
Constant expression evaluation? mdhe51ATdialDOTpipexDOTcom@eu.uu.net (Paul Davis) (2003-05-14)
Re: Constant expression evaluation? christian.bau@cbau.freeserve.co.uk (Christian Bau) (2003-05-16)
Re: Constant expression evaluation? chaos@vcc.de (Dierk Ohlerich) (2003-05-16)
Re: Constant expression evaluation? clint@0lsen.net (Clint Olsen) (2003-05-18)
Re: Constant expression evaluation? norlic@fly.srk.fer.hr (Niksa Orlic) (2003-05-29)
Re: Constant expression evaluation? camille@bluegrass.net (david lindauer) (2003-05-29)
| List of all articles for this month |

From: Clint Olsen <clint@0lsen.net>
Newsgroups: comp.compilers
Date: 18 May 2003 01:27:13 -0400
Organization: AT&T Broadband
References: 03-05-089
Keywords: optimize, practice
Posted-Date: 18 May 2003 01:27:13 EDT

Paul Davis wrote:
> 1) Add a second expression syntax section in the Bison description for
> constant expressions, which get evaluated during compilation. This sounds
> really kludgey, and I suspect would lead to lots of conflicts.


This is the correct way to do it except you use the same "expression"
rule as before but annotate your parse tree with a constant flag in it
- you make it a semantic check. Otherwise like you said you get
parser conflicts between "constant_expression" and "expression". You
just percolate the constant flag upwards as you continue to parse
expressions.


If both children are constants, so is the parent, and so on. This
kind of inheritance from the children works for many attributes in
fact. And if you then want to evaluate this expression, you can do it
at the rule that expects a constant expression:


constant_expression : expression
{
        if ($1->constant) {
                evaluate($1);
        }
        else
                error("constant expression expected\n");
}


Something that smells like that should work anyway.


-Clint


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.