Re: Expression simplifier.

Rasmus Anthin <>
3 May 2001 13:48:31 -0400

          From comp.compilers

Related articles
Expression simplifier. (Rasmus Anthin) (2001-04-29)
Re: Expression simplifier. (Rasmus Anthin) (2001-04-30)
Re: Expression simplifier. (2001-04-30)
Re: Expression simplifier. (Ira D. Baxter) (2001-04-30)
Re: Expression simplifier. (Rasmus Anthin) (2001-05-03)
| List of all articles for this month |

From: Rasmus Anthin <>
Newsgroups: comp.compilers
Date: 3 May 2001 13:48:31 -0400
Organization: Chalmers University of Technology
References: 01-04-145 01-04-146
Keywords: optimize
Posted-Date: 03 May 2001 13:48:31 EDT

Hi everyone!

I think I have succeeded with implementing a sorting algorithm for the
variables to be added (the terms) and the algorithm for adding the
terms if they are literals. I must say it wasn't that easy. I thought
it should be a lot easier than that because we're using a recursively
generated tree, but it seems like I was wrong. The sorting algorithm
became 45 rows long and the adding algorithm became 26 rows long. I'm
showing the adder here so that someone can react if I have done
anything unnessecary. The code is in matlab and should be somewhat
understandable for a non-matlab user:

function node=simple(node)
for ii=1:length(node.branch)
      if length(node.branch)>0
            if strcmp(node.branch{ii}.data,'<add_op>') & ii>1
                  if length(node.branch{ii-1}.branch)>0 & length(node.branch{ii+1}.branch)>0
                        if strcmp(node.branch{ii-1}.branch{1}.branch{1}.branch{1}.data,'<literal>') &...
                              if ii==3 & strcmp(node.branch{1}.data,'<add_op>')
                              foo=eval([sgn node.branch{ii-1}.branch{1}.branch{1}.branch{1}.branch{1}.data...
                              if foo<0
      if ~isempty(node.branch)


I haven't included the sorting algorithm due to a matter of space here. So
assume that the operands are sorted like this: "43+y-3+x" -> "-3+43+x+y".

Is it really this hard to make a simplifier or is the code above overkill?

Thanx in advace!
Thanx everyone who have helped me!

\ B. Rasmus Anthin

Post a followup to this message

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