Re: Expression simplifier.

Rasmus Anthin <e8rasmus@etek.chalmers.se>
3 May 2001 13:48:31 -0400

          From comp.compilers

Related articles
Expression simplifier. e8rasmus@etek.chalmers.se (Rasmus Anthin) (2001-04-29)
Re: Expression simplifier. e8rasmus@etek.chalmers.se (Rasmus Anthin) (2001-04-30)
Re: Expression simplifier. Brian.Inglis@SystematicSw.ab.ca (2001-04-30)
Re: Expression simplifier. idbaxter@semdesigns.com (Ira D. Baxter) (2001-04-30)
Re: Expression simplifier. e8rasmus@etek.chalmers.se (Rasmus Anthin) (2001-05-03)
| List of all articles for this month |
From: Rasmus Anthin <e8rasmus@etek.chalmers.se>
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>') &...
                                    strcmp(node.branch{ii+1}.branch{1}.branch{1}.branch{1}.data,'<literal>')
                              sgn='';
                              if ii==3 & strcmp(node.branch{1}.data,'<add_op>')
                                    sgn=node.branch{ii-2}.branch{1}.data;
                                    node.branch{ii-2}.data='';
                                    node.branch{ii-2}.branch=[];
                              end
                              foo=eval([sgn node.branch{ii-1}.branch{1}.branch{1}.branch{1}.branch{1}.data...
                                          node.branch{ii}.branch{1}.data...
                              node.branch{ii+1}.branch{1}.branch{1}.branch{1}.branch{1}.data]);
                              node.branch{ii+1}.branch{1}.branch{1}.branch{1}.branch{1}.data=cplxread(foo*sign(foo));
                              node.branch{ii-1}.data='';
                              node.branch{ii-1}.branch=[];
                              node.branch{ii}.data='';
                              node.branch{ii}.branch=[];
                              if foo<0
                                    node.branch{ii}.data='<add_op>';
                                    node.branch{ii}.branch{1}.data='-';
                                    node.branch{ii}.branch{1}.branch=[];
                              end
                        end
                  end
            end
      end
      if ~isempty(node.branch)
            node.branch{ii}=simple(node.branch{ii});
      end
end


%-----------------------------------------


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
    / e8rasmus@etek.chalmers.se
  / www.etek.chalmers.se/~e8rasmus


Post a followup to this message

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