我正在使用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$inputnn";
$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;
}