[antlr-interest] How do I set token type in ANTLR 3.1b1?

Vaclav Barta vbar at comp.cz
Sun Jun 8 01:27:55 PDT 2008


Hi,

I've been trying out ANTLR 3.1b1 with C#, and I'm running into 
trouble... My grammar (available at
http://macroscope.svn.sourceforge.net/viewvc/macroscope/MacroScope/MacroScope.g?revision=2 
) has a rule for numbers:

Number :
	( (Digit)+ ('.' | 'e') ) => (Digit)+ ( '.' (Digit)* (Exponent)? | 
Exponent) { $type = Real; }
	| '.' { $type = DOT; } ( (Digit)+ (Exponent)? { $type = Real; } )?
	| (Digit)+ { $type = Integer; }
	| '0x' ('a'..'f' | Digit)* { $type = HexLiteral; } // "0x" is valid hex 
literal
	;

That works fine with ANTLR 3.0.1, target CSharp. For ANTLR 3.1b1, CSharp 
is unusable (see 
http://www.antlr.org/pipermail/antlr-interest/2008-May/028461.html 
for details). CSharp2 target doesn't have that problem, but has others: 
while $token.text in the grammar must be changed to $token.Text, 
$token.type doesn't work at all: $type in the rule above generates 
compilable code which fails at runtime with NullReferenceException while 
$Type is just copied to the generated code, which therefore doesn't 
compile. What is the correct way to set an explicit token type? Would it 
help if I compiled ANTLR from the latest sources?

	Bye
		Vasek
--
http://www.mangrove.cz/
Open Source integration


More information about the antlr-interest mailing list