Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Posted on 2003-11-18
Medium Priority
492 Views
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
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

LVL 46

Accepted Solution

Kent Olsen earned 500 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
push d

An awful lot goes into a parser and compiler.

Good Luck,
Kent
0

LVL 22

Assisted Solution

grg99 earned 500 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.
they all use reccursive descent to parse the language and the expressions.

0

## Featured Post

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.
###### Suggested Courses
Course of the Month5 days, 4 hours left to enroll