2011-08-19 67 views
1

當輸入有錯誤的以下輸入在第三行:ANTLR antlrWorks錯誤消息不dispayed到輸出控制檯

SELECT entity_one, entity_two FROM myTable; 
first_table, extra_table as estable, tineda as cam; 
asteroid tenga, tenta as myName, new_eNoal as coble 

我antlrWorks調試它,發現對應於第三行中的錯誤消息被示出的調試器輸出窗口上:

輸出/ 測試 _input.txt線3:8所需的(...)+環不匹配在輸入「」 輸出/ 測試 _input.txt線的任何'tenga'3:9失蹤END_COMMAND

但是當我自己運行應用程序時,這些錯誤消息不會顯示在控制檯上。

錯誤消息得到顯示在控制檯上每當錯誤是上類似於第一行:

asteroid tenga, tenta as myName, new_eNoal as coble 
SELECT entity_one, entity_two FROM myTable; 
first_table, extra_table as estable, tineda as cam; 

控制檯輸出:

inputSql.rst線1:8所要求(...)+ ' inputSql.rst行1:9在'tenga'缺少END_COMMAND

如果錯誤不在第一行,我怎麼能讓它們顯示在控制檯上呢?因爲在你main方法


      UserRequest.g  

grammar UserRequest; 

tokens{ 
    COMMA = ',' ;  
    WS = ' ' ; 
    END_COMMAND = ';' ; 
} 

@header { 
package com.linktechnology.input; 
} 

@lexer::header { 
package com.linktechnology.input; 
} 

@members{ 
    public static void main(String[] args) throws Exception { 
      UserRequestLexer lex = new UserRequestLexer(new ANTLRFileStream(args[0])); 
      CommonTokenStream tokens = new CommonTokenStream(lex); 

      UserRequestParser parser = new UserRequestParser(tokens); 

      try { 
       parser.request(); 
      } catch (RecognitionException e) { 
       e.printStackTrace(); 
      } 
     } 
} 


/*------------------------------------------------------------------ 
* PARSER RULES 
*------------------------------------------------------------------*/ 

process : request* EOF ; 

request : (sqlsentence | create) END_COMMAND ; 

sqlsentence : SELECT fields tableName ; 

fields : tableName (COMMA tableName)* FROM ;     

create : tableName (COMMA tableName)+ ; 

tableName : WS* NAME (ALIAS NAME)? ;   


/*------------------------------------------------------------------ 
* LEXER RULES 
*------------------------------------------------------------------*/ 

NAME : LETTER (LETTER |DIGIT | '-' | '_')* ; 

fragment LETTER: LOWER | UPPER; 

fragment LOWER: 'a'..'z'; 

fragment UPPER: 'A'..'Z'; 

fragment DIGIT: '0'..'9';  

SELECT : ('SELECT ' |'select ') ; 

FROM : (' FROM '|' from ') ; 

ALIAS : (' AS ' |' as ') ; 

WHITESPACE : ( '\r' | '\n' | '\t' | WS | '\u000C')+ { $channel = HIDDEN; } ; 

回答

0

也就是說,你調用parser.request()而在調試時,你選擇的process規則爲出發點。並且由於request從您的輸入中消耗一個(sqlsentence | create) END_COMMAND,它不會產生錯誤。

更改main方法爲:

@members{ 
    public static void main(String[] args) throws Exception { 
     UserRequestLexer lex = new UserRequestLexer(new ANTLRFileStream(args[0])); 
     CommonTokenStream tokens = new CommonTokenStream(lex); 

     UserRequestParser parser = new UserRequestParser(tokens); 

     try { 
      parser.process(); 
     } catch (RecognitionException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

,你就會在控制檯上看到同樣的錯誤,因爲process迫使解析器消耗整個輸入,一路EOF

+0

現在運行平穩。謝謝,巴特! – Manny

+0

@Manny,不客氣。 –