Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Parsing Perl in Perl

by awwaiid (Friar)
on Jun 14, 2000 at 08:16 UTC ( #18044=perlquestion: print w/replies, xml ) Need Help??

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

Can anyone direct me to information about creating a syntax highlighter/formatter/parser for perl code (written in perl)? There are several HTML parsers, and general purpose parsers, but I was hoping for something specific to perl.

Or, better yet, something that can parse it to a tree form?

Replies are listed 'Best First'.
Re: Parsing Perl in Perl
by lhoward (Vicar) on Jun 14, 2000 at 15:43 UTC
    Parse::RecDesc is a framework for building a general purpose, recursive-descent parser. If you can come up with a grammar for the language you want to parse/highlight you could use it as the basis for a highliter/formatter.
      He said he wants a Perl code pretty-printer. There was one submitted as a paper to the Perl Conference a year or two ago, but it never was released as a final product.

      Update: Apache::PrettyPerl is a mod_perl module to turn Perl code into syntax-highlighted HTML on the fly. It might be worth a look, too.

      I've seen hacks that use the C formatter indent, but nothing's done a great job with it. The problem is that Perl is so bloody flexible, it's possible to write code that doesn't look like code to most pretty-printers.

      The a2ps program claims to handle Perl, but I don't know how well.

      There are two in-Perl solutions. The first is the B::DeParse module, which takes the compiled form of your program and spits it back out as Perl code. This takes some guesswork, though, as your program is seen after Perl has optimized it. The module is still under development, so feel free to fix any bugs you find :-)

      The second solution is to write your own. Good luck! A good place to start might be the Text::Balanced module. It's not as heavyweight as Parse::RecDescent. Perl is a complicated beast, but you might be able to do rudimentary parsing using either module.

      Let us know how the search goes. I'm sure many others would like a Perl pretty-printer.

        *Sigh*. Okay. After a bit of research looks like I'm going for your second solution. Apache::PrettyPerl is pretty nice and could be converted for a general perl syntax highlighter... but as you said thats not what I want.

        So... I'm going to write a module that parses perl into a tree of some kind, and then I can do all kinds of stuff with that. Including of course a pretty-printer. If any of you have any comments on that plan... I'd be glad to hear them!

        --Brock (

Re: Parsing Perl in Perl
by btrott (Parson) on Jun 14, 2000 at 09:07 UTC
    Check out some of the B:: modules. Perhaps you can find something in there to aid you along.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://18044]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2021-10-17 21:52 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (72 votes). Check out past polls.