Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

is Parse::RecDescent good for this job?

by Beatnik (Parson)
on May 28, 2002 at 13:03 UTC ( [id://169753]=perlquestion: print w/replies, xml ) Need Help??

Beatnik has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently playing with Parse::RecDescent and it makes some sense but not yet entirely. My current goal to parse something like :
Drive Section Directory SubSection Setting Setting Hardware Section Video SubSection Setting Setting
Now assume there are a certain number of sections (all named, some optional), each section has specific subsections (some optional, some arent, all named), each subsection has different settings (depending on the subsection and above section). When Sections (and subsections) are used, they must appear in a certain order. Now I tried writing basic grammar for the settings, but is Parse::RecDescent oki for the job for the entire structure (I guess it is)? I've been thinking about some kind of separate grammar for each section and each subsection but I'm not sure this is the way to go. Another way I thought of was to use basic grammar for the Sections and use RE for the extended bits like subsections and settings.

Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.

Replies are listed 'Best First'.
Re: is Parse::RecDescent good for this job?
by Abigail-II (Bishop) on May 28, 2002 at 13:24 UTC
    Parse::RecDescent can easily do this, but it might be overkill. The way you describe the structure doesn't show any recursion, and that's of course one of the main features of Parse::RecDescent.

    It will entirely depend on your needs. With Parse::RecDescent, you don't have to bother too much with the actual parsing, all you need to do is write the grammar, and focus on the action.

    But you pay a price. Parse::RecDescent isn't very fast. It may be fast enough for your needs, but if it isn't, or you think that learning the grammar of Parse::RecDescent isn't worth the hassle, you can whip up your own parser - with such a trivial structure, that shouldn't be too hard.

    So, the answer is, "yes, Parse::RecDescent can certainly do that, but do you really need such a heavy tool?".

    Abigail

      Thanks... I'll cook up a RE then :)
      BTW I attented your introduction to Recursive Descent Parsing talk on YAPC::2.00.1 in Amsterdam (and have the booklet to prove it). It was way over my head back then and I must confess it still is a bit :)

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
Re: is Parse::RecDescent good for this job?
by joealba (Hermit) on May 28, 2002 at 15:35 UTC
    This perl.com page has a pretty darn good tutorial on Parse::RecDescent. That should help you out.

    As Abigail-II says, Parse::RecDescent is probably overkill. But, I love to use little projects like this to learn these powerful modules. So, give the tutorial a shot!
      I think I checked that tutorial already :) also re-read Abigails booklet about 4 times, checked the FAQ a few times, read the mailing list and analyzed a few modules... I still have a hard time grasping it all :) Maybe it's just because Parse::RecDescent is quite complex :)

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
Re: is Parse::RecDescent good for this job?
by Anonymous Monk on May 29, 2002 at 03:45 UTC
    use App::Config

    It is designed for exactly this type of problem.   RecDescent will do the job but it is using the proverbial sledgehammer to crack a peanut.

Re: is Parse::RecDescent good for this job?
by meonkeys (Chaplain) on May 29, 2002 at 09:04 UTC
    Based on your question, it sounds like you're interested in using Parse::RecDescent for the fun and challenge of it (this is why I used the module for a simple log parser some time ago; sorry if I'm looking at your world through my own paradigms inappropriately). It was difficult and tricky to write, but definitely fun and worth it just for the academic challenge. Assuming you're in the same situation...

    Do it! It will be fun when it works. There is some jucy info and examples that would help you in "Data Munging with Perl" by Damian Conway, including an awesome step-by-step guide to Parse::RecDescent. I think the trickiest part of Parse::RecDescent--and the part worth really focusing on--is returning a good data structure from your parsing.

    If you would be so kind as to post more specific data to parse and some code examples you've worked out, I would be happy to try and help you write a Parse::RecDescent grammar.

    Update: Thank you Abigail-II... "Data Munging with Perl" was most definitely written by David Cross. Damian does get credit for the forward of this book, though.

    ---
    "A Jedi uses the Force for knowledge and defense, never for attack."
      There is some jucy info and examples that would help you in "Data Munging with Perl" by Damian Conway
      Just to make sure people don't get the wrong idea, "Data Munging with Perl" was written by David Cross. http://www.manning.com/cross/index.html

      Abigail

      i've a similar problem: here is the file i want to parse ================ @modname abc = 'A Business Company' @modinfo someinfo here... @modinfo note specialnote-lookup-from-other-file @fields 1 = 1 int 2 = 2-5 string 3 = 6 char @filename path/abc.csv @variable abc 3 text information about the 3rd field ================ As you can see, the file contain many 'sections' and each of them has format that are different. the only thing they have in common is the use of '@' as the section separator. please help Roy

        Please surround code in your post with <code></code> tags.

        ---
        "A Jedi uses the Force for knowledge and defense, never for attack."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://169753]
Approved by broquaint
Front-paged by RhetTbull
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-24 04:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found