gpt4 book ai didi

java - ANTLR API 问题;示例 + 提供的解决方法;需要解释

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:59:43 26 4
gpt4 key购买 nike

我使用 ANTLRWorks 创建了以下 Lexer。 (另见 http://bkiers.blogspot.com/2011/03/2-introduction-to-antlr.html#intro)

 // CSVLexer.g
lexer grammar CSVLexer;

@lexer::header {
package graphica.parsers;
}

Comma
: ','
;

LineBreak
: '\r'? '\n'
| '\r'
;

SimpleValue
: ~(',' | '\r' | '\n' | '"')+
;

QuotedValue
: '"' ('""' | ~'"')* '"'
;

我使用以下 Java 类来测试词法分析器。

 /**
*
* @author Nilo
*/
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
CommonTokenStream tokens = new CommonTokenStream(lexer);


// 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 循环之前插入一个 tokens.toString() ,则会按预期打印输出。

注意:我在带有 JDK 1.7/64 位的 Windows 7 上使用 ANTLWorks 1.4.3、ANTLR 3.4

问题:我不明白这一点。请解释。应该有一种方法可以在没有 tokens.toString()

的情况下使它工作

最佳答案

CommonTokenStream延伸BufferedTokenStream它有一个 List<Token> tokens当调用 getTokens() 时返回.但是这个List<Token> tokens只在特定时间被填满。在 3.3 和 3.4 中,它不会发生在 getTokens() 之后其中 3.2 确实填充了 tokens列表。

ANTLR 3.2(及之前版本)

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+ 中它是公开的,所以下面的工作:

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) = val1
token(2) = ,
token(3) = value2
token(4) = ,
token(5) = value3
token(6) = ,
token(7) = value3.2
token(8) = \n
token(9) = "line\nbreak"
token(10) = ,
token(11) = ABAbb
token(12) = ,
token(13) = end
token(14) = <EOF>

关于java - ANTLR API 问题;示例 + 提供的解决方法;需要解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8560556/

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