Re: Source to source compiling

f1rederc@iia.org (Chris Frederick)
Tue, 13 Sep 1994 08:07:16 GMT

          From comp.compilers

Related articles
Source to source compiling casper@snakemail.hut.fi (Casper Gripenberg) (1994-09-11)
Re: Source to source compiling eanders+@CMU.EDU (Eric A. Anderson) (1994-09-12)
Re: Source to source compiling norman@flaubert.bellcore.com (1994-09-14)
Re: Source to source compiling mabp@bga.com (1994-09-13)
Re: Source to source compiling f1rederc@iia.org (1994-09-13)
Re: source to source compiling ghiya@vani.cs.mcgill.ca (1994-09-18)
Re: Source to source compiling bernecky@eecg.toronto.edu (Robert Bernecky) (1994-09-18)
Re: Source to source compiling toconnor@vcd.hp.com (1994-09-18)
Re: Source to source compiling pjj@cs.man.ac.uk (1994-09-16)
Re: Source to source compiling rgg@tidos.tid.es (1994-09-19)
Re: Source to source compiling lexa@adam.botik.yaroslavl.su (1994-09-22)
[1 later articles]
| List of all articles for this month |

Newsgroups: comp.compilers
From: f1rederc@iia.org (Chris Frederick)
Keywords: translator
Organization: International Internet Association
References: 94-09-031
Date: Tue, 13 Sep 1994 08:07:16 GMT

> 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.


How about the following (expressed in C-like syntax)? It's simplistic
but it should work okay. If you have case/switch statements available:


state = START_STATE;
while (state != END_STATE)
{
      switch (state)
      {
            case STATE_1:
                  do_something();
                  if (a) { state = STATE_2; break; }
                  if (b) { state = STATE_3; break; }
                  if (c) { state = STATE_4; break; }
                  Error();
                  break;


            case STATE_2:
                  do_something();
                  if (a) { state = STATE_6; break; }
                  if (b) { state = STATE_1; break; }
                  if (c) { state = STATE_5; break; }
                  Error();
                  break;


            etc...


            default:
                  Error();
      }
}




If you don't have case statements but have the continue statement, use
instead:


state = START_STATE;
while (state != END_STATE)
{
      if (state == STATE_1)
      {
            do_something();
            if (a) { state = STATE_2; continue; }
            if (b) { state = STATE_3; continue; }
            if (c) { state = STATE_4; continue; }
            Error();
      }


      if (state == STATE_2)
      {
            do_something();
            if (a) { state = STATE_6; continue; }
            if (b) { state = STATE_1; continue; }
            if (c) { state = STATE_5; continue; }
            Error();
      }


      Error();
}




And finally, if you have neither case statements nor the continue statement:


state = START_STATE;
while (state != END_STATE)
{
      if (state == STATE_1)
      {
            do_something();
            if (a) { state = STATE_2; }
            else if (b) { state = STATE_3; }
            else if (c) { state = STATE_4; }
            else Error();
      }
      else if (state == STATE_2)
      {
            do_something();
            if (a) { state = STATE_6; }
            else if (b) { state = STATE_1; }
            else if (c) { state = STATE_5; }
            else Error();
      }
      else ...
      else Error();
}




> One kludgy solution would be to model every state as a function in the
> destination language. Then every goto statement in the source language
> would simply convert to a function call in the destination language.


You don't want to do that... the activation records would almost
certainly overrun the machine's stack space in a short time.


Good luck,
Chris
--
Chris Frederick
f1rederc@iia.org
--


Post a followup to this message

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