2013-01-05 39 views
0

我試圖線a, f_1(b, c, f_2(d, e))轉換爲使用Text::Balanced子程序和Lisp風格的函數調用行 a (f_1 b c (f_2 d e))的Perl:轉換函數調用到LISP風格的函數調用

函數調用的形式是f(arglist), arglist也可以在它內部進行一個或多個函數調用,

的路上我試圖 -

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]+\(/和結束標記extract_tagged作爲/\)/子,但不會太那裏工作。 Parse::RecDescent很難理解並在短時間內投入使用。

+1

你沒有分隔(報價)文字。你對'extract_delimited'沒有用處。你甚至從來沒有嘗試提取標識符?我認爲你該僱用某人了。你沒有時間並不能完成我們的工作來編寫你的代碼。 – ikegami

回答

1

轉換爲LISP樣式似乎需要的所有東西都是刪除逗號,並將每個左括號前移到之前的函數名稱之前。

該程序的工作方式是將字符串標記爲標識符/\w+/或括號/[()]/,並將該列表存儲在數組@tokens中。然後掃描這個數組,然後在標識符後面加一個左括號,然後切換這兩個數字。

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