473,327 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Variable declaration in middle of block?

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?

Nov 14 '05 #1
7 7666
seamoon wrote:
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.

--
Chris "electric hedgehog" Dollin
Nov 14 '05 #2
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?


{
just generate a new block...
} and close that block when you don't need that object any more.
Nov 14 '05 #3
seamoon <re******@group.please> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #4
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?


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.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #5
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?


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
Nov 14 '05 #6
-----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-----
Nov 14 '05 #7
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?


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.
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
6
by: craigbeanhead | last post by:
Hi, I'm teaching myself C from K&R2. I've come across something that I really don't understand. When I try to compile the following code (in VC++7), I get an "undeclared identifier" error. When...
5
by: Neil Zanella | last post by:
Hello, Unlike in pre-C99 versions of C where variables can only be defined at the beginning of blocks, C99 allows variables to be defined in arbitrary places inside blocks. However, gcc 3.2.2...
10
by: ElanKathir .S.N | last post by:
Hi all ! VB.NET adds the ability to create variables that are visible only within a block. A block is any section of code that ends with one of the words End , Loop , or Next . This means that...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
22
by: James Brodie | last post by:
I just wanted to get some advice on what are the better practices: 1) Should variables be declared at the beginning of a function or just before they are required? 2) Should all variabled be...
37
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
2
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
3
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.