开发者

Antlr beginner mismatchedtoken question

I'm just starting out with Antlr, so please forgive the noob开发者_如何转开发 question here. I'm lost. Any help is appreciated.

This is my grammar script:

grammar test;

script : 
    'begin script' IDENT ':'

    'end script' IDENT
    ;

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

This is the script I'm trying to run it against:

begin script spork:

end script spork

The result in ANTLRWorks 1.3.1:

Antlr beginner mismatchedtoken question

What am I doing wrong?


You usually want to tell ANTLR to ignore whitespace with a lexer rule:

WHITESPACE
    :   ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+
        { $channel = HIDDEN; } ;


I don't have much experience with AntLR, but my guess is that the lexer is producing a token for the newline and it's not matched in your grammar.

Normally when you're writing a parser you have to tell the lexer explicitly to ignore whitespace and line endings. But I could be wrong, that could be something that AntLR is supposed to do for you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜