Afsarnamak
Afsarnamak

Reputation: 3

how to write ANTLR grammar for parsing plain text file

am very new to this ANTLR tool, Need help on writing grammar rules in ANTRL, for converting/parsing plaint text to equivalent .xml file, using java. please any one help me on this.

i tried as below as per my understanding, and it works for single line(parser) not for full configList(parser)

ANTLR grammar rules below is my grammar .g4

grammar MyTest;

acl : 'acl number' INT configList ('#' configList)* ;
configList  :  config ('\n' config)*;

config : line ('\n' line)* ;

line : line WORD INT (WORD)+ ((SOURCE_LOW_IP)* |(WORD)* |(SOURCE_LOW_IP)*)+
        |WORD INT (WORD)+
;  

fragment
DIGIT   :   ('0'..'9');
INT :   [0-9]+ ;             // Define token INT as one or more digits
//WORD :  [A-Za-z][A-Za-z_\-]* ;
WORD : [A-Za-z][A-Za-z_\-]* ;
NEWLINE:'\r'? '\n' ; // return newlines to parser (is end-statement signal)
WS : [ \t\r\n]+ -> skip ; // toss out whitespace

SOURCE_LOW_IP : INT '.' INT '.' INT '.' INT ; // match IPs in parser

sample input of config list:


acl number 3001

rule 0 permit ip source any rule 1 permit ip source 172.16.10.1 # rule 2 permit ip source 172.16.10.2 0.0.0.255 rule 3 deny destination any rule 4 deny destination 172.16.10.4 rule 5 deny destination 172.16.10.5 0.0.0.255 # rule 6 permit ip source any destination 172.16.10.6 0.0.0.255 rule 7 permit ip source 172.16.10.7 0.0.0.255 destination 172.16.11.7 #

expected for output format as below( this will be taken care using java once antlr generates .java and other files)

 <filterRuleLists>
            <filterRuleList id='3001'>
              <filterRule action='ALLOW' protocol='ANY'>
                <sourceIPRange low='0.0.0.0' high='255.255.255.255' />
                <destinationIPRange low='0.0.0.0' high='255.255.255.255' />
                <fileLine file='config' startLine='4' stopLine='4' />
              </filterRule>
              <filterRule action='ALLOW' protocol='ANY'>
                <sourceIPRange low='172.16.10.1' high='172.16.10.1' />
                <destinationIPRange low='0.0.0.0' high='255.255.255.255' />
                <fileLine file='config' startLine='5' stopLine='5' />
              </filterRule>
        </filterRuleList>
    </filterRuleLists> 

Upvotes: 0

Views: 696

Answers (1)

Kevin Anderson
Kevin Anderson

Reputation: 4592

I am familiar with parser generators, but not ANTLR4 specifically, so this is a best-guess: I strongly suspect the grammar rules

configList : config ('\n' config)*;
config : line ('\n' line)* ;

should be rewritten as

configList : config (NEWLINE config)*;
config : line (NEWLINE line)* ;

as the fragment rule

NEWLINE:'\r'? '\n' ; // return newlines to parser (is end-statement signal)

will cause any '\n' characters to be processed into NEWLINE tokens.

Upvotes: 2

Related Questions