Skip to content
axexlck edited this page Nov 25, 2018 · 17 revisions

Symja maintains two parsers:

  • org.matheclipse.core.parser.ExprParser - which transforms parsed math expressions directly into the IExpr Class-hierarchy
  • org.matheclipse.parser.client.Parser - a client side parser (package org.matheclipse.parser.client) which can be used for example for RPC calls or in a Google GWT environment

ExprParser (package org.matheclipse.core.parser)

The following snippet creates the common Symja syntax parser and returns an IExpr Class-hierarchy

...
		final ExprParser parser = new ExprParser(EvalEngine.get(), ExprParserFactory.RELAXED_STYLE_FACTORY, true);
		IExpr expression = parser.parse("string-expression");
...

This snippet creates a Mathematica syntax parser and returns an IExpr Class-hierarchy

...
	static {
		Config.PARSER_USE_LOWERCASE_SYMBOLS = true;
	}
...

...
		final ExprParser parser = new ExprParser(EvalEngine.get(), ExprParserFactory.MMA_STYLE_FACTORY, false);
		IExpr expression = parser.parse("string-expression");
...

Parser (package org.matheclipse.parser.client)

This parser doesn't depend on an EvalEngine instance and can be used in clients to avoid the dependency to the complete Symja libraries by using only the package org.matheclipse.parser.client. The parser transforms a string into instances of the ASTNode base class.

The following snippet creates the common Symja syntax parser and returns an ASTNode instance:

...
		try {
			Parser p = new Parser(ASTNodeFactory.RELAXED_STYLE_FACTORY, false);
			ASTNode obj = p.parse("stringExpression");
		} catch (Exception e) {
			// handle syntax errors here 
		}
...

The JUnit test classes can be found in the file ParserTestCase.java.

This snippet creates a Mathematica syntax parser and returns an ASTNode instance:

...		
		try {
			Parser p = new Parser(ASTNodeFactory.MMA_STYLE_FACTORY, true);
			ASTNode obj = p.parse("stringExpression");
		} catch (Exception e) {
			// handle syntax errors here 
		}
...

The JUnit test classes can be found in the file RelaxedParserTestCase.java.

If necessary an ASTNode can be transformed to an IExpr with the AST2Expr class.

...		
		ASTNode node = .....
		IExpr expr = new AST2Expr(EvalEngine.get()).convert(node); 
...
Clone this wiki locally