【发布时间】:2011-01-17 15:22:51
【问题描述】:
我只是从 ANTLR 开始并尝试从日志文件中解析一些模式
例如:日志文件:
7114422 2009-07-16 15:43:07,078 [LOGTHREAD] 信息状态日志 - 任务 0 输入 : uk.project.Evaluation.Input.Function1(selected=["red","yellow"]){}
7114437 2009-07-16 15:43:07,093 [LOGTHREAD] 信息状态日志 - 任务 0 输出 : uk.org.project.Evaluation.Output.Function2(selected=["Rocket"]){}
7114422 2009-07-16 15:43:07,078 [LOGTHREAD] 信息状态日志 - 任务 0 输入 : uk.project.Evaluation.Input.Function3(selected=["blue","yellow"]){}
7114437 2009-07-16 15:43:07,093 [LOGTHREAD] 信息状态日志 - 任务 0 输出 : uk.org.project.Evaluation.Output.Function4(selected=["Speech"]){}
现在我必须解析这个文件,只找到'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';
【问题讨论】:
标签: antlr