2010-02-16 58 views
4

我只是用ANTLR開始,試圖解析一些模式出來一個日誌文件使用ANTLR來分析日誌文件

例如:日誌文件:

7114422 2009-07- 16 15:43:07078 [LOGTHREAD] INFO StatusLog - 任務0 輸入: uk.project.Evaluation.Input.Function1(選擇= [ 「紅色」, 「黃色」]){}

7114437 2009- 07-16 15:43:07,093 [LOGTHREAD]信息狀態日誌 - 任務0 輸出: uk.org.project.Evaluation.Output.Function2(選擇= [ 「火箭」]){}

7114422 2009-07-16 15:43:07078 [LOGTHREAD] INFO StatusLog - 任務0 輸入: uk.project.Evaluation.Input.Function3(選擇= [ 「藍色」, 「黃色」]){}

7114437 2009-07-16 15:43:07093 [LOGTHREAD] INFO StatusLog - 任務0 輸出: uk.org.project.Evaluation.Output.Function4(選擇= [ 「語音」]){}

現在我必須解析這個文件才能找到'Evaluation.Input.Function1',它的值是'red'和'yellow'以及'Evaluation.Output.Function2'和'Rocket'的值,並忽略其他所有內容, 2下面的輸入輸出功能3,4。有許多這樣的輸入和輸出功能,我必須找到這樣的輸入/輸出功能集。這是我嘗試過的語法,它不起作用。任何幫助,將不勝感激。是我在寫作的語法和ANTLR的第一次嘗試它變得相當艱鉅現在..

grammar test; 

    tag : inputtag+ outputtag+ ; 
//Input tag consists of atleast one inputfunction with one or more values 
inputtag: INPUTFUNCTIONS INPUTVALUES+; 

//output tag consists of atleast one ontput function with one or more output values 
outputtag : OUTPUTFUNCTIONS OUTPUTVALUES+; 

INPUTFUNCTIONS 
: INFUNCTION1 | INFUNCTION2; 

OUTPUTFUNCTIONS 
:OUTFUNCTION1 | OUTFUNCTION2; 

// Possible input functions in the log file 
fragment INFUNCTION1 
:'Evaluation.Input.Function1'; 

fragment INFUNCTION2 
:'Evaluation.Input.Function3'; 

//Possible values in the input functions 
INPUTVALUES 
: 'red' | 'yellow' | 'blue'; 

// Possible output functions in the log file 
fragment OUTFUNCTION1 
:'Evaluation.Output.Function2'; 

fragment OUTFUNCTION2 
:'Evaluation.Output.Function4'; 

//Possible ouput values in the output functions 
fragment OUTPUTVALUES 
: 'Rocket' | 'Speech'; 

回答

7

當你只在你解析文件的一部分感興趣,你並不需要一個解析器和寫文件整個格式的文法。只有詞法分析器和ANTLR的options{filter=true;}就足夠了。這樣,你只會抓住你在語法中定義的令牌,而忽略文件的其餘部分。

這裏有一個快速演示:

lexer grammar TestLexer; 

options{filter=true;} 

@lexer::members { 
    public static void main(String[] args) throws Exception { 
    String text = 
     "7114422 2009-07-16 15:43:07,078 [LOGTHREAD] INFO StatusLog - Task 0 input : uk.project.Evaluation.Input.Function1(selected=[\"red\",\"yellow\"]){}\n"+ 
     "\n"+ 
     "7114437 2009-07-16 15:43:07,093 [LOGTHREAD] INFO StatusLog - Task 0 output : uk.org.project.Evaluation.Output.Function2(selected=[\"Rocket\"]){}\n"+ 
     "\n"+ 
     "7114422 2009-07-16 15:43:07,078 [LOGTHREAD] INFO StatusLog - Task 0 input : uk.project.Evaluation.Input.Function3(selected=[\"blue\",\"yellow\"]){}\n"+ 
     "\n"+ 
     "7114437 2009-07-16 15:43:07,093 [LOGTHREAD] INFO StatusLog - Task 0 output : uk.org.project.Evaluation.Output.Function4(selected=[\"Speech\"]){}"; 
    ANTLRStringStream in = new ANTLRStringStream(text); 
    TestLexer lexer = new TestLexer(in); 
    CommonTokenStream tokens = new CommonTokenStream(lexer); 
    for(Object obj : tokens.getTokens()) { 
     Token token = (Token)obj; 
     System.out.println("> token.getText() = "+token.getText()); 
    } 
    } 
} 

Input 
    : 'Evaluation.Input.Function' '0'..'9'+ Params 
    ; 

Output 
    : 'Evaluation.Output.Function' '0'..'9'+ Params 
    ; 

fragment 
Params 
    : '(selected=[' String (',' String)* '])' 
    ; 

fragment 
String 
    : '"' (~'"')* '"' 
    ; 

現在做:

javac -cp antlr-3.2.jar TestLexer.java 
java -cp .:antlr-3.2.jar TestLexer // or on Windows: java -cp .;antlr-3.2.jar TestLexer 

,你會看到下面的被打印到控制檯:

> token.getText() = Evaluation.Input.Function1(selected=["red","yellow"]) 
> token.getText() = Evaluation.Output.Function2(selected=["Rocket"]) 
> token.getText() = Evaluation.Input.Function3(selected=["blue","yellow"]) 
> token.getText() = Evaluation.Output.Function4(selected=["Speech"]) 
+0

很好的例子,但犯規作品=/ –

+2

@arkilus,如果「它」不起作用,那麼它就不是一個好例子,我會假設......但它確實有效。事實上,它不適用於你是一個完全不同的故事:) –

+0

對不起巴特,你的例子實際上完美的作品,問題是與我的構建路徑...順便謝謝你的美妙的幫助與ANTLR在所以:) –