11

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.

2
  • Perhaps someone wrote a parser in ANTLR?
    – Nestor
    Commented Oct 22, 2009 at 17:01
  • 1
    There is a Mathematica parser out there (at least a description is linked at cs.berkeley.edu/~fateman) but it's written in Common Lisp, which, as far as I know doesn't have a CLR/.NET implementation.
    – Pillsy
    Commented Oct 23, 2009 at 2:55

3 Answers 3

6

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#?

1

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.

0

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.

1
  • Yes.. but for that I need the Kernel. Anyways... I think you are right. Such parser doesn't seem to exist. Part of the problem is that there is no published grammar for the language. I've also heard that the language cannot be parsed with a LALR parser.
    – Nestor
    Commented Oct 22, 2009 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.