Skip to content
Axel Kramer edited this page Aug 17, 2024 · 17 revisions

Scanning the input string

The first step in the Symja interpreter is scanning the input string. The scanner takes in raw source code as a series of characters and produce the tokens that are used in the parsers in the next steps.

The scanner can be found in the class:

  • org.matheclipse.parser.client.Scanner - it is the parent class for the two different parsers used in Symja.

Before producing the parsers tokens, all named (unicode-) characters in an input-string are substituted by the scanner with the corresponding unicode character code.

  • org.matheclipse.parser.client.Characters - for example in the substituteCharacters() method the input string "{\[Alpha], \[Phi], \[Pi]}" will be replaced with their corresponding unicode characters or predefined Symja constants "{α,ϕ,Pi}".

The two parsers can be found in the classes:

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

If an input expression couldn't be scanned or parsed the scanner or parser methods throw a SyntaxError exception:

  • org.matheclipse.parser.client.SyntaxError - syntax errors must be catched by the caller. The method SyntaxError#getMessage() can be used to print the syntax error.

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