Saturday, June 9, 2012

Lizz's Calculator - Software Update

Reminder
As a reminder, I am building a custom calculator with an Arduino as a brain. This will use four linear time algorithms for pre-parsing the equation and a context free grammar to define valid syntax with a recursive descent parser to evaluate the equations. Note that each time the application is redesigned I am changing the version numbers so that I can more easily compare my documentation. Version 0.1 is implementing recursive pre-parsing and currently ignoring high precision requirements* for fast development and greater overall simplicity.

*This can (semi-)easily be fixed later using 3rd party libraries or implementing a custom library which implements big_floats by keeping track of a numerator and denominator, each in scientific notation.

Pre-Parsing
The pre-parsing code is complete, tested and committed to the repository.  The following algorithms serve to sanitize the input; ensuring the syntax passed to the recursive descent parser can be parsed by the grammar efficiently and accurately. The pre-parsing process is completed as follows.

  1. Parses a string (char*) and convert each equation element to a node inside a doubly linked list. Each number is parsed and converted to a float, each multi-character operation is converted to a single node with an identifying character e.g. SIN(...)->'s', LOG(...)->'g', etc. . 
  2. Once the equations is parsed into a linked lists, an algorithm uses a stack to ensure parentheses matching. While performing this check, an instance variable in each node containing a parentheses is set to specify its corresponding parentheses. 
  3. A third algorithm is used to surround each argument of two-argument operations with parentheses if necessary. This is done recursively keeping track of the insert position of the left parentheses and searching for operations to specify the position for the closing parentheses e.g. '+', '-', '*' and '/'. To ensure correct order of operations, this parsing is done on addition and subtraction, then re-evaluated on multiplication and division. Note that this step is not required for exponents or single argument operations e.g. SIN, COS, etc. . The algorithm includes checks to avoid duplicate or nested parentheses using the "match" instance variable, set in step 2, in each node containing a parentheses e.g. avoid "((3))+((4))".
Current UML
This will grow quickly as hardware is added; the project is still pending 128x64 pixel display, 5-bit 30-button array and power management including power button and low power state.



Evaluating The Equation
The current grammar is currently being redesigned as an LL(1) or LL(2) context free grammar. The current grammar is far too ambiguous to evaluate equations efficiently. I will post updates as progress is made, but I have multiple large research deadlines approaching in the next two weeks.

No comments:

Post a Comment