Solved

Three address code

Posted on 2003-11-18
5
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 recursion 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.

756 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