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

Since Perl is an excellent language for parsing data of almost any kind, has any body used it for more intense purposes like writing compilers and interpreters. I am not speaking of languages like Forth, but rather more serious production scale compilers for languages like Java, Scala etc.

Are there any parser generators in Perl? One example that I found online was Larry Wall's STD.pm .

If such work has been done can somebody provide pointers to it. Are there any books/Resources which show how one can develop Compilers and Interpreters using Perl?

Replies are listed 'Best First'.
Re: Core computer science in Perl
by almut (Canon) on Apr 16, 2010 at 08:26 UTC
Re: Core computer science in Perl
by cdarke (Prior) on Apr 16, 2010 at 08:42 UTC
    Are there any books/Resources

    On my bookshelf: "Higher Order Perl" by Mark Jason Dominus has several chapters of interest. "Advanced Perl Programming" by Simon Cozens has a chapter on Parsing Techniques.
Re: Core computer science in Perl
by stvn (Monsignor) on Apr 17, 2010 at 01:12 UTC
    Since Perl is an excellent language for parsing data of almost any kind, has any body used it for more intense purposes like writing compilers and interpreters. I am not speaking of languages like Forth, but rather more serious production scale compilers for languages like Java, Scala etc

    While Perl is good at parsing, it is not really terribly well suited for compiler writing, which really requires a completely different set of features. Languages like Haskell and ML are really nice and easy to write compilers/interpreters in, and in general if I were to write a compiler I would opt for a strongly typed language like Haskell, ML, Java, etc. instead of a dynamic language like Perl.

    Are there any books/Resources which show how one can develop Compilers and Interpreters using Perl?

    Well, there is an online book about writing a Schema interpreter in Perl and I have a mostly complete Scheme-ish Lambda Calculus interpreter on github. There is also a Javascript Interpreter on CPAN which (according the README) is alpha quality but works. If you search for "Interpreter" or "Language" on CPAN a dozen or so other ones come up for languages such as Piet, Befunge, Prolog and a subset of OCaml called MiniCaml, to name a few.

    -stvn
Re: Core computer science in Perl
by Jeffrey Kegler (Hermit) on Apr 17, 2010 at 02:35 UTC

    Perhaps you might also want to look at my new parser generator, Marpa. Marpa is based on recent improvements to Earley's algorithm and is a general parser generator. It will parse from any grammar that you can write as BNF, without restriction. It handles ambiguity, left-recursion, right-recursion, you name it.

    Speeds seem quite acceptable in practical instances. I've written an HTML parser based on Marpa, Marpa::HTML, which is also on CPAN. That distribution contains an HTML pretty-printing utility, which I find useful in my own work.

    General BNF parsing has been neglected over the recent past and, despite signs it can be quite practical, has not resulted in any tools in widespread use. I hope Marpa will make BNF useable in the same way that regular expressions are now.

Re: Core computer science in Perl
by ambrus (Abbot) on Apr 16, 2010 at 16:59 UTC