Playing with expression tokens

MathParser.org - mXparser - logo - Tutorial

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

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

JAVA code

Case 1: Printing expression tokens

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("1+2^3-sin(10+cos(20-1))+30");
mXparser.consolePrintTokens(e.getCopyOfInitialTokens());
[mXparser-v.4.0.0]  --------------------
[mXparser-v.4.0.0] | Expression tokens: |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] |    TokenIdx |       Token |        KeyW |     TokenId | TokenTypeId |  TokenLevel |  TokenValue |   LooksLike |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] |           0 |           1 |       _num_ |           1 |           0 |           0 |         1.0 |             |
[mXparser-v.4.0.0] |           1 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           2 |           2 |       _num_ |           1 |           0 |           0 |         2.0 |             |
[mXparser-v.4.0.0] |           3 |           ^ |           ^ |           5 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           4 |           3 |       _num_ |           1 |           0 |           0 |         3.0 |             |
[mXparser-v.4.0.0] |           5 |           - |           - |           2 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           6 |         sin |         sin |           1 |           4 |           1 |         NaN |             |
[mXparser-v.4.0.0] |           7 |           ( |           ( |           1 |          20 |           2 |         NaN |             |
[mXparser-v.4.0.0] |           8 |          10 |       _num_ |           1 |           0 |           2 |        10.0 |             |
[mXparser-v.4.0.0] |           9 |           + |           + |           1 |           1 |           2 |         NaN |             |
[mXparser-v.4.0.0] |          10 |         cos |         cos |           2 |           4 |           3 |         NaN |             |
[mXparser-v.4.0.0] |          11 |           ( |           ( |           1 |          20 |           4 |         NaN |             |
[mXparser-v.4.0.0] |          12 |          20 |       _num_ |           1 |           0 |           4 |        20.0 |             |
[mXparser-v.4.0.0] |          13 |           - |           - |           2 |           1 |           4 |         NaN |             |
[mXparser-v.4.0.0] |          14 |           1 |       _num_ |           1 |           0 |           4 |         1.0 |             |
[mXparser-v.4.0.0] |          15 |           ) |           ) |           2 |          20 |           4 |         NaN |             |
[mXparser-v.4.0.0] |          16 |           ) |           ) |           2 |          20 |           2 |         NaN |             |
[mXparser-v.4.0.0] |          17 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |          18 |          30 |       _num_ |           1 |           0 |           0 |        30.0 |             |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] 

Case 2: Using tokens to print expression in a fancy way

import org.mariuszgromada.math.mxparser.*;
...
/* Expression definition */
Expression e = new Expression("1+2^3-sin(10+cos(20-1))+30");
/* Getting list of tokens */
ArrayList<Token> tokens = e.getCopyOfInitialTokens();
/* Evaluating max level */
int mxlev = 0;
for (Token t : tokens)
	if (t.tokenLevel > mxlev) mxlev = t.tokenLevel;
/* Printing tokens */
mXparser.consolePrintln(e.getExpressionString());
mXparser.consolePrintln("--------------------------");
for (int level = 0; level <= mxlev; level++) {
	for (Token t : tokens) {
		String s;
		if (t.tokenLevel == level) s = t.tokenStr;
		else {
			s = "";
			for (int i = 0; i < t.tokenStr.length(); i++)
				s = s + " ";
		}
		mXparser.consolePrint(s);
	}
	mXparser.consolePrintln();
}
[mXparser-v.3.0.0] 1+2^3-sin(10+cos(20-1))+30
[mXparser-v.3.0.0] --------------------------
[mXparser-v.3.0.0] 1+2^3-                 +30
[mXparser-v.3.0.0]       sin                 
[mXparser-v.3.0.0]          (10+         )   
[mXparser-v.3.0.0]              cos          
[mXparser-v.3.0.0]                 (20-1)   

Case 3: Playing with invalid tokens

import org.mariuszgromada.math.mxparser.*;
...
Expression e = new Expression("pi+var1-fun1(var1)+fun2(var2)-1pi");
mXparser.consolePrintTokens(e.getCopyOfInitialTokens());

Please check "Looks Like" field

[mXparser-v.4.0.0]  --------------------
[mXparser-v.4.0.0] | Expression tokens: |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] |    TokenIdx |       Token |        KeyW |     TokenId | TokenTypeId |  TokenLevel |  TokenValue |   LooksLike |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] |           0 |          pi |          pi |           1 |           9 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           1 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           2 |        var1 |             |          -1 |          -1 |           0 |         NaN |    argument |
[mXparser-v.4.0.0] |           3 |           - |           - |           2 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           4 |        fun1 |             |          -1 |          -1 |           0 |         NaN |    function |
[mXparser-v.4.0.0] |           5 |           ( |           ( |           1 |          20 |           1 |         NaN |             |
[mXparser-v.4.0.0] |           6 |        var1 |             |          -1 |          -1 |           1 |         NaN |    argument |
[mXparser-v.4.0.0] |           7 |           ) |           ) |           2 |          20 |           1 |         NaN |             |
[mXparser-v.4.0.0] |           8 |           + |           + |           1 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |           9 |        fun2 |             |          -1 |          -1 |           0 |         NaN |    function |
[mXparser-v.4.0.0] |          10 |           ( |           ( |           1 |          20 |           1 |         NaN |             |
[mXparser-v.4.0.0] |          11 |        var2 |             |          -1 |          -1 |           1 |         NaN |    argument |
[mXparser-v.4.0.0] |          12 |           ) |           ) |           2 |          20 |           1 |         NaN |             |
[mXparser-v.4.0.0] |          13 |           - |           - |           2 |           1 |           0 |         NaN |             |
[mXparser-v.4.0.0] |          14 |         1pi |             |          -1 |          -1 |           0 |         NaN |       error |
[mXparser-v.4.0.0]  ---------------------------------------------------------------------------------------------------------------
[mXparser-v.4.0.0] 

*** 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