1
我想使用perl正则表达式去除函数中的外括号,但是我不能构造不干扰内括号的正则表达式。这里有一个例子:Perl删除外括号
void init(){
if(true){
//do something
}
}
到
void init()
if(true){
//do something
}
是有一个正则表达式可以做到这一点?
我想使用perl正则表达式去除函数中的外括号,但是我不能构造不干扰内括号的正则表达式。这里有一个例子:Perl删除外括号
void init(){
if(true){
//do something
}
}
到
void init()
if(true){
//do something
}
是有一个正则表达式可以做到这一点?
这就是:
my $s = "void init(){ if(true){ //do something }}";
$s =~ s/^([^{]+)\{(.*)\}([^{]*)$/$1$2$3/s;
print "$s\n";
写的语言解析器。下面是使用Marpa::R2一个简单的例子:
#!/usr/bin/perl
use warnings;
use strict;
use Marpa::R2;
my $input = << '__IN__';
void init(){
if(true){
//do something
}
}
__IN__
my $dsl = << '__DSL__';
:default ::= action => concat
lexeme default = latm => 1
FuncDef ::= type name Arglist ('{') Body ('}')
Arglist ::= '(' Args ')'
Args ::= Arg* separator => comma
Arg ::= type name
Body ::= Block+
Block ::= nonbrace
| '{' nonbrace '}'
nonbrace ~ [^{}]*
comma ~ ','
type ~ 'void'
name ~ [\w]+
space ~ [\s]+
:discard ~ space
__DSL__
sub concat { shift; join ' ', @_ }
my $grammar = 'Marpa::R2::Scanless::G'->new({ source => \$dsl });
my $value = $grammar->parse(\$input, { semantics_package => 'main' });
print $$value;
在FuncDef
的花括号括号,它告诉马尔巴丢弃它们。
但是为什么?带括号的版本非常清晰(尤其是对于较长的功能体)。出于好奇,这是什么语言不需要围绕函数定义使用大括号? – ThisSuitIsBlackNot
Text :: Balanced'extract_bracketed'但不幸的是,你必须将你的文本填充到多行字符串中。 – stevesliva
也许这是C-to-XS转换器的一个步骤。 – mob