Named.conf logical blocks
Phil Mayers
p.mayers at imperial.ac.uk
Tue Jun 28 18:58:55 UTC 2011
On 06/28/2011 05:53 PM, Stefan Certic wrote:
> Hi Guys,
>
> Does anyone have a sample grammar for pharsing named.conf into a data
> structure? Perl or PHP are preffered, but anything would be fine just to get a
> clear picture about grammar and logical blocks.
The only think I ever wrote was a quick python hack that used a big
regexp and an "eat" block function. Maybe it'll be of some interest, but
beware - regexp engines are not parsers, as jwz once famously pointed
out: "You have a problem, and you decide to solve it with regexps. Now
you have two problems." ;o)
Anyway, break the regexp up and it's fairly obvious what it's doing. You
can get better results with a proper state-machine based parser.
#!/usr/bin/python
import re
import sys
import pprint
token_re =
re.compile(r'/\*.*\*/|//[^\n]*\n|#[^\n]*\n|;|{|}|\d+\.\d+\.\d+\.\d+|[-a-zA-Z0-9]+|"[^"]+"')
def eat(toklist):
all = []
v = []
while toklist:
t = toklist.pop(0)
if t=='{':
inner = eat(toklist)
v.append(inner)
elif t=='}' and toklist and toklist[0]==';':
break
elif t==';':
all.append(v)
v = []
else:
v.append(t)
return all
data = sys.stdin.read()
tokens = token_re.findall(data)
pprint.pprint(
eat(tokens)
)
More information about the bind-users
mailing list