突然想到了一个投机取巧的办法, 不知可行否?(语言解析虽通用, 但是麻烦啊)
代码如下:
<.
var fs = [
['(1.8+2)*(3*4)', '(1.8+2)*3*4'],
['3*(((4+5)))', '3*(4+5)'],
['1+1+1', '1+1+1'],
['2/(2/2)', '2/(2/2)'],
['1/((1+1))', '1/(1+1)'],
['1/((5+6))-7', '1/(5+6)-7']
]
var Need = false
foreach f = fsdo
var ret = f[0]
// 多次迭代
loop
Need = false
ret = FormatIt(ret)
if not Need then
{exit loop}
end loop
?? ret ~ /t/t ~ (ret = f[1])
end foreach
function FormatIt(f)
// 去除嵌套括号
Result = ''
var reg = System.Text.Regex('/( ( (?>[^()]+) | (?R) )* /)', 'x')
var mat = reg.Match(f)
while mat.Successdo
Result ~= mat.UnmatchedValue
Result ~= ReplaceIt(mat.Value, '^/( (/(.*/)) /)$', '$1')
mat.NextMatch()
end while
Result ~= mat.UnmatchedValue
// 处理前导运算符
var ps = [
['+/-*', '*/'],
['+', '+/-']
]
foreach p = psdo
Result = ReplaceIt(Result, GenPattern(p[0], p[1]), '${A}${B}')
end foreach
// 处理后继运算符
end function
function GenPattern(p0, p1)
var fact = '[/./d]+'
Result = '(?P<A>[%s]) /( (?P<B>%s ([%s]%s)+)+ /)'
.Format(p0, fact, p1, fact)
end function
function ReplaceIt(f, p, r)
Result =f
while Result.RegexIsMatch(p, 'x')do
Result = Result.RegexReplace(p, r, 'x')
Need = true
end while
end function
.>