Reputation: 23
I'm writing a parser for firewall configuration file. I am new to PyParsing and Python in general.
The question is how do i parse if more then 3 arguments occur, (xxxx,xxxx,xxxx) != (xxxx,xxxx,xxxx,xxxx), All rules are working fine and parse everything correctly if each line contain no more that 3 strings, but we can see that [Firewall [F1]] contains "NAT" after address field, and is ignored no matter how we change the rule.
Using (def printTokens(s,loc,toks): #s=orig string, loc=location, toks=matched tokens)
Please see the 2 outputs when using 4th argument ("NAT") and when we erase it. Thanks in advance!Need to parse everything including "NAT" with rules implemented.
from pyparsing import *
#===================================GRAMMER==================================
zone = Literal("zone")
zoneid = Word(alphanums)
host = Literal("host")
hostid = Word(alphanums)
interface = Literal("interface")
interfaceid = Word(alphanums)
firewall = Literal("firewall")
firewallid = Word(alphanums)
router = Literal("router")
routerid = Word(alphanums)
fstop = Literal(".")
comma = Suppress(",") #Converter for ignoring the results of a parsed expression.
slash = Literal("/")
ocbracket = Literal("{")
ccbracket = Literal("}")
sobracket = Literal("[")
scbracket = Literal("]")
hyphen = Literal("-")
underline = Literal("_")
word = Word(alphas)
#===================================IP-TYPE=================================
ip=Combine(Word(nums)+
fstop+ Word(nums) +
fstop+ Word(nums) +
fstop + Word(nums))
subnet = Combine(slash +Word(nums))
address = ip + Optional(subnet)
#===================================RULES===================================
#adword = address + word
zoneRule = zone + zoneid + address
hostRule = host + hostid + ocbracket
interfaceRule = interface + interfaceid + address
interfaceRule2 = interface + interfaceid + address + word
firewallRule = firewall + firewallid + ocbracket
routerRule = router + routerid + ocbracket
endRule = ccbracket
rule = zoneRule | hostRule | interfaceRule | interfaceRule2 | firewallRule | routerRule | endRule
rules = OneOrMore(rule)
#===================================DATA=====================================
details = """zone zone1 10.1.0.0/24
zone backbone 10.254.0.0/24
zone zone 10.2.0.0/24
host ha {
interface iha 10.1.0.1
}
host hb {
interface ihb 10.2.0.1
}
firewall f1 {
interface ifla 10.1.0.254
interface iflback 10.254.0.101 nat
}
router r2 {
interface ir2back 10.254.0.102
}
router r3 {
interface ir3b 10.2.0.103
}"""
#==================================METHODS==================================
def printTokens(s,loc,toks): #s=orig string, loc=location, toks=matched tokens
print (toks)
zoneRule.setParseAction(printTokens)
hostRule.setParseAction(printTokens)
interfaceRule.setParseAction(printTokens)
interfaceRule2.setParseAction(printTokens) #takes in 4 instances where as 3 declared
firewallRule.setParseAction(printTokens)
routerRule.setParseAction(printTokens)
endRule.setParseAction(printTokens)
rules.parseString(details)
#================================OUTPUT RESULT WITH NAT=================================
"""
['zone', 'zone1', '10.1.0.0', '/24']
['zone', 'backbone', '10.254.0.0', '/24']
['zone', 'zone', '10.2.0.0', '/24']
['host', 'ha', '{']
['interface', 'iha', '10.1.0.1']
['}']
['host', 'hb', '{']
['interface', 'ihb', '10.2.0.1']
['}']
['firewall', 'f1', '{']
['interface', 'ifla', '10.1.0.254']
['interface', 'iflback', '10.254.0.101']"""
#================================OUTPUT RESULT WITHOUT NAT=================================
"""['zone', 'zone1', '10.1.0.0', '/24']
['zone', 'backbone', '10.254.0.0', '/24']
['zone', 'zone', '10.2.0.0', '/24']
['host', 'ha', '{']
['interface', 'iha', '10.1.0.1']
['}']
['host', 'hb', '{']
['interface', 'ihb', '10.2.0.1']
['}']
['firewall', 'f1', '{']
['interface', 'ifla', '10.1.0.254']
['interface', 'iflback', '10.254.0.101']
['}']
['router', 'r2', '{']
['interface', 'ir2back', '10.254.0.102']
['}']
['router', 'r3', '{']
['interface', 'ir3b', '10.2.0.103']
['}']"""
Upvotes: 2
Views: 186
Reputation: 8721
If you want to match any number of expressions with a certain delimiter, use PyParsing's delimitedList. By default it allows whitespace around the delimiters; add combine=True
to require no whitespace.
However, if you want to allow optional items in your grammar, you should just add an optional item. For your interface rules, you can replace:
interfaceRule = interface + interfaceid + address
interfaceRule2 = interface + interfaceid + address + word
With:
interfaceRule = interface + interfaceid + address + Optional(word)
Finally, the actual issue with the code you posted is that you are using the |
operator, which is a short-hand form for MatchFirst. MatchFirst will try the given options in order, and return the result of the first one which matches. If you use Or instead, for which the short-hand form is the ^
operator, then it will instead try all of the options and return the one with the longest match.
Upvotes: 2