Re: How to build a simple compiler/expression analyzer?

"Rainer Schwenkreis" <rainer.schwenkreis@adlon.de>
20 Sep 2001 00:26:07 -0400

          From comp.compilers

Related articles
How to build a simple compiler/expression analyzer? sacha.schaer@unibas.ch (Sacha =?iso-8859-1?Q?Sch=E4r?=) (2001-09-16)
Re: How to build a simple compiler/expression analyzer? rainer.schwenkreis@adlon.de (Rainer Schwenkreis) (2001-09-20)
Re: How to build a simple compiler/expression analyzer? dmitry@elros.cbb-automation.de (2001-09-21)
| List of all articles for this month |

From: "Rainer Schwenkreis" <rainer.schwenkreis@adlon.de>
Newsgroups: comp.compilers,comp.lang.c++
Date: 20 Sep 2001 00:26:07 -0400
Organization: Compilers Central
References: 01-09-068
Keywords: parse, code
Posted-Date: 20 Sep 2001 00:26:06 EDT

Hello Sacha,


"Sacha Schär" <sacha.schaer@unibas.ch> schrieb im Newsbeitrag
> We need a simple compiler that converts a self defined script language
> into byte-code. What bothers me most are general algebraic
> expressions. I know quite well, how the script language and the
> corresponding byte-code should look like. Here is an example:
>
> set x = 1.2 * ( b + 2 ); (or similar)
>
> should convert into the following byte-code primitives:
>
> command: arguments: (comments:)
>
> <set_int> [t1] (2) (t1 = 2)
> <add_int> [t2] [b] [t1] (t2 = b * t1)
> <int_to_float> [t1] [t2] (t1 = (float)t2)
> <set_float> [t2] (1.2) (t2 = 1.2)
> <mult_float> [x] [t1] [t2] (x = t1 * t2)
>


Maybe you will get more trouble than needed with your Bytecode. I've
done some expression Compilers that produced Byte Code but with a
different approach. Since Parsing results in a tree like structure i
stored the tree in a byte array.


Example:
set x = 1.2 * ( b + 2 );


will become
set x * 1.2 + b 2


That stored in array with nodes that state the expression an
containing pointers (offsets) to the arguments:


Index Content Arg1 Arg2 Arg3
1 NODE_SET 2 3
2 NODE_VAR <Index of Variable>
3 NODE_MULT 4 5
4 NODE_FLOAT <Floatingpoint value 1.2>
5 NODE_ADD 6 7
6 NODE_VAR <Index of Variable>
7 NODE_INT <INTeger value 2>


Note that the nodes have different lengths depending on their type and
number of operands. The Index are Simplified in thos example but
would be Byte or Word Offsets in a real application


If youre interested in a more detailed expalnation, feel free to send me a
mail




Rainer


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.