antlr grammar multiple alternatives

Posted by joe on Stack Overflow See other posts from Stack Overflow or by joe
Published on 2010-04-21T21:34:00Z Indexed on 2010/04/21 21:53 UTC
Read the original article Hit count: 730

Filed under:

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

© Stack Overflow or respective owner

Related posts about antlr