Here's a description of the syntax
http://reference.wolfram.com/mathematica/guide/Syntax.html
http://reference.wolfram.com/mathematica/tutorial/TheSyntaxOfTheMathematicaLanguage.html
Things to note in particular is that whitespace can often mean
multiplication. i.e.
In[1]:= 12.1 2
Out[1]= 24.2
Mathematica supports many programming styles - procedule, functional
and rule-based programming
A Lisp to Mathematica translator was written by Prof Fateman
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.51.4310&rep=rep1&type=pdf
In the paper Fateman says the language appears to be ad-hoc, so he had
limited success with a common parser and used a hand-written one. But
I'm not entirely convinced of his objectivity - he tends to mock the
creators of Mathematica - his program is called MockMMA. He was one of
the creators of a computer algebra system Maxima, which he always
compares favorably to Mathematica, though few others do,
I've never written a compiler, beyond noddy calculators in
introductory books, so I suspect this language is not an ideal one to
learn with. But I'd be interested in what, if any tools would be
capable of handling such a complex language,
Dave
Fateman's work was a *Mathematica-to-Lisp* translator: quoting from that paper,
he wrote
"a Common Lisp program that can read (from a file or a keyboard) virtually
any Mathematica program or command, and will produce a Lisp data structure
closely resembling the FullForm printout of Mathematica."
It's also worth noting that Wolfram Research claims copyright over the
Mathematica language, and asserts that Fateman's translator infringed
that copyright. (Google 'Fateman Mathematica "Brown & Bain"' for a
letter from WRI's lawyers to Fateman claiming this.) I don't know the
exact boundary of how much of and/or how closely you can clone Mathematica
without getting into legal trouble.
--
-- "Jonathan Thornburg [remove -animal to reply]" <jth...@astro.indiana-zebra.edu>
Dept of Astronomy, Indiana University, Bloomington, Indiana, USA
"C++ is to programming as sex is to reproduction. Better ways might
technically exist but they're not nearly as much fun." -- Nikolai Irgens
A computer language cannot be copyrighted, ...
> I don't know the exact boundary of how much of and/or how closely
> you can clone Mathematica without getting into legal trouble.
... only actual code. So only if actual code has been used, there might
be a problem.
[This agrees with my understanding as well, but none of us are lawyers,
so I'll end the legal discussion here. -John]
Yes. but I think they gave up. His code is still available.
> A computer language cannot be copyrighted, ...
>
> > I don't know the exact boundary of how much of and/or how closely
> > you can clone Mathematica without getting into legal trouble.
>
> ... only actual code. So only if actual code has been used, there might
> be a problem.
> [This agrees with my understanding as well, but none of us are lawyers,
> so I'll end the legal discussion here. -John]
I'm more interested in the technical issues.
Does anyone have any comments about the most suitable tool for a
parser?
Dave
Hey Dave,
I may suggest you taking a look at yapp and also this tutorial (http://
alfarrabio.di.uminho.pt/~albie/publications/perlflex.pdf ). Perl
should be suitable in your case. Moreover, llvm is far more beyond a
parser.
-Qirun
Others have noted that LLVM is much more than a parser. Are you
trying to generate *code* from MMa syntax? Are you trying to generate
code from pure MMa equations or from actual MMa programs?
> Here's a description of the syntax
>
> http://reference.wolfram.com/mathematica/guide/Syntax.html
> http://reference.wolfram.com/mathematica/tutorial/TheSyntaxOfTheMathematicaLanguage.html
>
> Things to note in particular is that whitespace can often mean
> multiplication. i.e.
> I've never written a compiler, beyond noddy calculators in
> introductory books, so I suspect this language is not an ideal one to
> learn with. But I'd be interested in what, if any tools would be
> capable of handling such a complex language,
MMa (equations or programs) isn't a particularly complex language,
either conceptually or from the point of view of a parser.
Mostly it is Lisp S-expressions using xyz[...] instead of (xyz ...).
*Executing* MMa code is a bit messier; you need a program
transformation system to do pattern-match/rewrites to implement
much of the semantics. And of course of your code contains any
complicated formulas needing simplification,
you might need all of MMa proper to provide the necessary
set of rewrites that encode all that math knowledge.
FWIW, our DMS Software Reengineering Toolkit (a program
transformation system) has a working MMa langauge parser
as an available option (see website for more details).
We use a GLR parser for all of our parsers;
I suspect that's overkill for the MMa grammar but it seems to work fine.
--
Ira Baxter, CTO
www.semanticdesigns.com
Thank you very much. Sorry I did not reply earlier.
> Moreover, llvm is far more beyond a parser.
Is it a parser at all? I got the impression it took the abstract
syntax tree as input, and not the source language.
I've not played with LLVM at all. I suspect such a project is beyond
me, but I'd like to be aware of what tools might be able to do this.
Dave
As I noted, I had not looked at LLVM much, but I was not aware it
could be used for parsing the code. I was under the impression that
for example to make a C compiler, it uses the gcc front end, rather
than parse the C code directly.
LLVM seemed to be an ideal tool for the back end though.
> Are you trying to generate *code* from MMa syntax? Are you trying
> to generate code from pure MMa equations or from actual MMa
> programs?
I don't fully understand the question, which is no doubt due to my
lack of knowledge.
I'm thinking of basically making an MMA clone, which accepts
Mathematica input and acts as an interpreter. However, unlike Octave,
which is a MATLAB clone, I was not looking to make a 100% clone. If
part of the Mathematica language was particularly difficult to parse,
then it would be ignored.
If for example, it was found to be very difficult to parse input like
In[1]:= Pi //N
Out[1]= 3.14159
then I would be happy to accept that this could easily be written as:
In[2]:= N[Pi]
Out[2]= 3.14159
and not worry too much about it. I'm sure the above will not be an
issue, but there are some other complex parts of Mathematica which I
thought might be very challenging.
Likewise, if there are serious flaws in the way the language works, it
might be better to not worry about being compatible, but do it better.
Richard Fateman's paper points out what he considers a huge number of
flaws, but I take a lot of what he says with a pinch of salt. He is
clearly no fool, but is very negative about almost anything unless it
is Lisp. For example, he thinks use of a space for multiplication is a
bad idea, yet that's how most people write maths. We write
2 Pi x
rather than
2*Pi*x
> > Here's a description of the syntax
>
> >http://reference.wolfram.com/mathematica/guide/Syntax.html
> >http://reference.wolfram.com/mathematica/tutorial/TheSyntaxOfTheMathe...
> MMa (equations or programs) isn't a particularly complex language,
> either conceptually or from the point of view of a parser.
> Mostly it is Lisp S-expressions using xyz[...] instead of (xyz ...).
It always stuck me as complex language to use, with such obscure
syntax in places, that I thought it would be very difficult to parse.
Some Mathematica code is similar in obscurity to entries for the
obfuscated C contest!
In the paper "A Lisp-Language Mathematica-to-Lisp translator" by Prof.
Fateman
http://citeseerx.ist.psu.edu/viewdoc/download%3Fdoi%3D10.1.1.51.4310%26rep%3D
rep1%26type%3Dpdf&usg=AFQjCNG_OKvzYioUhV95ZSjVkwvLGW9xBw
he says in the section "Lexical Analysis and Parsing"
"After trying (with only modest success) various mostly-automatic
parsing techniques, we ended up with a basically hand-coded parser.
Mathematica unlike some computer algebra systems does not feature an
extensible syntax: this suggested the implementation was somewhat ad-
hoc."
I'm not sure how objective Richard's comments are though. I don't know
if
* He just wanted to have a dig at Wolfram Research.
* Richard is right, and most automatic parsing techniques are not
appropriate.
* He chose inappropriate mostly--automatic parsing techniques. (He
does not list what he tried)
* He chose the right parsing tools, but did a poor implementation
Much of the paper suggests the first may be the case, but it may be
the second, which is more worrying from my point of view.
> *Executing* MMa code is a bit messier; you need a program
> transformation system to do pattern-match/rewrites to implement
> much of the semantics. And of course of your code contains any
> complicated formulas needing simplification,
> you might need all of MMa proper to provide the necessary
> set of rewrites that encode all that math knowledge.
Yes, I am aware of that. My thoughts are that if one could get to the
point of being able to parse the input, making it open-source, then
others would be able to improve it by encoding at least a subset of
the maths knowledge. Realistically, it is not going to be possible to
make a fully functional Mathematica clone.
> FWIW, our DMS Software Reengineering Toolkit (a program
> transformation system)
I was looking for open-source solution, to make an open-source
alternative to Mathematica.
> Ira Baxter, CTOwww.semanticdesigns.com
Thank you Ian
Dave