gpt4 book ai didi

antlr4 - ANTLR 4 : Dynamic Token

转载 作者:行者123 更新时间:2023-12-04 14:14:57 27 4
gpt4 key购买 nike

鉴于:

grammar Hbs;

var: START_DELIM ID END_DELIM;

START_DELIM: '{{';

END_DELIM: '}}';

我想知道怎么改 START_DELIMEND_DELIM在运行时为例如 <%%> .

有谁知道如何在 ANTLR 4 中做到这一点?

谢谢。

最佳答案

有一种方法,但您需要将语法与目标语言联系起来(到目前为止,唯一的目标是 Java)。

这是一个快速演示(我添加了一些注释以澄清问题):

grammar T;

@lexer::members {

// Some default values
private String start = "<<";
private String end = ">>";

public TLexer(CharStream input, String start, String end) {
this(input);
this.start = start;
this.end = end;
}

boolean tryToken(String text) {

// See if `text` is ahead in the CharStream.
for(int i = 0; i < text.length(); i++) {

if(_input.LA(i + 1) != text.charAt(i)) {

// Nope, we didn't find `text`.
return false;
}
}

// Since we found the text, increase the CharStream's index.
_input.seek(_input.index() + text.length() - 1);

return true;
}
}

parse
: START ID END
;

START
: {tryToken(start)}? .
// The `.` is needed because a lexer rule must match at least 1 char.
;

END
: {tryToken(end)}? .
;

ID
: [a-zA-Z]+
;

SPACE
: [ \t\r\n] -> skip
;
{ ... }?是一个语义谓词。见: https://github.com/antlr/antlr4/blob/master/doc/predicates.md

这是一个小测试类:

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

public class Main {

private static void test(TLexer lexer) throws Exception {
TParser parser = new TParser(new CommonTokenStream(lexer));
ParseTree tree = parser.parse();
System.out.println(tree.toStringTree(parser));
}

public static void main(String[] args) throws Exception {

// Test with the default START and END.
test(new TLexer(new ANTLRInputStream("<< foo >>")));

// Test with a custom START and END.
test(new TLexer(new ANTLRInputStream("<? foo ?>"), "<?", "?>"));
}
}

运行演示如下:

*尼克斯
java -jar antlr-4.0-complete.jar T.g4
javac -cp .:antlr-4.0-complete.jar *.java
java -cp .:antlr-4.0-complete.jar Main

视窗
java -jar antlr-4.0-complete.jar T.g4
javac -cp .;antlr-4.0-complete.jar *.java
java -cp .;antlr-4.0-complete.jar Main

您将看到以下内容被打印到控制台:

(parse << foo >>)
(parse <? foo ?>)

关于antlr4 - ANTLR 4 : Dynamic Token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15295198/

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