gpt4 book ai didi

ANTLR 4 - 不匹配的输入

转载 作者:行者123 更新时间:2023-12-01 03:06:26 30 4
gpt4 key购买 nike

我正在尝试为我的一个项目开发一个带有 ANTLR 的小型 DSL。
因此,我为词法分析器编写了定义...

lexer grammar SpamkillerLexer;

MAILBOX: 'Mailbox';
PASSWORD: 'Password';
HOST: 'Host';
USER: 'User';
FOLDER: 'Folder';
PORT: 'Port';
ACTIONS: 'Actions';

WHEN: 'When';
SUBJECT: 'Subject';
BODY: 'Body';
EQUALS: 'Equals';
CONTAINS: 'Contains';
THEN: 'Then';
DELETE: 'Delete';
REDIRECT: 'Redirect';
TO: 'to';

BR_OP: '{';
BR_CL: '}';
EQ: '=';

STRING: '"' ( '\\"' | . )*? '"';
LITERAL: [a-zA-Z_0-9]+;

WS : [ \n\t\r]+ -> skip ;

...和解析器...
parser grammar SpamkillerParser;
mailboxes: mailbox+;
mailbox: MAILBOX LITERAL BR_OP settings BR_CL;

settings: setting+;
setting: (key EQ STRING | ACTIONS EQ actions);
key: MAILBOX | PASSWORD | HOST | USER | FOLDER | PORT;

actions: BR_OP action* BR_CL;
action: WHEN condition THEN job;
condition: (SUBJECT | BODY) (EQUALS | CONTAINS) STRING;
job: (DELETE | (REDIRECT TO STRING));

我的测试文件如下所示:
Mailbox Foobar {
Port = "123"
Host = "foohost"
User = "foouser"
Password = "foopass"
Folder = "Inbox"
Actions = {
When Subject Equals "fooooo" Then Delete
When Body Contains "fooooo" Then Redirect to "foo@bar.baz"
}
}

当我测试时 mailboxes在 ANTLR IntelliJ 插件中,它运行良好,我得到了相应的 AST:

AST

但是当我尝试以编程方式解析我的测试文件时,我收到错误...
line 1:8 mismatched input 'Foobar' expecting LITERAL

我试图重新排序我的词法分析器规则,但我的尝试都没有帮助我摆脱错误。有人知道如何解决这个问题吗?

我解析文件的代码如下所示:
String input = FileUtils.readFileToString(new File("test.txt"), Charsets.UTF_8);
CodePointCharStream inputStream = CharStreams.fromString(input);
SpamkillerLexer lexer = new SpamkillerLexer(inputStream);
CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
SpamkillerParser parser = new SpamkillerParser(commonTokenStream);
SpamkillerParser.MailboxesContext mailboxes = parser.mailboxes();

最佳答案

您没有在解析器中指定必须使用哪个标记词汇。所以ANTLR创建了隐式 用于解析器的 token (实际上是简单的终端),而不是在词法分析器中使用 token 。

要解决此问题,请提供 tokenVocab选项:

parser grammar SpamkillerParser;

options {
tokenVocab=SpamkillerLexer;
}

mailboxes: mailbox+;
mailbox: MAILBOX LITERAL BR_OP settings BR_CL;

settings: setting+;
setting: (key EQ STRING | ACTIONS EQ actions);
key: MAILBOX | PASSWORD | HOST | USER | FOLDER | PORT;

actions: BR_OP action* BR_CL;
action: WHEN condition THEN job;
condition: (SUBJECT | BODY) (EQUALS | CONTAINS) STRING;
job: (DELETE | (REDIRECT TO STRING));

关于ANTLR 4 - 不匹配的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56853370/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com