Posted by nano (nano), 15 September 2006Hi guys
Anyone out there have a neat way to parse an arithmetic expression in C++.
It could be like command line expr ( A * B ) / 2
Now I have the Arithmetic methods written that is multiple and add
but need a neat way to parse the expression to pass the values into the Arithmetic methods ...
Any ideas out there .
all ideas appreciated
Posted by admin (Graham Ellis), 15 September 2006I would use the regex library ....
Typically, the best way to parse arithmetic expressions such as you're writing about is to search for all the alphanumeric blocks and split the incoming stream into tokens based on those, then analyse each token. I've done this is Perl ... starting with
@tokens = split(/(\w+)/,$line);
to give you a list of tokens - note the capture brackets around the regular expression which means that it will be saved into the @tokens list.
You then need to handle the brackets, etc, through a dijkstra shunt. It's a long time since I wrote one of those!
We are running a Regular Expression Course on 13th October 2006 - a great place to learn if you've not come across them before, and we do have space available.
Note for anyone reading this page AFTER 13th October - click on the course link to find later dates; it's a regular course
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho