1

在第三行输入以下错误时:

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调试了一下,发现调试器输出窗口显示了第三行对应的错误信息:

output/__Test___input.txt line 3:8 required (...)+ loop does not match any at input '' output/__Test___input.txt line 3:9 缺少 END_COMMAND at 'tenga'

但是当我自己运行应用程序时,这些错误消息不会显示在控制台上。

每当错误出现在第一行时,错误消息就会显示在控制台上,例如:

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

当错误不在第一行时,我怎么能让它们也显示在控制台上?


                         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; } ;
4

1 回答 1

0

那是因为在您的main方法中,您在调试时调用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.

于 2011-08-19T07:13:52.393 回答