Solved

Three address code

Posted on 2003-11-18
5
466 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
ID: 9772062

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
ID: 9775230
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

943 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

4 Experts available now in Live!

Get 1:1 Help Now