Parser for the Mathematica syntax?

假如想象 提交于 2019-12-03 07:28:32

问题


Is there a built parser that I can use from C# that can parse mathematica expressions?

I know that I can use the Kernel itself to parse an expression, and use .NET/Link to retrieve the tree structure... But I'm looking for something that doesnt rely on the Kernel.


回答1:


My matheclipse-parser module implements a parser in Java which can parse a big subset of mathematica expressions. See the readme.md page for usage. Maybe you can port the parser to C#?




回答2:


I wrote a Mathematica parser in 300 lines of OCaml code under contract for Wolfram Research and found it to be quite easy because the grammar is clearly documented in their literature and any ambiguities are easily found by playing with Mathematica itself.




回答3:


The mathematica grammar isn't well documented, true. But AFAIK, it is LALR(1) and likely LL(1); the bracketed /tagged syntax from gives the parser complete clues about what to expect next, just like LISP and XML.

The DMS Software Reengineering Toolkit does have a Mathematica grammar that has been used for real tasks. This includes MMa programs as well as pure expression forms.

That probably doesn't help you, since you want one in C#.

If you have access to the Kernal, I'd stick to that.




回答4:


I don't think such a thing exists already (I'd love to know about it). But it may be useful that within Mathematica you can apply the function FullForm to any expression and get something very easy to parse, kind of like an s-expression in Lisp. For example,

FullForm[a+b*c]

yields

Plus[a, Times[b,c]]

That's the underlying representation of all Mathematica expressions and should be straightforward to parse.



来源:https://stackoverflow.com/questions/1608380/parser-for-the-mathematica-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!