ANTLR4 不在決議樹中顯示規則名稱。
例如,1 2列印為:

主要代碼:
std::string test = "1 2";
ANTLRInputStream input(test);
GrammarLexer lexer(&input);
CommonTokenStream tokens(&lexer);
GrammarParser parser(&tokens);
auto *tree = parser.expression();
std::cout << tree->toStringTree(true) << "\n";
uj5u.com熱心網友回復:
我深入研究了 ANTLR 的 C 運行時源代碼,發現了這兩個函式:
/// Print out a whole tree, not just a node, in LISP format
/// {@code (root child1 .. childN)}. Print just a node if this is a leaf.
virtual std::string toStringTree(bool pretty = false) = 0;
/// Specialize toStringTree so that it can print out more information
/// based upon the parser.
virtual std::string toStringTree(Parser *parser, bool pretty = false) = 0;
因此,要修復“錯誤”,請替換
tree->toStringTree(true)
和
tree->toStringTree(&parser, true)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/438981.html
