Grammars in Perl 5.10

So inspired by both this post by moritz and the awesome Regexp::Grammars module on CPAN, I created a Perl 5.10 version of the perl 6 code (also using GraphViz::Data::Grapher).  There are a few differences, such as naming the top rule, the '' element that Regexp::Grammars creates, and the output format which comes from GraphViz::Data::Grapher (I am outputting to png since GraphViz's as_svg [well dot's SVG output, technically] outputs the wrong size for the outer svg tag).

use strict;
use warnings;
use 5.010;

use Regexp::Grammars;
use Data::Dumper;
use GraphViz;
use GraphViz::Data::Grapher;

my %fruit = map { $_ => '' } qw(apple banana);
my %currency = map { $_ => '' } qw($ USD dollar EUR );

my $x = 'just 20,000 dollar per apple';

my $parser = qr{
    <rule: price_description>
    <value> <currency> per <fruit>
    <rule: currency> <%currency>
    <rule: fruit> <%fruit>
    <rule: value> <[number]> ** ( , )
    <rule: number> \d+

$x =~ $parser;
my $g = GraphViz::Data::Grapher->new(\%/);
print $g->as_png;


About this Entry

This page contains a single entry by jamesw published on August 22, 2009 9:23 AM.

Bugzilla? was the previous entry in this blog.

a less magical with block. is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.