Download MetaTrader 5
To add comments, please log in or register
Join MQL5 Cloud Network. Start earning money!
Mihai Ionescu
1251
Mihai Ionescu 2015.06.05 20:09 

Is anyone aware of a library to parse string expressions, that can work with mql5?

I am trying right now muparser, but I'm struggling with importing (I don't know yet what i have to import and how to use).

But it's possible it will not work, as it requires a type which is a pointer to void:

typedef void*  muParserHandle_t;    // parser handle

//exported function
API_EXPORT(muParserHandle_t) mupCreate(int nBaseType);

//using it
hParser = mupCreate(muBASETYPE_FLOAT);

 

If it's not possible with muparser, is anyone aware of another possibility? another library?

adding variables like muparser would be great! 

Koldun Zloy
222
Koldun Zloy 2015.06.07 18:00  
Mihai Ionescu:

Is anyone aware of a library to parse string expressions, that can work with mql5?

I am trying right now muparser, but I'm struggling with importing (I don't know yet what i have to import and how to use).

But it's possible it will not work, as it requires a type which is a pointer to void:

 

If it's not possible with muparser, is anyone aware of another possibility? another library?

adding variables like muparser would be great! 

This is done as follows:

#define _WIN64  // Define only for the 64-bit terminal!


#ifdef _WIN64
#define PVOID ulong
#else
#define PVOID uint
#endif

#define muParserHandle_t PVOID

#import "muparser.dll"
   muParserHandle_t mupCreate( int nBaseType );
   void mupRelease( muParserHandle_t a_hParser );
#import

const int muBASETYPE_FLOAT  = 0;
const int muBASETYPE_INT    = 1;

void OnStart()
{
   muParserHandle_t hParser = mupCreate( muBASETYPE_FLOAT );         // initialize the parser

   ...
   
   mupRelease( hParser );                                           // free the parser ressources
}
Mihai Ionescu
1251
Mihai Ionescu 2015.06.08 22:43  

Thank you thank you thank you!

Just when I started to prepare to reinvent the wheel, by doing a (mini mini) parser from scratch in MQL5 :D 

I really appreciate your help!! 


I did a test, with "5+5", but it gives 5.0000 instead of 10.00000 

 

I wrote this (I added 3 more functions) 

#define _WIN64  // Define only for the 64-bit terminal!


#ifdef _WIN64
   #define PVOID ulong
#else
   #define PVOID uint
#endif

#define muParserHandle_t PVOID
#define muFloat_t double

//#import "calculator_dll.dll"
#import "muparser64.dll"
   muParserHandle_t mupCreate( int nBaseType );
              void mupRelease( muParserHandle_t a_hParser );
            string mupGetExpr( muParserHandle_t a_hParser);
              void mupSetExpr( muParserHandle_t a_hParser, string& a_szExpr);
            muFloat_t mupEval( muParserHandle_t a_hParser);
   //double calc_calculate(string);
#import

const int muBASETYPE_FLOAT  = 0;
const int muBASETYPE_INT    = 1;

 

And in a test script I did this:

 

  string formula = "5+5";
  muParserHandle_t hParser = mupCreate( muBASETYPE_FLOAT );         // initialize the parser
  mupSetExpr( hParser, formula);
  double res = mupEval( hParser );
  mupRelease( hParser );                                           // free the parser ressources

 

result 5.0000

What do you think is wrong?

 

And one more thingy, as i try to understand what's happening with hParser.

In muParserDLL.h is written:

typedef void*  muParserHandle_t;

And you made muParserHandle_t ulong (on 64bit).

So basically a pointer, the address, is a ulong?

And we use it in the same way as with a file handler, for example?

 

I'm so glad it's possible to use muparser!!! 

Mihai Ionescu
1251
Mihai Ionescu 2015.06.08 22:47  

I did few more tests,

and no matter what simple formula i give it, apparently only first digit counts.

"100" => 1.000

"23" => 2.0000

"(34+2)" => 0.0000

Koldun Zloy
222
Koldun Zloy 2015.06.09 17:48  
MQL uses Unicode strings and muparser uses ansi.
Here is an example to set and to get strings.
#import "Kernel32.dll"
   int lstrlen( PVOID );
   PVOID lstrcpy( uchar& str[], PVOID );
#import "muparser64.dll"
   muParserHandle_t mupCreate( int nBaseType );
   void mupRelease( muParserHandle_t a_hParser );
   void mupSetExpr( muParserHandle_t a_hParser, const char& a_szExpr[] );
   const PVOID mupGetExpr( muParserHandle_t a_hParser );
   double mupEval( muParserHandle_t a_hParser );
#import

void OnStart()
{
   muParserHandle_t hParser = mupCreate( muBASETYPE_FLOAT );         // initialize the parser
   
   string formula = "5+5";
   uchar ansiStr1[];
   StringToCharArray( formula, ansiStr1 );
   mupSetExpr( hParser, ansiStr1 );
   double res = mupEval( hParser );
   
   Print( res );
   
   PVOID pAnsiStr = mupGetExpr( hParser );
   int len = lstrlen( pAnsiStr );
   uchar resAnsiStr[];
   ArrayResize( resAnsiStr, len + 1 );
   lstrcpy( resAnsiStr, pAnsiStr );
   string result = CharArrayToString( resAnsiStr );
   
   Print( result );
   
   mupRelease( hParser );
}
Koldun Zloy
222
Koldun Zloy 2015.06.09 17:50  
And one more thingy, as i try to understand what's happening with hParser.

In muParserDLL.h is written:

And you made muParserHandle_t ulong (on 64bit).

So basically a pointer, the address, is a ulong?

And we use it in the same way as with a file handler, for example?

Yes. A pointer is just a number.

Mihai Ionescu
1251
Mihai Ionescu 2015.06.09 19:09  
Koldun Zloy:
MQL uses Unicode strings and muparser uses ansi.
Here is an example to set and to get strings.

Super!

I can't wait to try it 

Mihai Ionescu
1251
Mihai Ionescu 2015.06.09 19:11  
Koldun Zloy:

Yes. A pointer is just a number.

It makes more sense now, thanks for this clarification
Mihai Ionescu
1251
Mihai Ionescu 2015.06.09 20:16  

I tried the example => hurray!, it works!

 

The second part of the code, doesn't work, but I don't needed right now, I will debug it later.

I mean, result variable is empty. 

ArashPartow
5
ArashPartow 2015.06.13 07:09  

Here are some other options:

 

https://code.google.com/p/math-parser-benchmark-project/

math-parser-benchmark-project
math-parser-benchmark-project
  • code.google.com
This is a benchmark suite for different implementations of open source math expression parsers and evaluators written in C++. Currently the following expression parsers are part of...
Mihai Ionescu
1251
Mihai Ionescu 2015.06.15 13:19  

I saw it too, while researching.

I add one more link here:http://www.garybeene.com/reviews/rev-parsers.htm

/
To add comments, please log in or register