Re: How is defined precedence with "(" and ")"

From: Warwick Harvey <wh_at_icparc.ic.ac.uk>
Date: Wed 14 Apr 2004 12:27:44 PM GMT
Message-ID: <20040414132743.R2682@tempest.icparc.ic.ac.uk>
Hi,

On Wed, Apr 14, 2004 at 12:24:11AM -0300, cbraz@alanet.com.br wrote:
> 
>         I'am trying to create a expression like (2 + 10) * 4.
> 
>         Of course i can do  EC_word x = (2 + 10) * 4 and it's work fine. 
> 
>         But i need to create a general expression at run time. If i do
> 
>         term(EC_functor("+",2), 2 , term(EC_functor("*",2),10,4 ) )  it's evaluate to 

Here you are creating a term with functor "+" and two arguments, the first
of which is 2 and the second of which is another term, this time with
functor "*", with arguments 10 and 4.  The structure can be represented like
this (assuming a fixed-width font):

  +
 / \
2   *
   / \
 10   4

This completely defines how the term should be evaluated; precedence doesn't
come into it.  First the 10 and the 4 are multiplied, and then the result is
added to two.  What you actually want looks like this:

    *
   / \
  +   4
 / \
2   10

Here the 2 and 10 are added, and the result multiplied by 4.  It should be
obvious how to construct this term: the top-level term has functor "*", with
its first argument being another term with functor "+".  I.e.

    term(EC_functor("*",2), term(EC_functor("+",2), 2, 10), 4)

Cheers,
Warwick
Received on Wed Apr 14 13:30:01 2004

This archive was generated by hypermail 2.1.8 : Wed 16 Nov 2005 06:07:28 PM GMT GMT