Variable declaration in middle of block?

S

seamoon

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?
 
I

infobahn

seamoon said:
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?

{
just generate a new block...


} and close that block when you don't need that object any more.
 
C

Chris Dollin

seamoon said:
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?

(a) Compile to the C-like subset of C++.

(b) Introduce a new block for each variable declaration.

(c) Declare all the variables at the start of the C function.

They're all perfectly straightforward.
 
J

Jens.Toerring

seamoon said:
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?

Creating new variables after the start of a block was not possible
with the old C89 standard but is allowed in the newer C99. Unfor-
tunately, fully compliant C99 compilers are still rare and thus
using this new feature will still restrict portability. On the other
hand, it is always possible to start a new block by enclosing code
in curly braces, and at the start of such a block you can create the
new you need variables, which then are visible within this block. So
you could start a new block on each new variable to create and close
the block only at the end of the function, i.e. use something like

void my_func( void )
{
int a = 0, b = 42;
do_something( a, b );
{
int c = a + b;
do_something_else( a, c );
{
int d = a - c;
do_something_other( c, b + d );
}
}
}

This doesn't look too nice, but automatically translated code seldom
looks nice and is usually not meant to be read by humans...

Regards, Jens
 
E

Eric Sosman

seamoon said:
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?

Three:

- Target C as defined by the latest "C99" version of
the Standard, which allows variable declarations
pretty much anywhere. Complete C99 implementations
are rather rare, but some "halfway there" compilers
may already accept this if invoked properly.

- A block can appear wherever a statement is legal,
so you can start a block whenever you need to
declare new variables. The resulting code would
be rather ugly, but since it's "object code" as
far as your compiler is concerned that should be
only a small disadvantage.

- Compilers, by their nature, need to be able to
rearrange things. Let your user declare variables
wherever he wants, but have your compiler gather
all the declarations together and stick them at
the beginning when it emits the C code.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
D

David Resnick

seamoon said:
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?

It is allowed in C99.

If you aren't doing C99, you always could create another block
before each internal declaration and close it before the
end of the function.

e.g.
void myfunc(void)
{
int foo;
call_some_func(foo);
{
int bar;
call_some_func(bar);
}
}

The semantics of this may differ slightly from what you want, in
that if you redeclare foo in the middle of the function it is an
illegal redeclaration if it is declared at the top I think(C99), but
adding an extra scope (as above) will make it work just fine, masking
the variable in the outer scope.

-David
 
M

Martin Ambuhl

seamoon said:
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?

Here are three choices (there are more):
1) Declare them at the top of the block
2) Use a C99 compiler
3) create a block beginning at the point of declaration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top