[antlr-interest] C# Parser : problem with identifying a class declaration

Aravinda Dassanayake aravinda777 at gmail.com
Mon Jan 14 03:36:22 PST 2008


Hi everybody,

I'm working on a C# parser to implement a code complexity analysis tool. I
need this to be a Java implementation although I ma analysing C# code since
this is goin to be part of a biger tool stack. I've chosen to implement this
following the language spec by MS, and implementing as a combined grammar
like the Java grammar (Which I find a beutiful piece of work!) since my
requirements are not very complex to go ahead with a tree grammar.

However, I have few problems.

The main issue is that the parser doesnt identify a class declaration
properly. It is trying to match it to a delegate declaration no matter what
I do. The rules in conflict are as follows.


type_declaration
    :
    class_declaration
    |struct_declaration
    |interface_declaration
    |enum_declaration
    |delegate_declaration
    ;

class_declaration
    :
    attributes? class_modifiers? 'class' IDENTIFIER class_base? class_body
';'?
    ;

delegate_declaration
    :
    attributes? delegate_modifiers? 'delegate' return_type IDENTIFIER '('
formal_parameter_list? ')' ';'
    ;

enum_declaration
    :
    attributes? enum_modifiers? 'enum' IDENTIFIER enum_base? enum_body ';'?
    ;


class_modifiers
    :
    (class_modifier) (class_modifier)*
    ;

class_modifier
    :
    'new'
    |'public'
    |'protected'
    |'internal'
    |'private'
    |'abstract'
    |'sealed'
    ;


delegate_modifiers
    :
    (delegate_modifier) (delegate_modifier)*
    ;

delegate_modifier
    :
    'new'
    |'public'
    |'protected'
    |'internal'
    |'private'
    ;

enum_modifiers
    :
    (enum_modifier) (enum_modifier)*
    ;


enum_modifier
    :
    'new'
    |'public'
    |'protected'
    |'internal'
    |'private'
    ;



Given below are some of the errors given by the generated java parser when
analysing a C# source code.

BR.recoverFromMismatchedToken
line 8:8 mismatched input 'class' expecting 'delegate'
BR.recoverFromMismatchedToken
line 20:9 mismatched input 'int' expecting 'delegate'
BR.recoverFromMismatchedToken
line 21:2 mismatched input '{' expecting ';'
line 22:10 no viable alternative at input '_hash'
line 22:16 no viable alternative at input 'CompareTo'
line 22:28 no viable alternative at input 'TextLine'
line 22:37 no viable alternative at input 'obj'


FYI, the global options are set as follows.

options {
backtrack=true;
memoize=true;
language=Java;
}



Without the class declaration properly identified, the parser does not go
ahead analysing entries within the class properly.

Therefore, a very quick pointer on how to make this properly identify
classes, delegates and enums would be highly appreciated, specially because
time is running out fast for me.

Thanks & Best Regards,
-- 
Aravinda Dassanayake

+94572231204
+94715300757

aravinda777 at yahoo.com
aravinda777 at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080114/c488044b/attachment-0001.html 


More information about the antlr-interest mailing list