Skip to content

antononcube/Raku-Mathematica-Grammar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Raku Mathematica::Grammar

This package provides grammar and action classes for parsing and interpretation of Mathematica (aka Wolfram Language) expressions.


Usage examples

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.


Grammar derivation

In order to derive the grammars I did the following steps:

  1. 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].
  2. Translate the g4 grammars from ANTLR4's BNF-like format to Raku using Jeff Goff's Raku package ANTLR4::Grammar, [JG1].

options{
  //We put target-language dependent code in a base class.
  superClass=LexerBase;
}
  1. 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>

  2. '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".


TODO

Highest priority items are put on top:

  1. Provide execution Raku actions.

  2. Parsing FullForm expressions tests:

    • Algebraic
    • Arithmetic
      • Standard
      • BigNum
      • Rationals
    • List
    • Association
    • Dataset
    • VerificationTest
    • Function definitions
  3. Make the InputForm work.

  4. Parsing InputForm expressions tests:

    • Algebraic
    • Arithmetic
      • Standard
      • BigNum
      • Rationals
    • List
    • Association
    • Dataset
    • VerificationTest
    • Function definitions
  5. Provide execution actions for natural languages explanations

    • English
    • Bulgarian

References

[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.

About

Grammar for parsing Mathematica (aka Wolfram Language) expressions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages