Author Topic: One of my creations: Creating an Algebraic Parser in C++/wxWidgets  (Read 6305 times)

0 Members and 1 Guest are viewing this topic.

Offline Nosferatu Arucard 1983

  • LV2 Member (Next: 40)
  • **
  • Posts: 28
  • Rating: +6/-0
    • View Profile
One of my creations: Creating an Algebraic Parser in C++/wxWidgets
« on: September 28, 2016, 09:06:42 pm »
This code belongs to one of my programs that I created  during my Ph.D. and are an Algebraic Parser library which enable the user input any algebraic expression and return a numerical value.  O.O

My implementation supports numbers in standard or scientific notation, 16 functions (12 trigonometric and 4 exponential/logarithmic), and also support variable assignments (it can store values on main memory  O.O)

The program use the wxWidgets framework, and the string manipulation uses the internal wxString type, which are more powerful than the STL library. Only the STL <cmath> was used to handle the arithmetic calculations.

The testing program is a kind of scientific calculator, although some buttons don't work as expect as the real job here was to test the new parser library for a PIXE program dedicated to handle nuclear stoichiometry by gamma ray yields  >:D

- To use the application, just fill any numerical expression and hit the "Return" button, the result will pop up on a Message Box.

 The program should evaluate some expressions like:
 2+6^5+sqrt(2+5/acos(0.5))
 To give the result: 7780.602815

 And using the variable feature, it can parse expressions like the following one:
 pi = 2*acos(0) , x = 2 * pi , a = 1 , b = a + 5 / sqrt(pi) : cos(x) + a*b
 Returning: 4.820948

 If you make a mistake, an error message will be present by a pop up window....
 pi = 2*acos(0) , x = 2 * pie , a = 1 , b = a + 5 / sqrt(pi) : cos(x) + a*b
 Giving the message:
 Declaration Error: Undefined Variable!

 - The algorithm I implemented is basically a sequence of three major steps:
 1. Lexical parsing, using an algorithm that somehow is partially my own invention. It uses some tricks to handle the parsing of numbers in scientific notation, and classifies the new tokens against a list of built-in words. On final, the original string are converted by a vector of tokens.
 2. The Shunting-Yard algorithm that convert the previous list of tokens to a postfix notation, while store any variables to a variable vector and checks if are correctly defined, and make the firsts syntax checks. Any error, like the previous step, will stop all process and displays the adequate error message.
 3. The postfix calculator, with some syntax checks, is the last step of whole process. Any arithmetic evaluation are done on this step, and if everything are correct, it will return the final result.

 The source code was bundled on the attachments, and the parser algorithm is implemented on CalculatorLibrary.  :)