[antlr-interest] C# generation bug - mutiple decls in same scope

David Jung t7 at pobox.com
Wed Jul 7 12:26:19 PDT 2004

I'm using the C# code generation, but I cann't compile the output due
to a bug in the code generator.  Using v2.7.4 & Mono1.0 on Linux.
After running the lexer .g file to produce a .cs file, trying to
compile it yields multiple errors:

ScigolLexer.cs(1488) error CS0136: A local variable named `_saveIndex'
cannot be declared in this scope since it would give a different
meaning to `_saveIndex', which is already used in a `parent or
current' scope to denote something else

Indeed, looking at the generated code we see instances of:
		int _saveIndex = 0;
		_saveIndex = text.Length;
		text.Length = _saveIndex;
			switch ( LA(1) )
			case 'n':
				int _saveIndex = 0;
				_saveIndex = text.Length;
				text.Length = _saveIndex;

which is an error, as the variable _saveIndex cannot be redeclared
in a nested block (unlike Java?).  See page 70 of the C# spec
"The local variable declaration space of a block includes any nested
blocks.  Thus, within a nested block it is not possible to declare a
local variable or constant with the same name as a local variable or
constant in an enclosing block."

I don't understand the internals of the C# code generator well enough
to see how to fix it.  It obviously has something to do with the
bSaveIndexCreated variable and declareSaveIndexVariableIfNeeded()

Has anyone else come upon this?  Any clues how to fix it?

Yahoo! Groups Links

<*> To visit your group on the web, go to:

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:

More information about the antlr-interest mailing list