Perl:围绕数学表达式的字符串操作
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是第一个(不完整的)解决方案,它提出了一些关于您想要做什么的问题:
为了简洁和清晰,我使用了更简单的语法 - 函数的参数是简单的数字或表达式。函数名称是单个字母。环绕声被
S{}
取代。不过,很容易将此扩展到您的示例。Sample.txt:
那么,第 7 行应该做什么? 8号线应该做什么?
第 9 行不起作用,因为我的代码不够智能,而且我不知道您是否需要它起作用。
我建议您确实需要一个解析器(例如 yacc)以及一个词法分析器。 Perl 有词法分析器,但没有解析器。
这里的问题是您正在搜索以下实例:
但由于表达式被定义为
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:
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:
but since expression is defined as
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.