ANTLR4 将 ParserRuleContext 树展平为数组

Jav*_*Man 4 antlr4

如何将ParserRuleContext带有子树的 a 展平为令牌数组?看起来ParserRuleContext.getTokens(int ttype)不错。但什么是ttype?是token类型吗?如果我想包含所有令牌类型,应使用什么值?

Bar*_*ers 5

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)