如何描述EBNF中的作用域?

2024-04-30 04:58:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Grako和Python为Cisco IOS和ASA配置编写一个解析器。我试图找出如何在EBNF中表示“scoped”关键字-例如,“description”关键字必须出现在interface范围内,但是interface有多个选项,它们都是可选的(我相信,顺序可以在设备之间更改):

interface Vlan1435
 nameif untrust
 description the outside interface for customer X
 bridge-group 1
 security-level 0

我发现最接近一个例子的是一个名为Farly的Perl应用程序,它使用Perl Parse::Recdescent模块,它似乎类似于Grako。你知道吗

从这里我得到了这种递归定义:

@@eol_comments :: /!([^\n]*?)\n/
@@whitespace :: /[\t ]+/

start
    =
    file_input $
    ;


file_input
    =
    {NEWLINE | asa_line}
    ;


asa_line
    =
      'names' NEWLINE
    | interface NEWLINE
    ;

interface
    =
    'interface' string NEWLINE interface_options
    ;


interface_options
    =
    if_name | sec_level | if_addr | if_bridgegroup | NEWLINE
    ;


if_bridgegroup
    =
    'bridge-group' digit NEWLINE interface_options
    ;


if_name
    =
    'nameif' string NEWLINE interface_options
    ;


sec_level
    =
    'security-level' digit NEWLINE interface_options
    ;

但是它会产生一个奇怪的嵌套AST,并且它不会“重置”以检测第二个接口或随后配置中的任何其他内容。你知道吗

这些作用域通常是如何在EBNF中定义的? (这类东西也有有用的教程吗?我的googlefu没有为Grako或解析器找到任何东西)


Tags: 解析器ifnewlinegroup关键字descriptiongrakolevel
1条回答
网友
1楼 · 发布于 2024-04-30 04:58:13

我在这些情况下使用的技巧是使用重复,即使选项只能出现一次:

interface_options
    =
    { @+:(if_name | sec_level | if_addr | if_bridgegroup)  NEWLINE}*
    ;

如果需要,可以使用语义操作来验证选项是否不重复。你知道吗

相关问题 更多 >