防火墙配置解析器无关的输入

时间:2020-05-25 06:48:11

标签: antlr antlr4

我正在尝试为某些防火墙设备编写配置解析器。我是第一次使用ANTLR。

我要解析的通常是以下类型的文本:

CreateMap<Source, Destination>().ConvertUsing<DestinationConverter>();

输入数据是带有配置行的“ config”块。我已经想出了这些规则:

config wireless-controller global
    set name ''
    set location ''
    set max-retransmit 3
    set data-ethernet-II disable
    set link-aggregation disable
    set mesh-eth-type 8755
    set fiapp-eth-type 5252
    set discovery-mc-addr 221.0.4.254
    set max-clients 0
    set rogue-scan-mac-adjacency 6
    set ipsec-base-ip 172.252.0.4
    set wtp-share disable
    set ap-log-server disable
    set ap-log-server-ip 0.0.0.0
    set ap-log-server-port 0
end

我仍然遇到问题,因为antlr似乎不喜欢然后结束要解析的数据的“ end \ n”: 1 │ grammar Fortigate ; 2 │ 3 │ /* 4 │ * Tokens 5 │ */ 6 │ 7 │ WHITESPACE : (' ' | '\t')+ -> skip ; 8 │ NEWLINE : ('\r'? '\n' | '\n' | '\r')+ ; 9 │ WORD : ([a-zA-Z0-9] | '.' | [\-_'"])+ ; 10 │ ENDBLOCK : 'end' ; 11 │ EDITSTART : 'edit' ; 12 │ NEXTEDIT : 'next' ; 13 │ /* 14 │ * Parser rules 15 │ */ 16 │ configline : ('set'|'unset') WORD+ NEWLINE ; 17 │ startconfigblock : 'config' WORD+ NEWLINE ; 18 │ editline : EDITSTART '"'.+?'"' ; 19 │ editblock : editline configline+ NEXTEDIT NEWLINE ; 20 │ configblock : startconfigblock (editblock | configline)+ ENDBLOCK NEWLINE; 21 │ 22 │ startRule : configblock+ ;

但是我的令牌树很干净 antlr4 Fortigate startRule -gui

Antlr不喜欢结尾的“ end”文本,尽管它在line 12:0 extraneous input 'end' expecting {'set', 'unset', 'end', 'edit'}规则中,并且未被其他规则使用...

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

输入end被标记为WORD。这是因为当词法分析器可以为多个规则匹配相同的字符时,第一个定义为“获胜”的规则。解决方法是,将关键字移到WORD规则上方:

ENDBLOCK    : 'end' ;
EDITSTART   : 'edit' ;
NEXTEDIT    : 'next' ;
WORD        : ([a-zA-Z0-9] | '.' | [\-_'"])+ ;

如果您还想将endWORD匹配,请引入如下解析器规则:

word
 : WORD
 | END
 ;

并在解析器规则中使用此word而不是WORD

顺便说一句,([a-zA-Z0-9] | '.' | [\-_'"])+可以重写为[a-zA-Z0-9.\-_'"]+(' ' | '\t')+可以重写为[ \t]+

最后,使用EOF令牌“锚定”解析器的启动规则始终是一个好主意:这样,您将迫使解析器消耗整个令牌流,而不是半途而废。