ANTLR 3.2(及之前)CommonTokenStream extends BufferedTokenStream which has a List<Token> tokens that is returned when one calls getTokens(). But this List<Token> tokens only gets filled at certain times. In 3.3 and 3.4 it does not happen after getTokens() where 3.2 does fill the tokens list.public List getTokens() { if ( p == -1 ) { fillBuffer(); } return tokens;}protected void fillBuffer() { // fill `tokens`}ANTLR 3.3(及之后)public List getTokens() { return tokens;}public void fill() { // fill `tokens`}请注意 3.2 的 fill 方法是如何受保护的,并且在 3.3+ 中它是公共的,因此以下有效:Notice how 3.2's fill method is protected and in 3.3+ it is public, so the following works:import org.antlr.runtime.*;public class CSVLexerTest { public static void main(String[] args) throws Exception { // the input source String source = "val1, value2, value3, value3.2" + "\n" + "\"line\nbreak\",ABAbb,end"; // create an instance of the lexer CSVLexer lexer = new CSVLexer(new ANTLRStringStream(source)); // wrap a token-stream around the lexer and fill the tokens-list CommonTokenStream tokens = new CommonTokenStream(lexer); tokens.fill(); // traverse the tokens and print them to see if the correct tokens are created // tokens.toString(); int n = 1; for (Object o : tokens.getTokens()) { CommonToken token = (CommonToken) o; System.out.println("token(" + n + ") = " + token.getText().replace("\n", "\\n")); n++; } }}产生输出:token(1) = val1token(2) = ,token(3) = value2token(4) = ,token(5) = value3token(6) = ,token(7) = value3.2token(8) = \ntoken(9) = "line\nbreak"token(10) = ,token(11) = ABAbbtoken(12) = ,token(13) = endtoken(14) = <EOF> 这篇关于ANTLR API 问题;提供示例+解决方法;需要解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-15 12:22