2013-10-21 53 views
0

我的ANTLR 4 Lexer.getCharPositionInLine的理解()函數是,它應該爲「在該令牌的第一個字符發生從零開始計數行內字符位置」返回--The定形的Antlr 4參考爲什麼antlr 4 Lexer.getCharPositionInLine()不正確地返回0?

使用以下antlr 4語法,似乎Lexer函數getCharPositionInLine()始終返回0.注意COMMENT詞法分析器規則中的Java代碼。它包含打印從getCharPositionInLine()返回的值的代碼。

grammar Expr; 

compilUnit : stat+ EOF ; 
stat : assign NEWLINE ; 

assign : IDENT ASSIGN INT ; 

// Lexer rules 
ASSIGN : '=' {System.out.println(getLine() + ":" + getCharPositionInLine() + " /" + getText() + "/");} ; 

APOS : '\'' {System.out.println(getLine() + ":" + getCharPositionInLine() + " /" + getText() + "/");} ; 

INT : ('0' | '-'? [1-9][0-9]*) {System.out.println(getLine() + ":" + getCharPositionInLine() + " /" + getText() + "/");} ; 

IDENT : [a-zA-Z][a-zA-Z0-9]* {System.out.println(getLine() + ":" + getCharPositionInLine() + " /" + getText() + "/");} ; 


/* For lines that have only a comment preceeded by optional white space, 
* skip the entire line including the newline. For lines that have a 
* comment preceeded by other code skip the comment and return a 
* NEWLINE. */ 
COMMENT : [ \t]* APOS NEND* END 
    { 
     int line = getLine(); 
     int pos = getCharPositionInLine(); 
     System.out.println("COMMENT " + line + ":" + pos + " /" + getText() + "/"); 
     if (pos == 0) { 
      skip(); 
     } 
     else { 
      setType(NEWLINE); 
      setText("\n"); 
     } 
    } 
    ; 

NEWLINE : END+ {System.out.println(getLine() + ":" + getCharPositionInLine() + " /" + getText() + "/");} ; 

WS : [ \t]+ {System.out.println(getLine() + ":" + getCharPositionInLine() + " /" + getText() + "/"); skip();} ; 

fragment END : '\u000c'? '\r'? '\n' ; 
fragment NEND : ~[\u000c\r\n] ; 

我使用這三個命令,在命令行:

java -jar antlr/antlr-4.1-complete.jar Expr.g4 
javac -cp antlr/antlr-4.1-complete.jar Expr*.java 
java -cp "antlr/antlr-4.1-complete.jar;." org.antlr.v4.runtime.misc.TestRig Expr compilUnit -tokens progs/hello.laf 

與此輸入:

'Yo 
x = 3 'Yay 

我得到這樣的輸出:

COMMENT 2:0 /'Yo 
/
2:1 /x/ 
2:2// 
2:3 /=/ 
2:4// 
2:5 /3/ 
COMMENT 3:0/'Yay 
/
[@0,4:4='x',<4>,2:0] 
[@1,6:6='=',<1>,2:2] 
[@2,8:8='3',<3>,2:4] 
[@3,16:15='<EOF>',<-1>,3:0] 
line 3:0 missing NEWLINE at '<EOF>' 

看樣子那是因爲評論詞法分析器r ule包括匹配一個換行符,詞法分析器已經將行號加1,並將字符位置重置爲0.但是,這與「The Definitive Antlr 4 Reference」中的文檔不符。我究竟做錯了什麼?或者這是Antlr 4中的一個錯誤?

回答

相關問題