This package provides grammar and action classes for parsing and interpretation of Mathematica (aka Wolfram Language) expressions.
Currently only Mathematica
FullForm
expressions parsing is implemented.
use Mathematica::Grammar;
say Mathematica::Grammar.parse( 'Plus[List[a,b],List[2,3]]');See the file "Basic-WL-expressions-parsing.raku" or the unit tests for more examples.
In order to derive the grammars I did the following steps:
-
Take ANTLR4 grammars from Rocky Bernstein's Python package FoxySheep2, [RB1].
- For more details of the origins of the "foxy sheep" projects see Robert Jacobson's project FoxySheep, [RJ1].
-
Translate the g4 grammars from ANTLR4's BNF-like format to Raku using Jeff Goff's Raku package ANTLR4::Grammar, [JG1].
-
See the conversion code in the file Convert-FoxySheep2-ANTLR4-grammars.raku
-
Comment out in InputFormLexerRules.g4 :
-
options{
//We put target-language dependent code in a base class.
superClass=LexerBase;
}-
Minor grammar tweaks:
-
Convert some tokens into regexes
-
Convert
( ... )groups into[ ... ] -
Translate applications of ANTLR4 sequence idiom into Raku's standard idiom, e.g.
<expr> % <COMMA>
-
-
'Fix' the left recursion of
<expr>in "FullForm.rakumod". Compare the original ANTLR4-derived rule and its replacement:
# Original
token expr {
|| <numberLiteral>
|| <StringLiteral>
|| <symbol>
|| <expr>
<LBRACKET>
<expressionList>
<RBRACKET> }# Replacement
token expr-head {
|| <numberLiteral>
|| <StringLiteral>
|| <symbol>
}
regex expr {
|| <expr-head> \h* [ <LBRACKET> \h* <expressionList> \h* <RBRACKET> ]+
|| <expr-head>
}For the actual ANTLR4-to-Raku conversion code see the file "Convert-FoxySheep2-ANTLR4-grammars.raku".
Highest priority items are put on top:
-
Provide execution Raku actions.
-
Parsing
FullFormexpressions tests:- Algebraic
- Arithmetic
- Standard
- BigNum
- Rationals
-
List -
Association -
Dataset -
VerificationTest - Function definitions
-
Make the
InputFormwork. -
Parsing
InputFormexpressions tests:- Algebraic
- Arithmetic
- Standard
- BigNum
- Rationals
-
List -
Association -
Dataset -
VerificationTest - Function definitions
-
Provide execution actions for natural languages explanations
- English
- Bulgarian
[JG1] Jeff Goff, ANTLR4::Grammar Raku package, (2015), GitHub/drforr.
[RB1] Rocky Bernstein, FoxySheep2 Python package, (2015), GitHub/rocky.
[RJ1] Robert Jacobson, FoxySheep Python package, (2015), GitHub/rljacobson.
[TP1] Terrance Parr, ANTLR, https://www.antlr.org.
[TP1] Terrance Parr, The Definitive ANTLR 4 Reference, (2013), Pragmatic Bookshelf, ISBN:978-1-934356-99-9.