I am trying to create an iptables parser using ANTLR4.
I wrote the grammar file and there were 5 errors that I can't resolve that are related to the parser rules. The input for running this grammar file is antlr4 -Dlanguage=Cpp iptables.g4
.
The errors are shown below:
error(50): iptables.g4::: syntax error: mismatched character ';' expecting '{'
error(50): iptables.g4:27:0: syntax error: unterminated rule (missing ';') detected at 'status_line :' while looking for rule element
error(50): iptables.g4::: syntax error: mismatched character ':' expecting '{'
error(50): iptables.g4:41:4: syntax error: mismatched input '|' expecting COLON while matching a lexer rule
error(50): iptables.g4:41:14: syntax error: '+' came as a complete surprise to me while matching rule preamble
The grammar file that I have written for the parser rules:
grammar iptables;
//Parser rules
parse
: rule+ EOF
;
rule
: chain_rule
| policy_rule
| append_rule
| status_line
;
chain_rule
: 'iptables' '-N' chain_name
;
policy_rule
: 'iptables' '-P' chain_name policy
;
append_rule
: 'iptables' '-A' chain_name options
;
status_line
: 'Chain' chain_name '(' 'policy' policy PACKET_COUNT 'packets,' BYTE_COUNT 'bytes)'
;
chain_name
: 'INPUT' | 'FORWARD' | 'OUTPUT' | CUSTOM_CHAIN
;
policy
: 'ACCEPT' | 'DROP' | 'QUEUE' | 'RETURN'
;
options_
: DEFAULT
| option+
;
option
: '-s' LOCATION
| '-d' LOCATION
| '-p' PROTOCOL
| '--dport' PORT
| '--sport' PORT
| '-j' policy
;
//Lexer Rules
fragment OCTET
: '0'
| [1-9] [0-9]?
| '1' [0-9] [0-9]?
| '2' [0-4] [0-9]?
| '25' [0-5]
;
IP_ADDRESS
: OCTET '.' OCTET '.' OCTET '.' OCTET
;
PORT
: [0-9]+
;
PROTOCOL
: TCP
| UDP
| ICMP
;
TCP
: 'tcp'
;
UDP
: 'udp'
;
ICMP
: 'icmp'
;
PACKET_COUNT
: [0-9]+
;
BYTE_COUNT
: [0-9]+
;
LOCATION
: IP_ADDRESS
| 'anywhere'
;
DEFAULT
: '--'
;
CUSTOM_CHAIN
: [a-zA-Z_][a-zA-Z0-9_]*
;
WS
: [ \t\r\n]+ -> skip
;
options
is a reserved word in ANTLR. Change:
options
: DEFAULT
| option+
;
to:
options_
: DEFAULT
| option+
;
(or something other than options
)