Skip to main content
https://travis-ci.org/rocky/FoxySheep2.svg?branch=master

This is the Robert Jacobson’s Python implementation of the FoxySheep parser and lexer for Mathematica. It has been stripped down and reorganized a bit.

Examples

When installed, the command-line translator is called foxy-sheep which can convert Mathematica InputForm to Mathematica FullForm without having Mathematica installed: To run the code interactively:

$ foxy-sheep
Enter a Mathematica expression. Enter either an empty line, Ctrl-C, or Ctrl-D to exit.
In[1]:= 1+2
Plus[1,2]
In[1]:=D[4 x^2]
D[Times[4,Power[x,2]]]
In[1]:=
$

The first few examples from Fast Introduction for Math Students

foxy-sheep -o python
Enter a Mathematica expression. Enter either an empty line, Ctrl-C, or Ctrl-D to exit.
In[1]:= 2 + 2
(2 + 2)

Out[1]=4
In[2]:= 1 + 2 + 3
(1 + 2 + 3)

Out[2]=6
In[3]:= % + 4
(Out() + 4)

Out[3]=10
In[4]:= 5 + 2 * 3 - 7.5
(5 + 2 * 3 -
decimal.Decimal(7.5))

In[5]:= ((5 - 3) ^ (1 + 2))/ 4
((5 - 3) ** (1 + 2) / 4)

Out[5]=2.0
In[6]:= GCD[12,15]
math.gcd(12, 15)

Out[6]=3
In[7]:= {1, 2, 3}
[1, 2, 3]

Out[7]=[1, 2, 3]

To call from Python:

from FoxySheep import if2ff, if2python
print(if2ff("x^2-3x+4")
# Prints: Plus[Power[x,2],Times[-1,3,x],4]
print(if2ff("x^2-3x+4")
# Prints (x ** 2 + -1 * 3)

For more demos run foxy-sheep --file pytest/parse_expressions/SO1.m or with other Mathamatica files in pytest/parse_expressions.

For help on foxy-sheep, run foxy-sheep --help.

Conversion to Python

A very crude translator to Python has been started. While there are still a lot of details to be filled out, some of the basics are there.

Here are some examples:

167.5 -> decimal.Decimal(167.5)
15^^8 -> int(15, 8)
132`  -> (132)
1 / 10 3 -> (1 / 10 * 3)
x^2 + y^2 -> (x ** 2 + y ** 2)
GCD[12, 15] -> math.gcd(12, 15)
Range[10] -> range(10)
{a, b, c, d}[[3]] -> [a, b, c, d][2]  # Handles 0-1 conversion
{a, b, c, d, e, f}[[2 ;; 4]] -> [a, b, c, d, e, f][1:3] # ditto

Conversion to Python is done via transforming “InputForm” input to an “OutputForm” parse tree, and then using that to convert to a Python AST. Finally a Python module, astor, is used to dump this to text.

Why do we go through this more complicated for translating one string to another?

By keeping the structure as an AST we can contemplate more powerful transformations and make use of routines that exist for working with Python AST’s.

For example, translating {1,2,3} + 2 into Python, while not handled now, can be done by looking at the types of the operands of plus, and noticing one is a scalar while the other is a list.

Regenerating the lexer/parser

To change the grammar you’ll need the ANTLR Parser Generator (antlr4), version 4.7.x installed.

To (re)generate the lexer/parser you need to

$ make

The resulting files are placed in FoxySheep/generated.

Files generated by ANTLR4 are assumed to be in a subdirectory called generated containing an empty __init__.py file. See the Makefile for details.

FoxySheepLexer Must Subclass Lexer

In order for the generated antlr4 lexer to work we need to patch the generated Python lexer FoxySheep.lexer.py; The patch file FoxySheep.lexer.py.patch does this. The Makefile target for FoxySheepParser.py contains the patch command.

If patching is not done you’ll get an AttributeError exception in the lexer you try to run it such as through foxy-sheep.

AttributeError: 'FoxySheepLexer' object has no attribute 'checkAdditiveOp'

See Also

FoxySheep

Release files for FoxySheep 1.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for FoxySheep 1.2.3
File Size Uploaded
FoxySheep-1.2.3.tar.gz 108.6 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for FoxySheep 1.2.3
File
FoxySheep-1.2.3-py38-none-any.whl Python 3.8 none any Details
FoxySheep-1.2.3-py36-none-any.whl Python 3.6 none any Details
FoxySheep-1.2.3-py3.8.egg Legacy Egg format - - Details
FoxySheep-1.2.3-py3.7.egg Legacy Egg format - - Details
FoxySheep-1.2.3-py3.6.egg Legacy Egg format - - Details

Total release size: 1.2 MB

Release files / FoxySheep-1.2.3.tar.gz

Download URL FoxySheep-1.2.3.tar.gz
Size 108.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c0b489a007948f60bdec24a815db5a9a1da0243b46a615949f18556c2b219fb1
BLAKE2b-256 checksum
How to use checksums
cd4d86be7b952e50c5a291c7e454c8d91fd9ceb18d95caba73da82a4d39dad1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / FoxySheep-1.2.3-py38-none-any.whl

Download URL FoxySheep-1.2.3-py38-none-any.whl
Size 121.5 kB
Tags Python 3.8
SHA-256 checksum
How to use checksums
829012654f76d1acb80204f9f0aae5d08e98e8fa90a0e5ae0a1ad6ee7c21b3e3
BLAKE2b-256 checksum
How to use checksums
ad3c72989620c661d3b3c6f5d2171f3e60e0d0d7d9230cfd0fb3ea147d116bce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / FoxySheep-1.2.3-py36-none-any.whl

Download URL FoxySheep-1.2.3-py36-none-any.whl
Size 121.5 kB
Tags Python 3.6
SHA-256 checksum
How to use checksums
674e8fa0e0af663b0252c49252db13deebb711a917945b68eafa8dd985d1ee8b
BLAKE2b-256 checksum
How to use checksums
4227f835ab5c9410f181d815bcecce2869346e86cb07b9d0586e6c262740abc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / FoxySheep-1.2.3-py3.8.egg

Download URL FoxySheep-1.2.3-py3.8.egg
Size 277.6 kB
Tags Egg
SHA-256 checksum
How to use checksums
7d77ef47cdd69fa029b3692ac1a502afdcfd775fc3dee8db9ef56e1c680d9ad7
BLAKE2b-256 checksum
How to use checksums
b32fb45cb0b87d3f0e2de4d2b5646eebe9f0246b4f476637f3719766c7fa55c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / FoxySheep-1.2.3-py3.7.egg

Download URL FoxySheep-1.2.3-py3.7.egg
Size 277.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
d23cb3743fe5fd95557d5c799e2b22fef3bf71d7d27ddc71694529bcec77abcc
BLAKE2b-256 checksum
How to use checksums
1c32c26f2989cde9e63f58a967d0a9a14f20e8165f15a0d93d10bb1d8013c250
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / FoxySheep-1.2.3-py3.6.egg

Download URL FoxySheep-1.2.3-py3.6.egg
Size 277.2 kB
Tags Egg
SHA-256 checksum
How to use checksums
d50d02f0c405a761705ee04d8789095d8ce23bb1058077b8e4e306fb5d297b61
BLAKE2b-256 checksum
How to use checksums
0b143d6f6d8cc3b8ba81bab784e6d7f010df10922d0da5dae351452c61278a3a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release history Release notifications | RSS feed

This release

1.2.3 This release

6 release files

1.2.2

6 release files

1.2.1

5 release files

1.2.0

5 release files

1.1.0

7 release files

1.0.0

7 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page