Parser - Extending


The parser shown is simplistic but illustrates the kinds of issues that must be addressed.  It can be easily extended.

Obviously you can add operators ( ^ could mean raise to the power ) trivially.

The real power comes when you extend the parser to handle variables.  In the real world, I have used the above approach to handle variables as complex as time series.  Typically you store "variables" in a linked list that specifies each variables type (scalar, time series, etc.).    Obviously the complexity increases but the hard work is done - GNP/5 is not that much different from 3/5.

Adding non-numeric data types adds another layer of complexity.  Rules for conversions are needed, etc.

Adding functions is more difficult.  Providing robust error-handling virtually requires some mechanism for prototyping functions as to arguments and return values.  If you can limit the nature of functions (unary functions that return an item of the same type), the task is not so onerous. 

Finally, note that the linked lists are analogous to "compiled" code.  For performance, you can store the lists for repeated use...