Question

I'm now writing C# grammar using Antlr 3 based on this grammar file.

But, I found some definitions I can't understand.

NUMBER:
    Decimal_digits INTEGER_TYPE_SUFFIX? ;
// For the rare case where 0.ToString() etc is used.
GooBall
@after        
{
    CommonToken int_literal = new CommonToken(NUMBER, $dil.text);
    CommonToken dot = new CommonToken(DOT, ".");
    CommonToken iden = new CommonToken(IDENTIFIER, $s.text);

    Emit(int_literal); 
    Emit(dot); 
    Emit(iden); 
    Console.Error.WriteLine("\tFound GooBall {0}", $text); 
}
    :
    dil = Decimal_integer_literal d = '.' s=GooBallIdentifier
    ;

fragment GooBallIdentifier
    : IdentifierStart IdentifierPart* ;

The above fragments contain the definition of 'GooBall'. I have some questions about this definition.

Why is GooBall needed? Why does this grammar define lexer rules to parse '0.ToString()' instead of parser rules?

Was it helpful?

Solution 2

Sorry. I found the reason from the official FAQ pages.

Now if you want to add '..' range operator so 1..10 makes sense, ANTLR has trouble distinguishing 1. (start of the range) from 1. the float without backtracking. So, match '1..' in NUM_FLOAT and just emit two non-float tokens:

OTHER TIPS

It's because that's a valid expression that's not handled by any of the other rules - I guess you'd call it something like an anonymous object, for lack of a better term. Similar to "hello world".ToUpper(). Normally method calls are only valid on variable identifiers or return values ala GetThing().Method(), or otherwise bare.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top