Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27187

DDMathParser create expression from NSString

I have an NSString:

n + (n - m)

Can I use DDMathParser for create an expression object.

For example I have base expression n + (n - m), but I need to have ability compare base expression n + (n - m) with an user expression e.g. (n - m) + n that typically the same as well.

Or is there alredy made solution how to compare two NSString considering signs and braces?

So I mean can I for example init two expressions with a string and then compare it like Expression1 = Expression2 using some method.

This is example how expressions have to look in parser

enter image description here

so this is the same struct and we can compare it using left and right node. of the tree. I am interesting to find out this solution that will parse string to expression tree. I think this solution was alredy made.

Upvotes: 0

Views: 361

Answers (1)

Dave DeLong
Dave DeLong

Reputation: 243156

Yes, you can use DDMathParser to do this:

NSString *string = @"5 + (5 - 3)";
NSError *error = nil;
DDExpression *expression = [DDExpression expressionFromString:string error:&error];

if (expression) {
  NSLog(@"%@", expression)
} else {
  NSLog(@"%@", error);
}

DDMathParser does have some (rudimentary) support for expression rewriting, but other than that it'd be up to you to compare the expression trees yourself.

Upvotes: 1

Related Questions