On Feb 26, 5:37 am, David Kirkby <david.kir...@onetel.net> wrote:

>
> I was hoping someone might have done it, [Write a grammar for Mathematica]
 as the task seems
> non-trivial. Did you write one for MockMMA, and if so are you willing
> to share it?


1. I (with a student) looked at the task and wrote a partial grammar,
but despaired of completing it as we found too many ad hoc patches.
Now it may be that someone has devised a complete and accurate
grammar,
especially one suited to some automated parser generator, e.g. LALR(1)
or LL(1) or GLR
but we were not clever enough to find a neat one.  I believe, and this
is
a contentious point, it is likely for someone to think he has a
complete and accurate grammar, even if it is not complete or accurate.
People found errors in my parser (since corrected), after several
years.
There may still be errors. And now there are deliberate differences
because
I found more convenient notation, e.g. for comparisons.

2. The material in the file 
http://www.cs.berkeley.edu/~fateman/lisp/mma4max/parser.lisp
is source code for a parser.  The parser is a recursive descent parser
with programs named in a fairly uniform fashion from which
one can generate a grammar.   e.g.   parse-dot, parse-power, ....
But the actions associated with some of these reduction routines
are nasty, and in some cases depend critically on material which
should have been abstracted out of the grammar into the lexical
phase. You (or anyone) is welcome to read up on recursive descent
parsing and run the algorithm for generating a R.D. parser in
reverse, and generate a grammar.  But it won't be complete and
accurate because the component subroutines do screwy things, like
looking ahead for the occasional odd character, -- not token, but
character.  Now it may be possible to do all this with a parser;
indeed it most assuredly IS possible, but the obvious grammar might
be quite huge. A cleverly-encoded grammar might be smaller, but I
think it would not be trivial to do that.  Among other things it
would have to take into account that blank space is sometimes a
token, but sometimes not.

3. I am of course perfectly willing to share the code.
 It has, in fact,  even been used
in a proposal to the NSF under some small business initiation grant.
I had nothing to do with the proposal, and the proposers did not
seek my permission (not that they needed it).


I'm sure that this discussion of the Mathematica syntax could be
placed in a more accessible location, but who knows.

Here's a puzzle.  a.b.c is parsed as Dot[a,b,c] or in Lisp, (Dot a b
c).
1 . 2 . 3 is parsed as Dot[1,2,3], and is displayed by Mathematica
as 1.2.3   ... Observe the white-space or the lack of it around the
dots.
 But if you type in 1.2.3 what do you think you get?
And what about 1. 2.3  ?
For those who do not have Mathematica handy  1.2.3 comes out as 0.36

And if you type it in to WolframAlpha  you get 6.  If you type it in
to MockMMa, you get 6/5.3   (that is, (Dot 6/5 3).   Note that
1.2 is converted to 6/5.


What do you suppose Ira's parser produces?
It is not the first case in which the person who wrote the display for
Mathematica gives a result which is apparently at odds with the input
syntax.

[the C grammar]

> I overlooked that. Thank you for the correction.
You are welcome.

I think the important point I might re-emphasize is this:

Parsing Mathematica syntax with a Sage back end does not get you very
far.
Any non-trivial piece of Mathematica will require a mathematica
evaluator
with matching, binding of variables, etc.   while parsing Sin[x] to
get
sin(x) is trivial, it is mostly pointless. You might think that
converting
Integrate[f,x]  into integrate(f,x) gets you somewhere, but the
accessible
syntax for translating f into Sage (or Maxima) consists of +,*,/, [,],
and
the typical functions like sin, cos, tan.  Why would someone write in
Mathematica syntax in the first place?

Wolfram Alpha seems to not expect Mathematica syntax -- its parser
seems to
disagree.

RJF

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to