-2

I am planning to build a parser, that parse only particular format as below.

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

This is one of the log for the router. I am naive developer. And just encountered ANTLR4 few days back. Using ANTLRWorks2 I checked my grammar. I created lexer and parser - two classes using ANTLR4. From now I don't know what to do. Please guide.

I guess, I have to create another java file which imports parser.java file and in that file I have to write parser logic.

I am attaching grammar file herewith.

/*
 * 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 Hello;

r  : MONTH ID TIME IP X UNAME Y PROTOCOL Z FULLIP SEP FULLIP W FULLIP SEP FULLIP V SEP FULLIP P;         // match keyword hello followed by an identifier
MONTH:[A-Z][a-z]+;
ID : [0-9]+ ;             // match lower-case identifiers
TIME : ID SEPERATOR ID SEPERATOR ID;
SEPERATOR: [:] | [.];
SEP: [-][>] ;
IP: ID SEPERATOR ID SEPERATOR ID SEPERATOR ID ;
FULLIP: IP SEPERATOR ID/*Port*/ ;
X: [a-z]+[,][a-z]+[ ][A-Z]+[ ][a-z]+[:][ ][a-z]+[:][<] ;
UNAME: [a-z]+[-][a-z]+[.][a-z]+[.][0-9]+ ;
Y: [>][ ][a-z]+[:][a-z]+[-][a-z]+[0-9][.][a-z]+[0-9]+[,][ ][a-z]+[ ] ;
PROTOCOL: [A-Z]+ ;
Z: [(][A-Z]+[,][A-Z]+[)][,];
W: [,][ ][A-Z]+[ ][(] ;
V: [)];
P: [,][ ][a-z]+[ ][0-9]+ ;
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines, \r (Windows)
Avid Learner
  • 67
  • 1
  • 6

1 Answers1

0

Get the parse tree from your parser, then traverse it using a visitor.

Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72
  • Thanks. But, my question is, what to do after I get the parser file. Do I need to create another .java file for input and to use parser.class file? And please provide one suitable example of parser file. – Avid Learner Dec 22 '15 at 06:06
  • I do not understand your question. Please have a look at [a recent project I did using ANTLR4](https://bitbucket.org/peturingi/trains/src/eb4434d5fcc0?at=master). RailSystem is the main class in that project. The unit tests might also help you understand this better. Network is one parser, TrainRoute is another. I might be better able to help you if you relate follow up questions to my source code. – Pétur Ingi Egilsson Dec 22 '15 at 06:36