Re: Source to source compiling (Thomas O'Connor)
Sun, 18 Sep 1994 04:34:16 GMT

          From comp.compilers

Related articles
Source to source compiling (Casper Gripenberg) (1994-09-11)
Re: Source to source compiling eanders+@CMU.EDU (Eric A. Anderson) (1994-09-12)
Re: Source to source compiling (1994-09-14)
Re: Source to source compiling (1994-09-13)
Re: Source to source compiling (1994-09-13)
Re: source to source compiling (1994-09-18)
Re: Source to source compiling (Robert Bernecky) (1994-09-18)
Re: Source to source compiling (1994-09-18)
Re: Source to source compiling (1994-09-16)
Re: Source to source compiling (1994-09-19)
Re: Source to source compiling (1994-09-22)
Re: Source to source compiling (1994-09-22)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (Thomas O'Connor)
Keywords: optimize, translator
Organization: Hewlett-Packard VCD
References: 94-09-031
Date: Sun, 18 Sep 1994 04:34:16 GMT

Casper Gripenberg ( wrote:
: I am currently working on a source to source compiler. The source
: language is a kind of state langage. A program containes 1-n states. All
: states have the same kind of structure, ie. in the beginning you initilaze
: variables, then comes some code to execute and at last a bunch of
: conditional jumps to other states.

: Example:

: Do something;
: if (a) goto STATE3;
: if (b) goto STATE2;
: etc...

: ....

: ....

: The destination langage is very c-like. With while loops and if-then-else
: statements.

: Now the problem is that I don't know how to convert these conditional goto
: statements in the source language to while/if-then-else constructs in the
: destination language. How do I check for eg. loops, and presumably some of
: the goto statements just cannot be translated into if-them-else statements.

I have considered this problem to some extent, though I will admit
that I have yet to implemented it. In my case the translation language
looks very similar to yours, except that I add the concept of a task.
In other words, I have a state engine, which contains one or more
stage machines. Each state machine is identified by a particular task
name, each task consists of a set of one or more states. i.e.:

    Do something;
    if (a) goto STATE3;
    if (b) goto STATE2;



    Do something else;
    if (a) goto STATE3;
    if (b) goto STATE2;



The processing of the language is sequential, processing the statements in
the current state of each task until the end of the state, or until a goto
is encountered, then the thread of execution moves on to the next task
until all tasks have been processed, and starts again.

In the target language, C, while and do loops are actually mapped to an if
statement. Because the processing is sequential, when the execution gets
to the end of the while statement, processing for this task ends, and
execution moves on to the next task. When while loop is again visited on
the next scan, the while loop is again executed to the end of the while

Tasks are mapped to elements in an array of tasks in the main function of
the target language. Tasks are structures which contain, among other
things, the current state state of the task (an integer), and a pointer to
a function which performs the processing for that task. On startup, the
main function enters a while loop which calls the task function, with the
current state as an argument. The function returns the new state of the

Each function consists of a switch statement keyed on the current state of
the task. The statements from the state code map pretty well to C
statements in the switch structure. A GOTO in the state code is mapped to
new_state= ; break; statement set in the switch. The function returns
with the value of the new_state variable, or the current state if no goto
was encountered.

The targe source might look something like:

int Task_1(int state)
    switch (state) {
        case 1:
            Do something;
            if (a) new_state = 3; break;
            if (b) new_state = 2; break;

        case 2:
        case 3:

void main()
    struct task {
        int state;
        int (*tfunct)(int state);

    struct task tasks[NO_OF_TASKS];

    [ some initialization code to assign functions to the array]

    while (true) {
        for (i=0; i<NO_OF_TASKS; i++)
            tasks[i].state = (*tasks[i].tfunct(tasks[i].state));

I hope this gives you some ideas. As I say, I haven't had a chance to
do any real implementation. If you do get around to implementing, or
even devising a formal grammer, lexical analyzer, yacc file, etc. I'd
be interested.

Good luck.

Thomas J. O'Connor
Computer Integrated Manufacturing (CIM) Technical Support
Hewlett-Packard Company
Printer Manufacturing Division,
Vancouver Washington

Fax: (206) 212-3550
HP External: (206) 212-5031
HP Telnet: 212-5031

Post a followup to this message

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