Parsing of blocks (e.g. foo { bar })

From: Tor Houghton (torh+news_at_removethisbitbeforethedot.bogus.net)
Date: 10/30/03


Date: Thu, 30 Oct 2003 12:36:39 +0000

Hi,

I'm wondering if there is a more elegant way of parsing "blocks" of data.

I have a configuration file, thus:

   # comment
   
   some.host.name {
      ; another comment
      /etc/hostname.fxp0
      /etc/hosts
      /etc/mail
   }

And I currently parse this using the following code:

    while(<CONF>) {
       next if(/^\s*(\#|;)|^\s*$/);
       if(m#(\S+)\s+{# .. m#}#) {
          $HOST=$1 if $1;
          if(!/\{|\}/) {
             print "add: $HOST ",$_;
          }
       }
    }

Before I go "all out" and complete this section of the code using the
above technique, I was wondering if there was other (better) ways of doing
this (for one, I'm not sure how to catch out any errors in the file, for
instance:

    some.host.here {
       another.host {
          /usr
          /opt
       }
       /etc
       /var/mail
    }

.. here the current code fails miserably. Mind you, I'm probably the only
one going to use this, so that may not be such a big deal :-> )

Thanks!

Tor



Relevant Pages

  • Re: C parser document
    ... > I'm trying to parse a configuration file, ... > this is the first time I'm trying something like this, ... > C's I/O functions are a mess and hard to use, that is if you want to ... Be more specific about what you're trying to parse and the problems ...
    (comp.lang.c)
  • Kernel device for Toshiba laptop sound
    ... I have just installed 5.2_release on a toshiba A10 satellite. ... well except I can't parse the appropriate device line for the kernel ...
    (comp.unix.bsd.freebsd.misc)
  • Embed a confugeration file inside exeutable
    ... I open a configuration file with fopen and parse its content in my VC 6.0 ... I want to embed it (as a resource or something similar) inside ... Prev by Date: ...
    (microsoft.public.vc.ide_general)
  • Re: Kernel device for Toshiba laptop sound
    ... > well except I can't parse the appropriate device line for the kernel ...
    (comp.unix.bsd.freebsd.misc)
  • global variables in module
    ... I'm writing a GUI with wxPython. ... I want to save preferences and defaults ... values in a configuration file. ... I'm going ot parse this file using ...
    (comp.lang.python)