2011-08-19 33 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,不客气。 –