" /> " /> "/>

Parse double quoted string using MARPA :: R2 perl

145 Views Asked by At

I'm implementing a parser using MARPA::R2.

I have a G1 rule like :

PARAM ::= STRING | REGEX_STRING

and L0 rule like:

STRING                     ~ [^ \/\(\),&:\"~]+   -----> works fine
REGEX_STRING               ~ [\"([^:]*?)\"] -----> doesn't work

Using REGEX_STRING, I'm trying to parse strings enclosed in double quotes, but something is wrong with the regex. Also, I want to remove the double quotes and only keep the content between quotes.

So, if i give input using below code :

my $recce = Marpa::R2::Scanless::R->new({grammar => $grammar});
my $input = "\"foo\"";  --> here, it should parse "foo" and give me foo.
print "Trying to parse:\n$input\n\n";
$recce->read(\$input);
my $value_ref = ${$recce->value};
print "Output:\n".Dumper($value_ref);

Other examples : "bar123", "foo(123)" etc.

1

There are 1 best solutions below

2
daxim On BEST ANSWER
use 5.026;
use strictures;
use Data::Dumper qw(Dumper);
use Marpa::R2 qw();

my $grammar = Marpa::R2::Scanless::G->new({
    bless_package => 'parsetree',
    source        => \<<'',
:default ::= action => [values] bless => ::lhs
lexeme default = action => [ start, length, value ] bless => ::name latm => 1
:start ::= expression
expression ::= funcname params
params ::= epsilon | lparen param rparen
epsilon ::=
funcname ~ [a-z0-9]+
lparen ~ '('
param ::= unquotedparam | quotedparam
unquotedparam ::= [a-z0-9]+
quotedparam ::= '"' stringliteral '"'
stringliteral ~ [^"]+
rparen ~ ')'

});
say $grammar->show_rules;

for my $input (qw[
    func("foo")
    bar123
    foo(123)
]) {
    my $r = Marpa::R2::Scanless::R->new({
        grammar => $grammar,
        trace_terminals => 1
    });
    $r->read(\$input);
    say Dumper $r->value;
}