Current Issue


Table of contents

CD-ROM

Sys Admin and The Perl Journal CD-ROM version 12.0

Version 12.0 delivers every issue of Sys Admin from 1992 through 2006 and every
issue of The Perl Journal from 1996-2002 in one convenient CD-ROM!

Order now!

Sys Admin Magazine > Archives > 2001 > December 2001

Parsing Interesting Things

Randal L. Schwartz

Someone recently popped into one of the newsgroups I frequent and asked how to parse an INI file. You might have seen those before, with sections and keyword=value lines, like:

[login]
timeout=30
remote=yes

[password]
minlength=6

I think they started in the Microsoft world, since no sane UNIX hacker would have come up with something like that. No, we come up with things like .Xdefaults and sendmail.cf and termcap. But the request seemed simple: parse the file and gather the information into a hash for quick access, two levels deep, of course.

Now, I usually carry the banner here for “use the CPAN”, and in fact, there are numerous CPAN modules that parse INI files (too many, I think). But let’s take a different route here. Suppose we were parsing a file that wasn’t already CPANned to death. What tools could we use?

Well, certainly Perl’s regular expressions are pretty powerful in the first place, and this task really wouldn’t be that difficult with hand-written code, but we can go a bit further and pull out a nifty tool from the CPAN: the “madman of Perl” Damian Conway’s Parse::RecDescent. This module permits extremely complex parsers to be built by specifying a nice hierarchical description of the data (as a grammar), and a series of actions to be taken as each portion of the data is returned. I find it very simple to use, and whipped up a parser in no time.

The key to a useful grammar is getting the description right, and what to do once you’ve seen that. First, let’s look at a file. A file is a series of sections, so in the grammar language, that’s given as:

file: sections

Actually, a file is a bit more than that. If we just used that, the grammar would match any prefix of the input that also had sections.




MarketPlace

Flowcharts from C/C++ code -- Free trial download
Understand C/C++ code in less time. A new team member ? Inherited legacy code ? Get up to speed faster with Crystal Flow for C/C++. Code-formatting improves readability. Flowcharts are integrated with code browser. Export flowcharts to Visio.

Automate Software Builds with Visual Build Pro
Easily create an automated, repeatable process for building and deploying software.

WinDev 12 - Powerful IDE
Develop 10 times faster ! ALM, IDE, .Net, RAD, 5GL, Database, 5GL, 64-bit, etc. Free Express version

Web based bug tracking - AdminiTrack.com
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.

Wanna see your ad here?