Constant expression evaluation?

Paul Davis <mdhe51ATdialDOTpipexDOTcom@eu.uu.net>
14 May 2003 00:52:43 -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: Paul Davis <mdhe51ATdialDOTpipexDOTcom@eu.uu.net>
Newsgroups: comp.compilers
Date: 14 May 2003 00:52:43 -0400
Organization: Compilers Central
Keywords: optimize
Posted-Date: 14 May 2003 00:52:43 EDT

I'm hoping to add constant expressions to a simple compiler I've
written, but it looks like some of my early design decisions will make
this difficult. I'd appreciate some ideas on better ways to do this.


Currently, I've got various places, like declarations, where the
syntax requires a constant. It wasn't obvious that expressions would
be needed, but it now looks like I need them. For example:


property A = 6; // this is what I've got
property B = 2*3; // this is what I need


My expression evaluator (in Bison) currently creates a set of stacks
(the actual stack depends on context) which are later processed by an
RPN calculator. So, when I get '2*3', I push 2 on the stack, followed
by 3, followed by '*'. The expression doesn't get evaluated to '6'
till a later time, which is effectively 'runtime' (this is a
sort-of-interpreted language). My problem is therefore that the
front-end compiler doesn't know that this is a constant expression,
and so rejects it as an error because it's not a 'constant'. The Bison
action code for expressions doesn't calculate the value of an
expression: it simply pushes the elements of the expression onto the
required stack.


This is my first compiler, and I've got no idea how this would
normally be done. Some things I could try are:


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.


2) Add a dedicated stack for expression evaluation. This gets loaded
as normal during compilation, but the expression action code would
also attempt to evaluate the stack. If it's all constant, it returns
that constant; otherwise, it copies the stack elsewhere for run-time
processing.


Any better ideas? Thanks.


Paul



Post a followup to this message

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