gpt4 book ai didi

ANTLR4 将 ParserRuleContext 树展平为数组

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

如何压平 ParserRuleContext将子树转换为 token 数组? ParserRuleContext.getTokens(int ttype)看起来挺好的。但什么是 ttype ?是 token 类型吗?如果我想包含所有 token 类型,要使用什么值?

最佳答案

ParserRuleContext.getTokens(int ttype)只检索父节点的某些子节点:它不会递归地进入父树。

但是,编写自己很容易:

/**
* Retrieves all Tokens from the {@code tree} in an in-order sequence.
*
* @param tree
* the parse tee to get all tokens from.
*
* @return all Tokens from the {@code tree} in an in-order sequence.
*/
public static List<Token> getFlatTokenList(ParseTree tree) {
List<Token> tokens = new ArrayList<Token>();
inOrderTraversal(tokens, tree);
return tokens;
}

/**
* Makes an in-order traversal over {@code parent} (recursively) collecting
* all Tokens of the terminal nodes it encounters.
*
* @param tokens
* the list of tokens.
* @param parent
* the current parent node to inspect for terminal nodes.
*/
private static void inOrderTraversal(List<Token> tokens, ParseTree parent) {

// Iterate over all child nodes of `parent`.
for (int i = 0; i < parent.getChildCount(); i++) {

// Get the i-th child node of `parent`.
ParseTree child = parent.getChild(i);

if (child instanceof TerminalNode) {
// We found a leaf/terminal, add its Token to our list.
TerminalNode node = (TerminalNode) child;
tokens.add(node.getSymbol());
}
else {
// No leaf/terminal node, recursively call this method.
inOrderTraversal(tokens, child);
}
}
}

关于ANTLR4 将 ParserRuleContext 树展平为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22769343/

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