Built-in tokens (key words) manipulation

mXparser - logo

Did you find the software useful? Please consider donation 🙂
Developing and maintaining MathParser.org-mXparser takes a lot of time, mainly my free time. I hope it saved some of your time. If yes, then buy me a coffee 🙂
DONATE

JAVA code

Case 1: Removing built-in tokens

import org.mariuszgromada.math.mxparser.*;
...
/*
 * Removing built-in tokens methods:
 *
 * mXparser.removeBuiltinTokens(String... tokens)
 * mXparser.unremoveBuiltinTokens(String... tokens)
 * mXparser.unremoveAllBuiltinTokens()
 * mXparser.getBuiltinTokensToRemove()
 */
Expression e;
/*
 * Example 1: Before tokens removal
 */
e = new Expression("sin(pi)+cos(pi)");
e.checkSyntax();
mXparser.consolePrintln("*** Example 1: Before tokens removal");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 2: Removing "sin", "cos" tokens
 */
mXparser.removeBuiltinTokens("sin", "cos");
e = new Expression("sin(pi)+cos(pi)");
e.checkSyntax();
mXparser.consolePrintln("*** Example 2: Removing \"sin\", \"cos\" tokens");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 3: Printing removed built-in tokens
 */
mXparser.consolePrint("*** Example 4: Printing removed built-in tokens : ");
for (String t : mXparser.getBuiltinTokensToRemove() )
	mXparser.consolePrint(t + ", ");
mXparser.consolePrintln();
mXparser.consolePrintln();
/*
 * Example 4: Un-removing built-in token
 */
mXparser.unremoveBuiltinTokens("cos");
e.checkSyntax();
mXparser.consolePrintln("*** Example 4: Un-removing built-in token");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 5: Un-removing all built-in token
 */
mXparser.unremoveAllBuiltinTokens();
e.checkSyntax();
mXparser.consolePrintln("*** Example 5: Un-removing all built-in token");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());

Result:

[mXparser-v.4.1.0] *** Example 1: Before tokens removal
[mXparser-v.4.1.0] 

[sin(pi)+cos(pi)] checking ...
[sin(pi)+cos(pi)] no errors.

[mXparser-v.4.1.0] *** Example 2: Removing "sin", "cos" tokens
[mXparser-v.4.1.0] 

[sin(pi)+cos(pi)] checking ...
[sin(pi)+cos(pi)] (sin, 0) invalid <TOKEN>.
[sin(pi)+cos(pi)] (cos, 5) invalid <TOKEN>.
[sin(pi)+cos(pi)] errors were found.

[mXparser-v.4.1.0] *** Example 4: Printing removed built-in tokens : sin, cos, 
[mXparser-v.4.1.0] 
[mXparser-v.4.1.0] *** Example 4: Un-removing built-in token
[mXparser-v.4.1.0] 

[sin(pi)+cos(pi)] checking ...
[sin(pi)+cos(pi)] (sin, 0) invalid <TOKEN>.
[sin(pi)+cos(pi)] errors were found.

[mXparser-v.4.1.0] *** Example 5: Un-removing all built-in token
[mXparser-v.4.1.0] 

[sin(pi)+cos(pi)] checking ...
[sin(pi)+cos(pi)] no errors.

Case 2: Modifying built-in tokens

/*
 * Modifying built-in tokens methods:
 *
 * mXparser.modifyBuiltinToken(String currentToken, String newToken)
 * mXparser.modifyBuiltinToken(String currentToken, String newToken, String newTokenDescription)
 * mXparser.unmodifyBuiltinTokens(String... currentOrNewTokens)
 * mXparser.unmodifyAllBuiltinTokens()
 * mXparser.getBuiltinTokensToModify()
 */
Expression e;
/*
 * Example 1: "sine" and "cosine" are not defined
 */
e = new Expression("sine(pi)+cosine(pi)");
e.checkSyntax();
mXparser.consolePrintln("*** Example 1: \"sine\" and \"cosine\" are not defined");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 2: Changing "sin" to "sine"
 */
mXparser.modifyBuiltinToken("sin", "sine");
e.checkSyntax();
mXparser.consolePrintln("*** Example 2: Changing \"sin\" to \"sine\"");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 3: Changing "cos" to "cosine" + setting new description
 */
mXparser.modifyBuiltinToken("cos", "cosine", "My new cosine token");
e.checkSyntax();
mXparser.consolePrintln("*** Example 3: Changing \"cos\" to \"cosine\" + setting new description");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
mXparser.consolePrintHelp("My new cosine");
mXparser.consolePrintln();
/*
 * Example 4: Printing modified built-in tokens
 */
mXparser.consolePrintln("*** Example 4: Printing modified built-in tokens: ");
String[][] modifiedTokens = mXparser.getBuiltinTokensToModify();
mXparser.consolePrintln();
for (int i = 0; i < modifiedTokens.length; i++) {
	String currentToken = modifiedTokens[i][0];
	String newToken = modifiedTokens[i][1];
	String newTokenDescription = modifiedTokens[i][2];
	mXparser.consolePrintln(currentToken + " -> " + newToken + ", " + newTokenDescription);
}
mXparser.consolePrintln();
/*
 * Example 5: Un-modifying "sin" token
 */
e = new Expression("sine(pi)+cosine(pi)");
mXparser.unmodifyBuiltinTokens("sin");
e.checkSyntax();
mXparser.consolePrintln("*** Example 5: Un-modifying \"sin\" token");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 6: Un-modifying all modified token
 */
mXparser.unmodifyAllBuiltinTokens();
e.checkSyntax();
mXparser.consolePrintln("*** Example 6: Un-modifying all modified token");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());

Result:

[mXparser-v.4.1.0] *** Example 1: "sine" and "cosine" are not defined
[mXparser-v.4.1.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] (sine, 0) invalid <TOKEN>.
[sine(pi)+cosine(pi)] (cosine, 5) invalid <TOKEN>.
[sine(pi)+cosine(pi)] errors were found.

[mXparser-v.4.1.0] *** Example 2: Changing "sin" to "sine"
[mXparser-v.4.1.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] (cosine, 5) invalid <TOKEN>.
[sine(pi)+cosine(pi)] errors were found.

[mXparser-v.4.1.0] *** Example 3: Changing "cos" to "cosine" + setting new description
[mXparser-v.4.1.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] no errors.

[mXparser-v.4.1.0] Help content: 

    #  key word            type                    syntax                                        since description
    -  --------            ----                    ------                                        ----- -----------
   39. cosine              <Unary Function>        cosine(x)                                     1.0   My new cosine token


[mXparser-v.4.1.0] *** Example 4: Printing modified built-in tokens: 
[mXparser-v.4.1.0] 
[mXparser-v.4.1.0] sin -> sine, null
[mXparser-v.4.1.0] cos -> cosine, My new cosine token
[mXparser-v.4.1.0] 
[mXparser-v.4.1.0] *** Example 5: Un-modifying "sin" token
[mXparser-v.4.1.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] (sine, 0) invalid <TOKEN>.
[sine(pi)+cosine(pi)] errors were found.

[mXparser-v.4.1.0] *** Example 6: Un-modifying all modified token
[mXparser-v.4.1.0] 

[sine(pi)+cosine(pi)] checking ...
[sine(pi)+cosine(pi)] (sine, 0) invalid <TOKEN>.
[sine(pi)+cosine(pi)] (cosine, 5) invalid <TOKEN>.
[sine(pi)+cosine(pi)] errors were found.

Case 3: Overriding built-in tokens

/*
 * Overriding built-in tokens methods:
 *
 * mXparser.setToOverrideBuiltinTokens();
 * mXparser.setNotToOverrideBuiltinTokens();
 * mXparser.checkIfsetToOverrideBuiltinTokens();
 */
Expression e;
/*
 * Example 1: user defined function and user defined constant with name conflict
 */
Function sin = new Function("sin(x) = 2*x");
Constant pi = new Constant("pi = 1");
e = new Expression("sin(pi)", sin, pi);
e.checkSyntax();
mXparser.consolePrintln("*** Example 1: user defined function and user defined constant with name conflict");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
/*
 * Example 2: Setting option to override built-in tokens
 */
mXparser.setToOverrideBuiltinTokens();
e.checkSyntax();
mXparser.consolePrintln("*** Example 2: Setting option to override built-in tokens");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());
mXparser.consolePrintln();
mXparser.consolePrintln(e.getExpressionString() + " = " + e.calculate());
/*
 * Example 3: Checking override built-in tokens option
 */
mXparser.consolePrintln();
mXparser.consolePrintln("*** Example 3: Checking override built-in tokens option");
mXparser.consolePrintln(" Option = " + mXparser.checkIfsetToOverrideBuiltinTokens());
/*
 * Example 4: Setting option not to override built-in tokens
 */
mXparser.consolePrintln();
e = new Expression("sin(pi)", sin, pi);
mXparser.setNotToOverrideBuiltinTokens();
e.checkSyntax();
mXparser.consolePrintln("*** Example 4: Setting option not to override built-in tokens");
mXparser.consolePrintln("\n\n" + e.getErrorMessage());

Result:

[mXparser-v.4.1.0] *** Example 1: user defined function and user defined constant with name conflict
[mXparser-v.4.1.0] 

[sin(pi)] checking ...
[sin(pi)] (pi) Duplicated <KEYWORD>.
[sin(pi)] (sin) Duplicated <KEYWORD>.
[sin(pi)] errors were found.

[mXparser-v.4.1.0] *** Example 2: Setting option to override built-in tokens
[mXparser-v.4.1.0] 

[sin(pi)] checking ...
[sin(pi)] (sin, 0) checking user defined function ...
[sin(pi)] -> [sin] = [ 2*x] checking ...
[sin(pi)] -> [sin] = [ 2*x] no errors.
[sin(pi)] no errors.

[mXparser-v.4.1.0] 
[mXparser-v.4.1.0] sin(pi) = 2.0
[mXparser-v.4.1.0] 
[mXparser-v.4.1.0] *** Example 3: Checking override built-in tokens option
[mXparser-v.4.1.0]  Option = true
[mXparser-v.4.1.0] 
[mXparser-v.4.1.0] *** Example 4: Setting option not to override built-in tokens
[mXparser-v.4.1.0] 

[sin(pi)] checking ...
[sin(pi)] (pi) Duplicated <KEYWORD>.
[sin(pi)] (sin) Duplicated <KEYWORD>.
[sin(pi)] errors were found.

*** If you found the software useful donation is something you might consider 🙂 ***

If you found the software useful donation is something you might consider :-)

Enjoy! 🙂

 

Best regards,

Mariusz Gromada

Download latest release – v.4.1.1 Aeries: bin + doc + src (.zip 13.4 MB)

Download latest release – v.4.1.1 Aeries: bin only (.zip 4.35 MB) – separate binaries for java: 1.5, 1.6, 1.7, 1.8; .NET: 2.0, 3.0, 3.5, 4.0, 4.5, 4.6, 4.7, .NET Core, .NET Standard, .NET PCL, Xamarin.Android, Xamarin.iOS

Source code .zipSource code .tar.gz View on GitHubMathSpace.pl