Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define a parsing grammar in Perl 5?

Tags:

parsing

perl

raku

I recently decided to look at Perl, and when faced with Perl 5 vs 6, I went with 5 due to its maturity and vast support.

However, one of the features of Perl 6 which really intrigued me was the grammar keyword, allowing one to define a language grammar which can be used to parse complex text. In other languages either an external library or a manual parsing implementation is usually needed for this.

Now that I've settled on Perl 5, is there any rough equivalent to Perl 6's grammar keyword in Perl 5 (or other functionality for building grammars), built-in or otherwise?

like image 797
Aaron Christiansen Avatar asked Nov 11 '16 17:11

Aaron Christiansen


2 Answers

Though I haven't used it much myself, I believe that the Acmeist software Pegex is close to Perl 6 Grammars.

like image 199
Tommy Stanton Avatar answered Nov 06 '22 09:11

Tommy Stanton


There's no core support, but you can have a look at Parse::RecDescent, Marpa::R2, Regexp::Grammars, or for simpler tasks, just use (DEFINE) in a regex (see perlre).

like image 42
choroba Avatar answered Nov 06 '22 11:11

choroba