# Grammar for Cisco devices
# Configuration is expected to be preprocessed via following command:
# perl -ne '/(^\s*)(\S.*)$/; print length($1)." $2\n"'
#<autotree>
{ # perl code follows
$::res = {};
# Returns pointer to context hashref as specified by ctx stack.
# @param ctx_path array of ctx stack.
sub ctx {
return ctx_rel($::res, @_);
}
# Returns pointer to context hashref as specified by ctx stack,
# relative to the supplied ctx pointer.
# @param ptr current ctx pointer from which ctx build starts.
# @param ctx_path array of ctx stack.
sub ctx_rel {
my $ptr = shift;
my @ctx_path = @_;
foreach my $part (@ctx_path) {
$ptr->{$part} = {} unless exists ($ptr->{$part});
$ptr = $ptr->{$part};
}
return $ptr;
}
} # end of perl code
file: <skip: qr/[^\S\n]*/> # Ignore non-newline whitespace
line(s) eofile
line: s_controller
| s_interface
| s_vrf
| s_vlan
| vtp_line
| indent comment
| indent cmdline
| indent emptyline
| <error>
emptyline: eol
comment: /!.*/ eol
cmdline: l_hostname eol
| l_hash eol
| word(s) eol
word: /[0-9a-zA-Z:\/_\#\"\.,+<>()&{}-]+/
{ $item[1] }
type: /\w+/
{ $item[1] }
num: /\d+/
{ $item[1] }
indent: /\d+/
{ $item[1] }
range: /\d+/"-"/\d+/
{ { from => $item[1], to => $item[3] } }
keyword: /[\w-]+/
{ $item[1] }
identifier: /[0-9a-zA-Z:_-]+/
{ $item[1] }
quoted_text: <perl_quotelike>
{ $item[1][2] }
| /[0-9a-zA-Z:\/_\#\"\.,-]+/
{ $item[1] }
value: /[0-9a-zA-Z:\/_\#\"\.,-]+/
{ $item[1] }
eofile: /^\Z/
eol: /\n/
rest_of_line: word(s)
| eol
# section
section: cmdline(s) "!"
{ print "section\n"; }
l_section: /[1-9]/ word(s) eol
| /[1-9]/ "!" eol
| /[1-9]/ eol
| <error>
# Lines w/ hash (passwd, secret etc)
l_hash: word(s) /\S+/ word(s)
| word(s) /\S+/
# Hostname
l_hostname: "hostname" identifier
{ $::res->{hostname} = $item{identifier} }
# Description
l_description: "description" /[^\n]+/
{ $arg{ctx}->{description} = $item[2] }
# controller section
controller_num: /\d+\/\d+/
{ $item[1] }
s_controller: "0" "controller" type controller_num eol s_controller_l[ctx => ctx('controller', $item{controller_num}) ](s) "0" "!" eol
{ $::res->{controller}->{$item{controller_num}}->{type} = $item{type} }
s_controller_l: "1" s_controller_content[ctx => $arg{ctx}] eol
| l_section
s_controller_content: l_description[ctx => $arg{ctx}]
| "channel-group" num "timeslots" range
{ $arg{ctx}->{"channel-group"}->{$item{num}}->{ts} = $item{range} }
| "channel-group" num "unframed"
{ $arg{ctx}->{"channel-group"}->{$item{num}}->{unframed} = 1 }
| "framing" keyword
{ $arg{ctx}->{framing} = $item{keyword} }
| "au-4" num "tug-3" num eol s_tug_l[ctx => ctx_rel($arg{ctx}, 'au', $item[2], 'tug', $item[4]) ](s)
s_tug_l: "2" "tug-2" num "e1" num tug_content[ctx => ctx_rel($arg{ctx}, $item[3].'/'.$item[5]) ] eol
tug_content: l_description[ctx => $arg{ctx}]
| "unframed"
{ $arg{ctx}->{unframed} = 1 }
| "channel-group" num "timeslots" range
{ $arg{ctx}->{"channel-group"}->{$item{num}}->{ts} = $item{range} }
| word(s)
# Vlan range
vlan_s_range: /\d+/"-"/\d+/
{
for (my $i = $item[1]; $i <= $item[3]; $i++) {
$arg{ctx}->{$arg{key}}->{$i} = 1; }
}
| /\d+/
{ $arg{ctx}->{$arg{key}}->{$item[1]} = 1 }
vlan_range: vlan_s_range","vlan_range
| vlan_s_range
# vlan section
s_vlan: "0" "vlan" num eol "1" "name" identifier eol
{ $::res->{vlans}->{$item{num}}->{name} = $item{identifier} }
# interface section
iface_name: /\w+(-\w+)?\d+[0-9\/\.:]*/
{ $item[1] }
s_interface: "0" "interface" iface_name /\S*/ eol s_interface_l[ctx => ctx("interface", $item{iface_name}) ](s) "0" "!" eol
{
$::res->{interface}->{$item{iface_name}}->{type} = $item[4]
if length($item[4]) > 0
}
ip: /\d+\.\d+\.\d+\.\d+/
{ $item[1] }
s_interface_l: "1" s_interface_content[ctx => $arg{ctx}] eol
| l_section
s_interface_content: l_description[ctx => $arg{ctx}]
| /(no)?/ "shutdown"
{ $arg{ctx}->{shutdown} = ($item[1] eq 'no') ? 0 : 1 }
| "ip" "address" ip ip
{ $arg{ctx}->{ip} = $item[3]; $arg{ctx}->{mask} = $item[4] }
| "encapsulation" keyword /\S*/
{
$arg{ctx}->{encap} = $item[2];
$arg{ctx}->{encap_param} = $item[3] if length($item[3]) > 0
}
| "frame-relay" keyword keyword
{ $arg{ctx}->{"frame-relay"} = {type => $item[2], value => $item[3]} }
| "bandwidth" num
{ $arg{ctx}->{bandwidth} = $item{num} }
| "ip" "vrf" "forwarding" word
{ $arg{ctx}->{"ip-vrf-fwd"} = $item{word} }
| "switchport" "mode" /access|trunk/
{ $arg{ctx}->{"switchport-mode"} = $item[3] }
| "switchport" "access" "vlan" num
{ $arg{ctx}->{vlan} = $item{num} }
| "switchport" "trunk" "encapsulation" keyword
{ $arg{ctx}->{"trunk-encap"} = $item{keyword} }
| "switchport" "trunk" "allowed" "vlan" vlan_range[ctx => $arg{ctx}, key => "vlan"]
| "channel-group" num "mode" keyword
{ $arg{ctx}->{"channel-group"}->{$item{num}}->{mode} = $item{keyword} }
| "ip" "vrf" "forwarding" word
{ $arg{ctx}->{"ip-vrf-fwd"} = $item{word} }
# vrf section
s_vrf: "0" "ip" "vrf" keyword eol s_vrf_l[ctx => ctx("vrf", $item{keyword}) ](s) "0" "!" eol
s_vrf_l: "1" s_vrf_content[ctx => $arg{ctx}] eol
| l_section
rd_val: /\d+:\d+/
{ $item[1] }
s_vrf_content: l_description[ctx => $arg{ctx}]
| "rd" rd_val
{ $arg{ctx}->{rd} = $item{rd_val} }
| "export" "map" keyword
{ $arg{ctx}->{"export-map"} = $item{keyword} }
| "route-target" /export|import/ rd_val
{
$arg{ctx}->{"route-target"}->{$item[2]} = []
unless exists $arg{ctx}->{"route-target"}->{$item[2]};
push @{$arg{ctx}->{"route-target"}->{$item[2]}}, $item{rd_val};
}
# vtp
vtp_line: "0" "vtp" /mode|domain/ identifier eol
{ $::res->{vtp}->{$item[3]} = $item{identifier} }