如何制作简单的ANTLR语法扩展?

发布于 2025-01-27 16:21:58 字数 1387 浏览 5 评论 0原文

我正在编写一个使用ANTLR来解析Java式表达式的框架。我想到创建一种新型的自由形式字面意思。文字看起来与字符串相似,所以我认为我使用的是我使用的Java8语法,而新的文字与字符串相同,但由''字符而不是'“”。

所以我创建了:

ExternalLiteral
    :   '`' StringCharacters? '`'
    ;

在Lexer和修改后:

fragment
StringCharacter
    :   ~["`\\\r\n]
    |   EscapeSequence
    ;

因此

fragment
EscapeSequence
    :   '\\' [btnfr"'`\\]
    |   OctalEscape
    |   UnicodeEscape // This is not in the spec but prevents having to preprocess the input
    ;

,“”将被视为特殊性格,与''相同。然后,在语法中,我修改了

literal
    :   IntegerLiteral
    |   FloatingPointLiteral
    |   BooleanLiteral
    |   CharacterLiteral
    |   StringLiteral
    |   ExternalLiteral
    |   NullLiteral
    ;

它似乎对我有用,但是当我尝试解析任何此类表达式时,例如`0`,我得到了:

line 1:3 mismatched input '<EOF>' expecting {'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'new', 'short', 'super', 'this', 'void', IntegerLiteral, FloatingPointLiteral, BooleanLiteral, CharacterLiteral, StringLiteral, ExternalLiteral, 'null', '(', '!', '~', '++', '--', '+', '-', Identifier, '@'}
line 1:3 missing {IntegerLiteral, FloatingPointLiteral, BooleanLiteral, CharacterLiteral, StringLiteral, ExternalLiteral, 'null'} at '<EOF>'

我之前与Antlr打架时,我不知道是antlr还是我,这是问题。有没有比我更多的经验的人看到我可能做错了什么?

I'm writing a framework that uses ANTLR to parse Java-style expressions. I had in mind to create a new type of free-form literal. The literal will look similar to a string, so I thought to extend the Java8 grammar I'm using with a new literal identical to StringLiteral, but bounded by '`' characters instead of '"'.

So I created:

ExternalLiteral
    :   '`' StringCharacters? '`'
    ;

in the Lexer and modified:

fragment
StringCharacter
    :   ~["`\\\r\n]
    |   EscapeSequence
    ;

and

fragment
EscapeSequence
    :   '\\' [btnfr"'`\\]
    |   OctalEscape
    |   UnicodeEscape // This is not in the spec but prevents having to preprocess the input
    ;

so '`' would be treated as a special character, identically to '"'. Then in the grammar I modified

literal
    :   IntegerLiteral
    |   FloatingPointLiteral
    |   BooleanLiteral
    |   CharacterLiteral
    |   StringLiteral
    |   ExternalLiteral
    |   NullLiteral
    ;

That seems like it would work to me, but when I try to parse any such expressions, e.g.`0`, I get:

line 1:3 mismatched input '<EOF>' expecting {'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'new', 'short', 'super', 'this', 'void', IntegerLiteral, FloatingPointLiteral, BooleanLiteral, CharacterLiteral, StringLiteral, ExternalLiteral, 'null', '(', '!', '~', '++', '--', '+', '-', Identifier, '@'}
line 1:3 missing {IntegerLiteral, FloatingPointLiteral, BooleanLiteral, CharacterLiteral, StringLiteral, ExternalLiteral, 'null'} at '<EOF>'

I've had fights with ANTLR before, I don't know if it's ANTLR or me that is more the problem. Does anyone with more experience than me see what I might've done wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文