开发者

antlr grammar multiple alternatives

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开发者_如何学JAVA" using multiple alternatives: 1, 2


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=*.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜