PDA

View Full Version : Please help on QRegExp



lni
22nd September 2011, 08:03
Hi,

I need to create a Qt script dynamically from user input formular using QRegExp. I got completely lose. Please help.

I have a list of QVector<float> a, aa, b, etc... which I can transfer to QScriptEngine.

I have a QLineEdit to allow user to enter their own formular to compute a new one, for instance, use can enter the following string (case insensitive):

math.max(a)/math.min(aa)*b+aa/b-Math.pow(aa)/a/MATH.pow(a,2.14)

I need to convert this string into QtScript dynamically, so it should become:



var max_a = max( a ); // max is a global function somewher in my script library
var min_aa = min( aa ); // min is a global function somewher in my script library

var result = new Array;
for ( var idx = 0; idx < a.length; idx++ ) {
result[ idx ] = max_a / min_aa * b[ idx ] + aa[ idx ] / b[ idx ] - Math.pow( aa[ idx ] ) / a[ idx ] / Math.pow( a[ idx ], 2.14 );
}


I believe I need to use QRegExp, but am unable to get it done.

Many thanks!!

mentalmushroom
22nd September 2011, 09:35
I think you should implement you own algorithm for this task. You can find many samples on the web.

amleto
22nd September 2011, 21:37
not really sure why you want to use regex there?

ChrisW67
23rd September 2011, 08:51
So, you want to parse (http://en.wikipedia.org/wiki/Parse) this:


math.max(a)/math.min(aa)*b+aa/b-Math.pow(aa)/a/MATH.pow(a,2.14)

looking for instances of some arbitrary and ambiguous variable names and substitute something in place of each. At the very least you need some rules to decide the boundary of a variable name (which may be the same as the word boundary (\b) test in regular expressions). Then you need to test for each possible variable name in the string, with its boundary conditions, and replace it when found. Given that this is user input your really should verify that the syntax is good.

When you have tried to write a simple, standalone program that attempts this come back here with the program and the actual thing the gets you completely lost.

wysota
24th September 2011, 23:10
To be honest I don't understand what you want to convert here. The script you mention looks like a perfectly valid JavaScript. Why not simply execute it (after assigning proper values to "a", "aa", etc.)?

lni
25th September 2011, 07:12
To be honest I don't understand what you want to convert here. The script you mention looks like a perfectly valid JavaScript. Why not simply execute it (after assigning proper values to "a", "aa", etc.)?

Could you try this code and switch between "if 0" and "if 1"? Thanks!



#include <QScriptEngine>
#include <QCoreApplication>
#include <QDebug>

static void addToEngine( QScriptEngine& engine, const QString& varName, const QVector<float>& val )
{
QScriptValue result = engine.newArray( val.size() );
for ( int idx = 0; idx < val.size(); idx++ ) {
result.setProperty( idx, val[ idx ] );
}
engine.globalObject().setProperty( varName, result );
}

int main( int argc, char** argv )
{
QCoreApplication app( argc, argv );

QVector<float> a( 5 ), aa( 5 );
for ( int idx = 0; idx < 5; idx++ ) {
a[ idx ] = idx;
aa[ idx ] = idx + 10;
}

QScriptEngine engine;

addToEngine( engine, "a", a );
addToEngine( engine, "aa", aa );

#if 1
QScriptValue result = engine.evaluate( "d = a * aa;" );
#else
QScriptValue result = engine.evaluate( "\n\
var d = new Array; \n\
for ( var idx=0; idx < a.length; idx++ ) {\n\
d[idx] = a[idx] * aa[idx]; \n\
}" );
#endif

qDebug() << result.toString();

QScriptValue dVal = engine.globalObject().property( "d" );
qDebug() << dVal.toString();

}

wysota
25th September 2011, 07:42
What's your point? JavaScript doesn't define vector operations so obviously trying to do vector multiplication fails. How is that related to your problem? "a" and "aa" in your script should point to values, not vectors.