I've pasted an example that passes your test cases <http://scsys.co.uk:8002/391780>. It's just one of the several possible approaches. This one works by moving the expressions down into the lexer, and searching for them as lexemes.

I've thought, and written in my blog, a lot on the topic of Marpa-powered searches. But it was long ago and at that point there wasn't much interest, so I moved on.

-- jeffrey

On 06/07/2014 10:48 PM, Steven Haryanto wrote:
Hi all,

I wonder if it's feasible to use Marpa, like regular expression, to detect some pattern inside a string. An example of what I'm trying to do is to extract some numeric expression from these strings:

"1+2"
"This is an expression: 1+2, and this is another 1+2+4"
"1+2 is the expression"

I want to recognize and extract 1+2 and 1+2+4 from the above. Here's my current (and failing) attempt:

---
use MarpaX::Simple qw(gen_parser);

my $p = gen_parser(
    grammar => <<'_',
lexeme default  = latm => 1
:default      ::= action=>::first
:start        ::= answer

answer        ::= expr
                | expr any
                | any expr            action=>get1
                | any expr any        action=>get1

expr          ::= num
                | expr '+' expr

num             ~ [\d]+

any             ~ [\d\D]+

:discard        ~ ws
ws              ~ [\s]+
_
    trace_terminals => 1,
    trace_values => 1,
    actions => {
        get0 => sub { $_[1] },
        get1 => sub { $_[2] },
    },
);

sub check { say "Input: $_[0]"; say "Output: ", $p->($_[0]); say "=" x 20 }

check('1');
check('1 + 2');
check('1 + 2 is the expression');
check('This is an expression: 1 + 2 and another 1+2+4');
--

Regards,
Steven
--
You received this message because you are subscribed to the Google Groups "marpa parser" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected] <mailto:[email protected]>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "marpa 
parser" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to