Perl:围绕数学表达式的字符串操作

发布于 2024-10-04 01:17:33 字数 889 浏览 0 评论 0原文

我正在学习 Perl,我想包围这样的表达式

function1(mathematical_expresion1)*function2(mathematical_expresion2)

其中“function1”和“function2”可以是任何单词并在内部组合数字,例如 function1 可以是 fun3dec、fun4nels、factor9 或其他... 所以我的代码将变成这样

surrounded(function1(mathematical_expresion1)*function2(mathematical_expresion2))surroundedend

其中包围和包围结束是字符串链。

因此,如果我有这样的表达式:

exp(-49/200)-exp(-49/200)*(x-49/200)+1/2*exp(-49/200)*(x-49/200)^2-1/6*exp(-49/200)*(x-49/200)^3+1/24*exp(-49/200)*(x-49/200)^4-1/120*exp(-49/200)*(x-49/200)^5+1/720*exp(-49/200)*(x-49/200)^6-1/5040*exp(-49/200)*(x-49/200)^7+1/40320*exp(-49/200)*(x-49/200)^8-1/362880*exp(-49/200)*(x-49/200)^9+1/3628800*exp(-49/200)*(x-49/200)^10-1/39916800*exp(-49/200)*(x-49/200)^11+1/479001600*exp(-49/200)*(x-49/200)^12

我可以将前一个表达式中的两项的所有乘法包围起来。

谢谢你教我 Perl!

I´m learning Perl, I would like to surrounded an expression like that

function1(mathematical_expresion1)*function2(mathematical_expresion2)

Where 'function1'and 'function2' could be whatever word and combine numbers inside, for instance function1 could be fun3dec, fun4nels, factor9 or whatever...
so my code will became this

surrounded(function1(mathematical_expresion1)*function2(mathematical_expresion2))surroundedend

Where surrounded and surroundedend are string chains.

So if I have an expression like this:

exp(-49/200)-exp(-49/200)*(x-49/200)+1/2*exp(-49/200)*(x-49/200)^2-1/6*exp(-49/200)*(x-49/200)^3+1/24*exp(-49/200)*(x-49/200)^4-1/120*exp(-49/200)*(x-49/200)^5+1/720*exp(-49/200)*(x-49/200)^6-1/5040*exp(-49/200)*(x-49/200)^7+1/40320*exp(-49/200)*(x-49/200)^8-1/362880*exp(-49/200)*(x-49/200)^9+1/3628800*exp(-49/200)*(x-49/200)^10-1/39916800*exp(-49/200)*(x-49/200)^11+1/479001600*exp(-49/200)*(x-49/200)^12

I could surround a all multiplication of two terms in the previous expression.

Thank you for teach me Perl!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜唯美灬不弃 2024-10-11 01:17:33

这是第一个(不完整的)解决方案,它提出了一些关于您想要做什么的问题:

为了简洁和清晰,我使用了更简单的语法 - 函数的参数是简单的数字或表达式。函数名称是单个字母。环绕声被 S{} 取代。不过,很容易将此扩展到您的示例。

Sample.txt:

 1  A(1)
 2  A(1)*B(2)
 3  C(2)+A(1)*B(2)
 4  C(A(1)*B(2))
 5  C(A(1)*B(3)+C(4))
 6  A(1)*B(2)+C(3)*D(4)
 7  A(1)*B(2)*C(3)*D(4)
 8  A(B(2)*C(3))*D(4)
 9  C(A(1)+B(3))*C(4)

perl < sample.txt -pe 's/(\w\(\d+\)\*\w\(\d+\))/S{\1}/g'

 1  A(1)
 2  S{A(1)*B(2)}
 3  C(2)+S{A(1)*B(2)}
 4  C(S{A(1)*B(2)})
 5  C(S{A(1)*B(3)}+C(4))
 6  S{A(1)*B(2)}+S{C(3)*D(4)}
 7  S{A(1)*B(2)}*S{C(3)*D(4)}
 8  A(S{B(2)*C(3)})*D(4)
 9  C(A(1)+B(3))*C(4)

那么,第 7 行应该做什么? 8号线应该做什么?

第 9 行不起作用,因为我的代码不够智能,而且我不知道您是否需要它起作用。


我建议您确实需要一个解析器(例如 yacc)以及一个词法分析器。 Perl 有词法分析器,但没有解析器。

这里的问题是您正在搜索以下实例:

<expression> `*` <expression>

但由于表达式被定义为

<expression> = \d+
             | <function>
             | <expression> <op> <expression>

You need a 下推自动机< /a> 使其正常工作。 Perl 正则表达式无法做到这一点。

注意:我的编译器课程已经过去十年了,所以我的术语可能有点不稳定。

Here's a first (incomplete) solution, that raises some questions about what you want to do:

I have used a simpler syntax for brevity and clarity - arguments to functions are simple digits or expressions. function names are single letters. The surround is replaced by S{}. It's easy to extend from this to your example, though.

sample.txt:

 1  A(1)
 2  A(1)*B(2)
 3  C(2)+A(1)*B(2)
 4  C(A(1)*B(2))
 5  C(A(1)*B(3)+C(4))
 6  A(1)*B(2)+C(3)*D(4)
 7  A(1)*B(2)*C(3)*D(4)
 8  A(B(2)*C(3))*D(4)
 9  C(A(1)+B(3))*C(4)

perl < sample.txt -pe 's/(\w\(\d+\)\*\w\(\d+\))/S{\1}/g'

 1  A(1)
 2  S{A(1)*B(2)}
 3  C(2)+S{A(1)*B(2)}
 4  C(S{A(1)*B(2)})
 5  C(S{A(1)*B(3)}+C(4))
 6  S{A(1)*B(2)}+S{C(3)*D(4)}
 7  S{A(1)*B(2)}*S{C(3)*D(4)}
 8  A(S{B(2)*C(3)})*D(4)
 9  C(A(1)+B(3))*C(4)

So, what should line 7 be doing? What should line 8 be doing?

Line 9 doesn't work because my code isn't smart enough, and I don't know if you NEED it to work.


I suggest that you really need a parser (e.g. yacc) as well as a lexer. Perl has a lexer in it, but not a parser.

The problem here is that you are searching for instances of:

<expression> `*` <expression>

but since expression is defined as

<expression> = \d+
             | <function>
             | <expression> <op> <expression>

You need a push down automaton to make this work correctly. Perl regular expressions just can't do this.

NOTE: it's been a decade since my compiler course, so I may be a bit wobbly in my terminology.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文