使用 MARPA :: R2 perl 解析双引号字符串

Parse double quoted string using MARPA :: R2 perl

我正在使用 MARPA::R2 实现解析器。

我有一个 G1 rule 喜欢 :

PARAM ::= STRING | REGEX_STRING

L0 rule喜欢:

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

使用 REGEX_STRING,我试图解析用双引号引起来的字符串,但正则表达式有问题。另外,我想去掉双引号,只保留引号之间的内容。

因此,如果我使用以下代码提供输入:

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);

其他示例:"bar123"、"foo(123)" 等

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;
}