0

サブルーチンを使用a, f_1(b, c, f_2(d, e))して、行を Lisp スタイルの関数呼び出しで 行に変換しようとしています。a (f_1 b c (f_2 d e))Text::Balanced

関数呼び出しの形式 f(arglist)は です。arglist には、階層呼び出しを使用して 1 つまたは複数の関数呼び出しを含めることができます。

私が試した方法 -

my $text = q|a, f_1(a, b, f_2(c, d))|;

my ($match, $remainder) = extract_bracketed($text); # defaults to '()' 
# $match is not containing the text i want which is : a, b, f_2(c,d) because "(" is preceded by a string;

my ($d_match, $d_remainder) = extract_delimited($remainder,",");
# $d_match doesnt contain the the first string
# planning to use remainder texts from the bracketed and delimited operations in a loop to translate.

/^[\w_0-9]+\(/開始タグが asで終了タグが asのサブ extract_tagged も試してみました/\)/が、そこでも機能しません。 Parse::RecDescent短時間で理解して使いこなすのは難しい。

4

1 に答える 1

1

LISP スタイルに変換するために必要と思われるのは、カンマを削除し、各開き括弧をその前にある関数名の前に移動することだけです。

このプログラムは、文字列を識別子/\w+/または括弧にトークン化し/[()]/、リストを array に格納することによって機能し@tokensます。次に、この配列がスキャンされ、識別子の後に開きかっこが続く場所で、2 つが入れ替わります。

use strict;
use warnings;

my $str = 'a, f_1(b, c, f_2(d, e))';

my @tokens = $str =~ /\w+|[()]/g;

for my $i (0 .. $#tokens-1) {
  @tokens[$i,$i+1] = @tokens[$i+1,$i] if "@tokens[$i,$i+1]" =~ /\w \(/;
}

print "@tokens\n";

出力

a ( f_1 b c ( f_2 d e ) )
于 2013-01-05T19:48:59.000 に答える