Solved

Three address code

Posted on 2003-11-18
5
464 Views
Last Modified: 2012-06-27
i need to write a compiler in C that compile C code to three address code.

i got recognizer working, i just need help in how to convert to a three address code.
e.g
a= b+c+d;
convert to:
int t1;
t1=b+c;
a=t1+d;

thank you for any help.
Winzy
0
Comment
Question by:Winzy54
5 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 125 total points
Comment Utility

Hi Winzy54,

You're going to want to research how to parse expressions and build a binary tree from them.  Most compilers do this.  In your first example (a=b+c+d) the compiler will build a tree that resembles the following:

      d
    /
   +
  / \
 b   c

When the tree is traversed, b is added to c, and the sum added to d.  This gives the result that you want to store into a.

For stack machines (like Intel and AMD processors) reverse polish notation demonstrates very clearly how to do this.  b+c+d is converted to bc+d+.  Moving from left to right b becomes operator1 and c becomes operator2.  The plus sign says to add the two operators (and hold the result in operator1).  d is encountered next and becomes the new operator2 and when the final plus sign is encountered, the value in operator1 (which is b+c) is added to operator2 (d) and the calculation is complete.

On a stack machine it is something like this:

push b
push c
add
push d
add


An awful lot goes into a parser and compiler.

Good Luck,
Kent
0
 
LVL 22

Assisted Solution

by:grg99
grg99 earned 125 total points
Comment Utility
The easiest way to do this is to write a recursive-descent parser.  
These parsers are very easy to write, as the code reflects the structure being parsed.
You end up with code something like:

{  L = GetLeftSide(); op=  GetOperator(); R = GetRightSide();
   Emit( "Temp = L op R" ):
}

It's a bit too involved to explain in this little box-- you'd better find a book that explains recursive descent parsing.  
Or download a "Tiny Pascal" compiler source for some ideas,
 they all use reccursive descent to parse the language and the expressions.

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now