Related articles |
---|
[4 earlier articles] |
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) |
Re: Source to source compiling md@pact.srf.ac.uk (1994-09-22) |
Newsgroups: | comp.compilers |
From: | lexa@adam.botik.yaroslavl.su (lexa) |
Keywords: | translator |
Organization: | Research Centre for Multiprocessor Systems, Program Systems Institute, Russian Academy of Sciences |
Date: | Thu, 22 Sep 1994 18:29:00 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.
> ...
> 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.
Why kludgy? Good solution. Let's only modify it a bit. Each state
is modelled as a function, and on the top we have the
"state monitor":
------------------------------
void (* state_n())() /* This is a function that models state */
{
initializing ... ;
do_something ... ;
/* Here next state is returned - it is a pointer to another
state function */
if (a) return state_i;
if (b) return state_j;
if (c) return state_k;
return state_error;
}
state_monitor ()
{
/* Define next_state variable as a
pointer to function that returns
pointer to function that returns
void.
Initialize it into initial state function. */
void (*(* next_state)())() = state_0;
...
/* Main loop. Here a function which is pointed by next_state
is called - it returns pointer to the next state function */
while (next_state != 0)
next state = (* next_state)();
...
}
------------------------------------------------
Casts are omitted - with them it would be nice C exercise.
You see: no goto's, nor if-then-else's - only if-return's and
one call.
I used such an implementation of finite automata when
hard-coding a C lexer. Some state functions contained
monitors, so there were monitor hierarhy there. Worked well.
If pointers to functions are absent in target language, you'll
need to return integers from state functions. They could be used
in monitor to choose appropriate function (with if-then-elses
and binary search or with switch).
Hope this helps.
Good luck,
Lexa
---
Alexey I. Adamovich, | e-mail:
researcher | lexa@adam.botik.yaroslavl.su
PSI RAS |
Russia |
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.