tags:

views:

181

answers:

2

I have this simple grammar for a C# like syntax. I can't figure out any way to separate fields and methods. All the examples I've seen for parsing C# combine fields and methods in the same rule. I would like to split them up as my synatx is pretty simple.

grammar test;

options
{
    language =CSharp2;
    k = 3;
    output = AST;
}

SEMI : ';' ;
LCURLY : '{' ;
RCURLY : '}' ;
LPAREN : '(' ;
RPAREN : ')' ;
DOT :'.';

IDENTIFIER  
    :   ( 'a'..'z' | 'A'..'Z' | '_' )
        ( 'a'..'z' | 'A'..'Z' | '_' | '0'..'9' )*
    ;

 namespaceName 
    : IDENTIFIER (DOT IDENTIFIER)*
    ;

 classDecl
    : 'class' IDENTIFIER LCURLY (fieldDecl | methodDecl)* RCURLY
    ;

 fieldDecl
    : namespaceName IDENTIFIER SEMI;
 methodDecl
    : namespaceName IDENTIFIER LPAREN RPAREN SEMI;

I always end up wit this warning

Decision can match input such as "IDENTIFIER DOT IDENTIFIER" using multiple alternatives: 1, 2
A: 

Not sure if it's related, but i'm noticing that your field and method rules say

: namespaceName IDENTIFIER ...

That'd allow for something like ns.subnsTheSecondIdentifier, which would be ambiguous. (The second identifier would be swallowed up with the namespace name.)

You may need to change your namespaceName rule to look like

: IDENTIFIER DOT (IDENTIFIER DOT)*

in order to make parsing work right.

I also don't see you accounting for whitespace anywhere. Not sure if you have to, as i don't mess with ANTLR. But the sample grammars i've seen so far are for math expressions and such, which don't require whitespace. C#'ish grammars do -- like, for example, you need whitespace after 'class', and you can have it pretty much anywhere but inside an identifier.

cHao
That isn't true. namespaceName IDENTIFIER matches NS.Type <space> FieldName or Type <space> FieldName
joe
Doesn't seem to by default. See http://www.antlr.org/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3#FiveminuteintroductiontoANTLR3-Handlewhitespace .
cHao
A: 

Since namespaceName can be IDENTIFIER DOT IDENTIFIER DOT IDENTIFIER ... I think you have problems with k=3 in your options.

Can you remove the K option, ANTLR will default to K=*.

WayneH
Thanks this solved the issue
joe