Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Backus Naur Form

by skyknight (Hermit)
on Apr 26, 2005 at 23:12 UTC ( [id://451797]=perlquestion: print w/replies, xml ) Need Help??

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

Is anyone aware of a good Perl module for parsing a file of BNF rules and returning them in a nice structure? A quick search of CPAN resulted in no eponymously named packages. Surely this is a problem that myriad Perl programmers have solved previously. Why isn't there some obviously named module on CPAN? Am I blind, or do I need to write one? I'm all for contributing, but I'd rather not wantonly reinvent the wheel. In any case, I'm surprised that something like Parse::BNF doesn't already exist.

Replies are listed 'Best First'.
Re: Backus Naur Form
by kvale (Monsignor) on Apr 26, 2005 at 23:19 UTC
    If you have a grammar for your BNF rules, you could use Parse::RecDescent to parse the BNF rules for you. Decide on a 'nice structure' that you would like to compile the BNF rules into, and write Parse::RecDescent rules to create that structure as Parse::RecDescent parses your BNF rules.

    -Mark

      P::RD (Parse::RecDescent) uses BNF to parse text. It doesn't parse BNF. If I understood the OP correctly, he wants to parse the BNF notation itself, i.e. given a file with a BNF description, create a parse tree from it.

      Naturally, you can use P::RD it to either:

      1. Look into its source and see how it parses the BNF.
      2. Write a grammar for BNF in it
      I recommend the second technique, because it's (1) very simple, the BNF notation is trivial. (2) Will let one understand BNF as the rule descriptions of P::RD are written in it.
        P::RD doesnt *quite* use BNF, in fact its kind of a slightly mutated *Extended*BNF (EBNF). BNF itself is a lot simpler.

        C.

Re: Backus Naur Form
by castaway (Parson) on Apr 27, 2005 at 09:11 UTC
    I spent quite a while also looking for a BNF parser, and turned up a couple not on CPAN (eg this one) .. But I don't remember if I got around to actually trying it out.

    I also found it strange that theres none on CPAN, I guess people don't do this too often.. (I was looking to parse the SQL92 spec, which is freely available in BNF form..)

    In the end though, I acquired the SQL1999 spec in pdf form, and hand parsed the interesting bits..

    C.

      Well, in that case, perhaps it's worth writing one. Er, maybe... I wonder if anyone other than the two of us would use it. :-)
        I would! I thought of writing a (E)BNF to P::RD parser in P::RD, but never quite got round to it. It would certainly be useful, as you can dig out BNFs for lots of languages that don't necessarily have P::RD-based parsers on CPAN. It would also make P::RD parsers hand-converted from changeable BNFs much easier to maintain. I would think it'd be quite simple to do this if you can write a meta-grammar for BNF in P::RD. Maybe it's so simple that it's been done several times already, but never made its way onto CPAN...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-16 12:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found