Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Saturday June 18, 2005
08:38 PM

The initial math parser

[ #25268 ]

As part of my plan to create a macro system for Prolog (think "Prolog source filter"), I've finally managed to create a regex that will match complicated Prolog math functions. Don't print out the final regex. Your eyes will hurt.

#!/usr/bin/perl

use warnings;
use strict;
use Regexp::Common;

my $var              = qr/[[:upper:]][[:alnum:]_]*/;
my $num              = $RE{num}{real};
my $anon             = '_';
my $simple_math_term = qr/(?:$num|$var|$anon)/;
my $op               = qr{[-+*/%]};
my $compare          = qr/(?:=|is|[<>]=?)/;
my $lp               = qr/\(/;
my $rp               = qr/\)/;

my $simple_rhs = qr/
    $simple_math_term
    \s*
    (?:
        $op
        \s*
        $simple_math_term
        \s*
    )*
/x;
my $simple_group_term = qr/$lp\s*$simple_rhs\s*$rp/;

my $math_term = qr/(?:$simple_math_term|$simple_group_term)/;
my $complex_rhs = qr/
    $math_term
    \s*
    (?:
        $op
        \s*
        $math_term
        \s*
    )*
/x;
my $complex_group_term = qr/$lp\s*$complex_rhs\s*$rp/;
my $final_math_term = qr/(?:$math_term|$complex_group_term)/;
my $rhs = qr/
    $final_math_term
    \s*
    (?:
        $op
        \s*
        $final_math_term
        \s*
    )*
/x;

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

# FrobNitz is 9 / (3 + (4+7) % ModValue) + 2 / (3+7).

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.