开发者

ANTLRWorks error compiling grammar: "syntax error: invalid char literal: INVALID"

I wrote a stub for a grammar (only matches comments so far), and it's giving me the error:

syntax error: invalid char literal: <INVALID>

Moreover, I've tracked down the error to being in the following command:

... ~LINE_ENDING* ...
LINE_ENDING     : ( '\n' | '\r' | '\r\n');

Can someone h开发者_开发问答elp me fix this?


The ~ operator can only be applied to a set. In a lexer, the elements of a set are characters of an input stream. In other words, you can have this:

~(  'a'..'z'
|   'C'
|   '\r'
|   '\n'
)

But you can't have this because it's a sequence (of two characters) instead of a set.

~('\r\n')

The problem you encountered is an extension of this second case.


Not sure why you get that error (I have no means of testing it myself at the moment). Perhaps the fact you're negating either a single char (\r or \n) or a double char (\r\n) is an issue?

What happens if you try:

SingleLineComment
    :   '//' (~LineBreakChar)* (NewLine | EOF)
    ;

LineBreakChar
    :   '\r' | '\n'
    ;

NewLine
    :   '\r'? '\n' | '\r'
    ;

?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜