How to build a simple compiler/expression analyzer?

Sacha =?iso-8859-1?Q?Sch=E4r?= <>
16 Sep 2001 00:37:14 -0400

          From comp.compilers

Related articles
How to build a simple compiler/expression analyzer? (Sacha =?iso-8859-1?Q?Sch=E4r?=) (2001-09-16)
Re: How to build a simple compiler/expression analyzer? (Rainer Schwenkreis) (2001-09-20)
Re: How to build a simple compiler/expression analyzer? (2001-09-21)
| List of all articles for this month |

From: Sacha =?iso-8859-1?Q?Sch=E4r?= <>
Newsgroups: comp.compilers,comp.lang.c++
Date: 16 Sep 2001 00:37:14 -0400
Organization: Compilers Central
Keywords: interpreter, question, comment
Posted-Date: 16 Sep 2001 00:37:14 EDT


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)

For simplicity, the primitives will all occupy the same amount of
memory. The tokens inside angle brackets are symbols, the contents of
the square brackets correspond to array indices (where the variables
are stored), inside the round brackets are number constants.

I know there are many parser/compiler generators around, but it's hard
for me to choose one. Somehow I prefer to have an algorithm/source
code for the problem above rather than a program that generates
unreadable c or c++ source code which does the job. However, if there
is a compiler generator which is easy to operate and produces exactly
what i want, i'm also open for this...

Please don't tell me about the huge number of existing scripting
langauges, i have my reasons to implement a new one.

Any hints are greatly appreciated

[Translating expressions into byte codes is pretty easy in any parser
generator you care to use. Most generate C code, but only part of the
C you have to debug is the action code you wrote yourself. -John]

Post a followup to this message

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