ANTLR4解析器

时间:2015-12-23 12:21:16

标签: java parsing grammar antlr4 visitor

在Visitor类中写什么?

我们已经为我们的语言编写了一个语法。我们不需要对其执行任何操作。如果语言通过书面语法传递,那么我们只想从中获取一些对象。

将语言作为输入:

Dec 17 14:00:00 103.56.229.11 firewall,info FFFW forward: in:<pppoe-mm.demo.649> out:sfp-sfpplus1.vlan113, proto TCP (ACK,PSH), 10.0.15.245:49831->103.235.46.39:443, NAT (10.0.15.245:49831->202.173.127.253:49831)->103.235.46.39:443, len 250

期望的输出:

Dec, 17, 14:00:00, 103.56.229.11, pppoe-mm.demo.649, TCP, 10.0.15.245:49831, 103.235.46.39:443, 202.173.127.253:49831

我们的语法(文件名:sys.g):(运行良好,我们使用ANTLRWorks2证明了这一点)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

grammar sys;

r: IDENT NUM time ip x+ user xout proto xuser ipfull xtra ipfull xtra1 ipfull xtra ipfull xtra2 ipfull xtra3; 
time: NUM SEP NUM SEP NUM;
ip: NUM USER NUM USER NUM USER NUM ;
ipfull: NUM USER NUM USER NUM USER NUM SEP NUM ;
x: (IDENT | SEP | NUM)+ LTHAN;
user: (IDENT | USER | NUM)+ ;
xuser: (IDENT | SEP | NUM)+ ;
xout: GTHAN IDENT+ SEP IDENT+ USER IDENT+ USER IDENT SEP IDENT;
proto: IDENT ;
xtra: USER GTHAN ;
xtra1: SEP IDENT SEP;
xtra2: SEP xtra;
xtra3: SEP IDENT NUM;

IDENT: ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9')* ;
NUM: ('0'..'9')+ ;
LTHAN: '<' ;
GTHAN: '>' ;
SEP: ':' | ',' | '(' | ')' ;
USER: '-' | '.' ;
WS : (' ' | '\t' | '\r' | '\n')+ -> skip ;

给定语言的生成树:

Generated tree for the language

问题1: 我们使用antlr4.5编译了我们的语法文件,我们也使用了访问者。所以我们的问题是如何在另一个文件中打印特定对象?

问题2: 是否需要创建另一个名为&#34; value&#34;的类。它将值返回给访问者?

EvalVisitor.java文件:

public class EvalVisitor extends sysBaseVisitor{

//
}

我们的主要java文件,即SysLogCheck.java,我们使用我们的语法sys.g文件生成的Lexer(SysLexer.java)和Parser(SysParser.java)。

import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import java.io.*;
import org.antlr.v4.runtime.*;

public class SysLogCheck {
    public static void main(String[] args) throws Exception {
        ANTLRInputStream input = new ANTLRInputStream(new FileInputStream(new File("input.txt")));
        sysLexer lexer = new sysLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        sysParser parser = new sysParser(tokens);
        ParseTree tree = parser.r();
        EvalVisitor visitor = new EvalVisitor();
        visitor.visit(tree);        
    }
}

1 个答案:

答案 0 :(得分:0)

至于你的第一个问题:

以下是原始访问者的示例,其中输出Dec, 17, 14:00:00

在阅读/* do something with the results */的行中,您可以放置​​一些保存结果的代码。

import org.antlr.v4.runtime.tree.ParseTree;

public class EvalVisitor extends sysBaseVisitor{

  class LogEntry {
    String ident1;
    String dayNum;
    String ip;

    /*
    ...
     */
  }

  static LogEntry logEntry;

  @Override
  public Object visit(ParseTree tree) {
    /* Setup logentry used by all visitors (this case, there is only a single visitor...)*/
    logEntry = new LogEntry();

    /* visit */
    final Object o = super.visit(tree);

    /* do something with the results */
    System.out.println(logEntry.ident1 + ", " + logEntry.dayNum + ", " + logEntry.ip);

    return o;
  }

  StringBuilder stringBuilder;
  @Override
  public Object visitR(sysParser.RContext ctx) {
    logEntry.ident1 = ctx.IDENT().getText();
    logEntry.dayNum = ctx.NUM().getText();
    return super.visitR(ctx);
  }

  @Override
  public Object visitTime(sysParser.TimeContext ctx) {
    logEntry.ip = ctx.getText();
    return super.visitTime(ctx);
  }

  @Override
  public Object visitIp(sysParser.IpContext ctx) {
    return super.visitIp(ctx);
  }

  @Override
  public Object visitIpfull(sysParser.IpfullContext ctx) {
    return super.visitIpfull(ctx);
  }

  @Override
  public Object visitX(sysParser.XContext ctx) {
    return super.visitX(ctx);
  }

  @Override
  public Object visitUser(sysParser.UserContext ctx) {
    return super.visitUser(ctx);
  }

  @Override
  public Object visitXuser(sysParser.XuserContext ctx) {
    return super.visitXuser(ctx);
  }

  @Override
  public Object visitXout(sysParser.XoutContext ctx) {
    return super.visitXout(ctx);
  }

  @Override
  public Object visitProto(sysParser.ProtoContext ctx) {
    return super.visitProto(ctx);
  }

  @Override
  public Object visitXtra(sysParser.XtraContext ctx) {
    return super.visitXtra(ctx);
  }

  @Override
  public Object visitXtra1(sysParser.Xtra1Context ctx) {
    return super.visitXtra1(ctx);
  }

  @Override
  public Object visitXtra2(sysParser.Xtra2Context ctx) {
    return super.visitXtra2(ctx);
  }

  @Override
  public Object visitXtra3(sysParser.Xtra3Context ctx) {
    return super.visitXtra3(ctx);
  }
  //
}