-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
seamoon wrote:
Hi,
I'm doing a simple compiler with C as a target language. My language uses
the possibility to declare variables anywhere in a block with scope to the
end of the block. As I remembered it this would be easily translated to C,
but it seems variable declaration is only possible in the beginning of a
block in C. Any suggestions how to get around this?
This question would be better asked and answered in a group like
comp.compilers
But, for a quick answer, I'd have to say that you have a couple of
options that I can think of.
If your target is C99, then you can generate your resulting C code such
that the variables are defined anywhere in block scope.
However, if your target is not C99, then you'll probably have to build a
two-pass compiler with symbol table. First pass looks through the source
language for declarations, and stores the symbols in the symbol table.
When it reaches the end of the scope of the symbols (i.e. end of a block
of code, where symbols have block scope), it goes back to the beginning
of the block and commences pass 2. The second pass emits the C block
prolog code, including the declarations of block-scope variables, then
continues through the source block, generating C statement code. Pass 2
has to ignore source declarations (except for matching them agains the
symbol table), so that it won't generate declarations mid-block.
Followups set to comp.compilers
- --
Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group
(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)
iD8DBQFCJIFuagVFX4UWr64RAv0MAKDXtmo1Dp6+u+/9mwcVI0vlN9RKFQCg2LPK
GLzY6SfJpfaYtAZesnRxzPU=
=ASJr
-----END PGP SIGNATURE-----