Friday afternoon blag fest

Ovid publiustemp-londonpm at yahoo.com
Fri Sep 21 16:03:46 BST 2007


--- Dave Hodgkinson <davehodg at gmail.com> wrote:

> > I believe mine is 5214 characters, but it appears to very fast when
>  
> > run
> > against its intended target text.
> 
> There's enough repetition in there that it should be templated and  
> iterated over.

I should have explained the context.  It's actually for AI::Prolog and
the core parser doesn't understand Prolog math.  Rather than extend
(and possibly break) a working parser, I wrote a preprocessor which
turns things like this:

  X \= 9 / (3 + (4+7) % ModValue) + 2 / (3+7).

Into things like this (whitespace added for alleged clarity):

  ne(X, 
    plus(
      div(9, mod(plus(3, plus(4, 7)), ModValue)), 
      div(2, plus(3, 7))
    )
  ).

However, the regex is definitely built programmatically.  The final
regex is built like this:

  my $expression = qr/
    (
        ($simple_math_term)
        \s+
        ($compare)
        \s+
        ($rhs)
    )
    (?=[,.])
  /x;

I would never dream of writing a regex that huge by hand.

http://search.cpan.org/src/JJORE/AI-Prolog-0.739/lib/AI/Prolog/Parser/PreProcessor/Math.pm
http://tinyurl.com/369xzq

Cheers,
Ovid

--
Buy the book  - http://www.oreilly.com/catalog/perlhks/
Perl and CGI  - http://users.easystreet.com/ovid/cgi_course/
Personal blog - http://publius-ovidius.livejournal.com/
Tech blog     - http://use.perl.org/~Ovid/journal/


More information about the london.pm mailing list