[antlr-interest] How do I set token type in ANTLR 3.1b1?
Johannes Luber
JALuber at gmx.de
Sun Jun 8 15:34:39 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).
Actually, the reason is that a function is missing in CSharpTarget.java. How that happened is a total mystery to me, as I copied the contents from CSharpTarget.java wholly...
> CSharp2 target doesn't have that problem, but has others:
> while $token.text in the grammar must be changed to $token.Text,
That isn't supposed to be necessary. I did explicitly test all cases and found that .text is sufficient then (I even changed the wiki text afterwards). Does anyone else have this problem? Really - is this an isolated problem? I once had to use .Text in 3.0.1, too, which prompted my quest to undo this difference. But a search where .Text would be translated failed to show up any result, a test grammar created to replicate this issue worked fine (for whatever reason), and as the runtimes otherwise don't touch the action translation stuff, I doubt that the target choice does affect things. But you could test if Java gives the same problems. I'll test that myself this week, once I get my development workbook back and can recreate my test environment. Due to an unforeseen problem with wildlife it may not be Monday as planned...
> $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
The code should work, and because I don't have my notebook for four weeks already, trunk is still the same as in b1. But you could try it anyways. Still, you could tell more about your environment - maybe somehow it affects ANTLR?
Johannes
--
GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx
More information about the antlr-interest
mailing list