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
Solved

Three address code

Posted on 2003-11-18
5
470 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:
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.

791 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