Search in sources :

Example 51 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class BasicPatternPropertiesTestCase method checkPriority.

public void checkPriority(String patternString, int priority) {
    try {
        ASTNode node = fParser.parse(patternString);
        IExpr pat = AST2Expr.CONST.convert(node);
        PatternMatcher matcher = new PatternMatcher(pat);
        assertEquals(matcher.getPriority(), priority);
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals(0, priority);
    }
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher)

Example 52 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class BasicPatternPropertiesTestCase method comparePriority.

public void comparePriority(String patternString1, String patternString2, int result) {
    try {
        ASTNode node = fParser.parse(patternString1);
        IExpr pat1 = AST2Expr.CONST.convert(node);
        PatternMatcher matcher1 = new PatternMatcher(pat1);
        node = fParser.parse(patternString2);
        IExpr pat2 = AST2Expr.CONST.convert(node);
        PatternMatcher matcher2 = new PatternMatcher(pat2);
        assertEquals(matcher1.compareTo(matcher2), result);
    } catch (Exception e) {
        e.printStackTrace();
        assertEquals(Integer.MAX_VALUE, result);
    }
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) IExpr(org.matheclipse.core.interfaces.IExpr) PatternMatcher(org.matheclipse.core.patternmatching.PatternMatcher)

Example 53 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class Parser method parseExpression.

/**
	 * See <a href="http://en.wikipedia.org/wiki/Operator-precedence_parser"> Operator -precedence parser</a> for the
	 * idea, how to parse the operators depending on their precedence.
	 * 
	 * @param lhs
	 *            the already parsed left-hand-side of the operator
	 * @param min_precedence
	 * @return
	 */
private ASTNode parseExpression(ASTNode lhs, final int min_precedence) {
    ASTNode rhs;
    Operator oper;
    InfixOperator infixOperator;
    PostfixOperator postfixOperator;
    while (true) {
        if (fToken == TT_NEWLINE) {
            return lhs;
        }
        if ((fToken == TT_LIST_OPEN) || (fToken == TT_PRECEDENCE_OPEN) || (fToken == TT_IDENTIFIER) || (fToken == TT_STRING) || (fToken == TT_DIGIT) || (fToken == TT_SLOT) || (fToken == TT_SLOTSEQUENCE)) {
            // lazy evaluation of multiplication
            oper = fFactory.get("Times");
            if (oper.getPrecedence() >= min_precedence) {
                rhs = parseLookaheadOperator(oper.getPrecedence());
                lhs = fFactory.createFunction(fFactory.createSymbol(oper.getFunctionName()), lhs, rhs);
                continue;
            }
        } else {
            if (fToken == TT_DERIVATIVE) {
                lhs = parseDerivative(lhs);
            }
            if (fToken != TT_OPERATOR) {
                break;
            }
            infixOperator = determineBinaryOperator();
            if (infixOperator != null) {
                if (infixOperator.getPrecedence() >= min_precedence) {
                    getNextToken();
                    ASTNode compoundExpressionNull = parseCompoundExpressionNull(infixOperator, lhs);
                    if (compoundExpressionNull != null) {
                        return compoundExpressionNull;
                    }
                    while (fToken == TT_NEWLINE) {
                        getNextToken();
                    }
                    rhs = parseLookaheadOperator(infixOperator.getPrecedence());
                    lhs = infixOperator.createFunction(fFactory, lhs, rhs);
                    while (fToken == TT_OPERATOR && infixOperator.getGrouping() == InfixOperator.NONE && infixOperator.getOperatorString().equals(fOperatorString)) {
                        getNextToken();
                        rhs = parseLookaheadOperator(infixOperator.getPrecedence());
                        ((FunctionNode) lhs).add(rhs);
                    }
                    continue;
                }
            } else {
                postfixOperator = determinePostfixOperator();
                if (postfixOperator != null) {
                    if (postfixOperator.getPrecedence() >= min_precedence) {
                        getNextToken();
                        lhs = postfixOperator.createFunction(fFactory, lhs);
                        lhs = parseArguments(lhs);
                        continue;
                    }
                } else {
                    throwSyntaxError("Operator: " + fOperatorString + " is no infix or postfix operator.");
                }
            }
        }
        break;
    }
    return lhs;
}
Also used : InfixOperator(org.matheclipse.parser.client.operator.InfixOperator) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) PrefixOperator(org.matheclipse.parser.client.operator.PrefixOperator) Operator(org.matheclipse.parser.client.operator.Operator) PostfixOperator(org.matheclipse.parser.client.operator.PostfixOperator) ASTNode(org.matheclipse.parser.client.ast.ASTNode) FunctionNode(org.matheclipse.parser.client.ast.FunctionNode) InfixOperator(org.matheclipse.parser.client.operator.InfixOperator)

Example 54 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class DoubleEvaluator method getVariables.

/**
	 * Get the variable names from the given expression.
	 * 
	 * @param expression
	 * @param result
	 *            a set which contains the variable names
	 * @param relaxedSyntax
	 *            if <code>true</code> us e function syntax like
	 *            <code>sin(x)</code> otherwise use <code>Sin[x]</code>.
	 */
public static void getVariables(String expression, Set<String> result, boolean relaxedSyntax) {
    Parser p = new Parser(relaxedSyntax ? ASTNodeFactory.RELAXED_STYLE_FACTORY : ASTNodeFactory.MMA_STYLE_FACTORY, relaxedSyntax);
    ASTNode node = p.parse(expression);
    getVariables(node, result);
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode) Parser(org.matheclipse.parser.client.Parser)

Example 55 with ASTNode

use of org.matheclipse.parser.client.ast.ASTNode in project symja_android_library by axkr.

the class Parser method parse.

/**
	 * Parse the given <code>expression</code> String into an ASTNode.
	 * 
	 * @param expression
	 *            a formula string which should be parsed.
	 * @return the parsed ASTNode representation of the given formula string
	 * @throws SyntaxError
	 */
public ASTNode parse(final String expression) throws SyntaxError {
    initialize(expression);
    final ASTNode temp = parseExpression();
    if (fToken != TT_EOF) {
        if (fToken == TT_PRECEDENCE_CLOSE) {
            throwSyntaxError("Too many closing ')'; End-of-file not reached.");
        }
        if (fToken == TT_LIST_CLOSE) {
            throwSyntaxError("Too many closing '}'; End-of-file not reached.");
        }
        if (fToken == TT_ARGUMENTS_CLOSE) {
            throwSyntaxError("Too many closing ']'; End-of-file not reached.");
        }
        throwSyntaxError("End-of-file not reached.");
    }
    return temp;
}
Also used : ASTNode(org.matheclipse.parser.client.ast.ASTNode)

Aggregations

ASTNode (org.matheclipse.parser.client.ast.ASTNode)56 Parser (org.matheclipse.parser.client.Parser)32 IExpr (org.matheclipse.core.interfaces.IExpr)10 FunctionNode (org.matheclipse.parser.client.ast.FunctionNode)8 MathException (org.matheclipse.parser.client.math.MathException)6 PatternMatcher (org.matheclipse.core.patternmatching.PatternMatcher)5 SymbolNode (org.matheclipse.parser.client.ast.SymbolNode)5 IAST (org.matheclipse.core.interfaces.IAST)4 IOException (java.io.IOException)3 AST2Expr (org.matheclipse.core.convert.AST2Expr)3 NumberNode (org.matheclipse.parser.client.ast.NumberNode)3 FileReader (java.io.FileReader)2 ISymbol (org.matheclipse.core.interfaces.ISymbol)2 ArithmeticMathException (org.matheclipse.parser.client.math.ArithmeticMathException)2 InfixOperator (org.matheclipse.parser.client.operator.InfixOperator)2 PostfixOperator (org.matheclipse.parser.client.operator.PostfixOperator)2 PrefixOperator (org.matheclipse.parser.client.operator.PrefixOperator)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1