Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

An extensible math expression parser with plug-ins

Rate me:
Please Sign up or sign in to vote.
4.92/5 (147 votes)
13 Mar 2008CPOL51 min read 1.4M   29K   364  
Design and code for an extensible, maintainable, robust, and easy to use math parser.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>MTParserLib: MTCompilerPublicI Class Reference</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.4 -->
<div class="qindex"><a class="qindex" href="main.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div>
<h1>MTCompilerPublicI Class Reference</h1><!-- doxytag: class="MTCompilerPublicI" --><code>#include &lt;<a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a>&gt;</code>
<p>
<p>Inheritance diagram for MTCompilerPublicI:
<p><center><img src="classMTCompilerPublicI.png" usemap="#MTCompilerPublicI_map" border="0" alt=""></center>
<map name="MTCompilerPublicI_map">
<area href="classMTCompilerI.html" alt="MTCompilerI" shape="rect" coords="0,56,118,80">
<area href="classMTParser.html" alt="MTParser" shape="rect" coords="128,56,246,80">
<area href="classMTParserCompiler.html" alt="MTParserCompiler" shape="rect" coords="0,112,118,136">
</map>
<a href="classMTCompilerPublicI-members.html">List of all members.</a><hr><a name="_details"></a><h2>Detailed Description</h2>
Public Compiler Interface. 
<p>
Responsibilities:<ul>
<li>Take an expression in the infix notation and transform it to a stack whose items can be evaluated sequentially</li><li>Validate the expression syntax</li></ul>

<p>

<p>
Definition at line <a class="el" href="MTParserPublic_8h-source.html#l00598">598</a> of file <a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a>.<table border="0" cellpadding="0" cellspacing="0">
<tr><td></td></tr>
<tr><td colspan="2"><br><h2>Public Member Functions</h2></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual bool&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTCompilerPublicI.html#a0">isAutoVarDefinitionEnabled</a> () const =0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Query the automatic variable definition feature state.  <a href="#a0"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTCompilerPublicI.html#a1">compile</a> (const MTCHAR *expr)=0  throw (MTParserException)</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Compile the expression to speed up the following evaluations.  <a href="#a1"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual bool&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTCompilerPublicI.html#a2">isConstant</a> () const =0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Return whether the expression is constant or not.  <a href="#a2"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual unsigned int&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTCompilerPublicI.html#a3">getNbUsedVars</a> () const =0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Get the number of used variables.  <a href="#a3"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual MTSTRING&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTCompilerPublicI.html#a4">getUsedVar</a> (unsigned int index) const =0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Get a used variable symbol.  <a href="#a4"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="a5"></a><!-- doxytag: member="MTCompilerPublicI::getExpression" ref="a5" args="() const =0" -->
virtual MTSTRING&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTCompilerPublicI.html#a5">getExpression</a> () const =0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Get the current expression. <br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top">virtual void&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTCompilerPublicI.html#a6">resetExpression</a> ()=0</td></tr>

<tr><td class="mdescLeft">&nbsp;</td><td class="mdescRight">Set a null expression.  <a href="#a6"></a><br></td></tr>
<tr><td class="memItemLeft" nowrap align="right" valign="top"><a class="anchor" name="a7"></a><!-- doxytag: member="MTCompilerPublicI::~MTCompilerPublicI" ref="a7" args="()" -->
virtual&nbsp;</td><td class="memItemRight" valign="bottom"><a class="el" href="classMTCompilerPublicI.html#a7">~MTCompilerPublicI</a> ()</td></tr>

</table>
<hr><h2>Member Function Documentation</h2>
<a class="anchor" name="a1"></a><!-- doxytag: member="MTCompilerPublicI::compile" ref="a1" args="(const MTCHAR *expr)=0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual void MTCompilerPublicI::compile           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top">const MTCHAR *&nbsp;</td>
          <td class="mdname1" valign="top" nowrap> <em>expr</em>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap>  throw (<a class="el" href="classMTChainedExceptions.html">MTParserException</a>)<code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Compile the expression to speed up the following evaluations. 
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>expr</em>&nbsp;</td><td>A mathematical expression</td></tr>
  </table>
</dl>

<p>
Implemented in <a class="el" href="classMTParser.html#z3_1">MTParser</a>, and <a class="el" href="classMTParserCompiler.html#a1">MTParserCompiler</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a3"></a><!-- doxytag: member="MTCompilerPublicI::getNbUsedVars" ref="a3" args="() const =0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual unsigned int MTCompilerPublicI::getNbUsedVars           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap> const<code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Get the number of used variables. 
<p>
For example, if variables x, y and z are defined and the expression is x+2, then there is only one used variable: x.<p>
<dl compact><dt><b>Returns:</b></dt><dd>The number of defined variables</dd></dl>

<p>
Implemented in <a class="el" href="classMTParser.html#z4_9">MTParser</a>, and <a class="el" href="classMTParserCompiler.html#a4">MTParserCompiler</a>.
<p>
Referenced by <a class="el" href="MTParser_8cpp-source.html#l00314">MTParser::getNbUsedVars()</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a4"></a><!-- doxytag: member="MTCompilerPublicI::getUsedVar" ref="a4" args="(unsigned int index) const =0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual MTSTRING MTCompilerPublicI::getUsedVar           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="md" nowrap valign="top">unsigned int&nbsp;</td>
          <td class="mdname1" valign="top" nowrap> <em>index</em>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap> const<code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Get a used variable symbol. 
<p>
<dl compact><dt><b>Parameters:</b></dt><dd>
  <table border="0" cellspacing="2" cellpadding="0">
    <tr><td valign="top"></td><td valign="top"><em>index</em>&nbsp;</td><td>The requested used variable. Range from 0 to the number of used variables-1.</td></tr>
  </table>
</dl>
<dl compact><dt><b>Returns:</b></dt><dd>Used variable symbol</dd></dl>

<p>
Implemented in <a class="el" href="classMTParser.html#z4_10">MTParser</a>, and <a class="el" href="classMTParserCompiler.html#a5">MTParserCompiler</a>.
<p>
Referenced by <a class="el" href="MTParser_8cpp-source.html#l00319">MTParser::getUsedVar()</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a0"></a><!-- doxytag: member="MTCompilerPublicI::isAutoVarDefinitionEnabled" ref="a0" args="() const =0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual bool MTCompilerPublicI::isAutoVarDefinitionEnabled           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap> const<code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Query the automatic variable definition feature state. 
<p>
<dl compact><dt><b>Returns:</b></dt><dd>Return true if the automatic variable definition feature is on</dd></dl>

<p>
Implemented in <a class="el" href="classMTParser.html#z4_12">MTParser</a>, and <a class="el" href="classMTParserCompiler.html#a0">MTParserCompiler</a>.
<p>
Referenced by <a class="el" href="MTParser_8cpp-source.html#l00232">MTParser::isAutoVarDefinitionEnabled()</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a2"></a><!-- doxytag: member="MTCompilerPublicI::isConstant" ref="a2" args="() const =0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual bool MTCompilerPublicI::isConstant           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap> const<code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Return whether the expression is constant or not. 
<p>
A constant expression returns the same result all times it is evaluated.
<p>
Implemented in <a class="el" href="classMTParser.html#z3_4">MTParser</a>, and <a class="el" href="classMTParserCompiler.html#a2">MTParserCompiler</a>.
<p>
Referenced by <a class="el" href="MTParser_8cpp-source.html#l00566">MTParser::isConstant()</a>.    </td>
  </tr>
</table>
<a class="anchor" name="a6"></a><!-- doxytag: member="MTCompilerPublicI::resetExpression" ref="a6" args="()=0" --><p>
<table class="mdTable" cellpadding="2" cellspacing="0">
  <tr>
    <td class="mdRow">
      <table cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td class="md" nowrap valign="top">virtual void MTCompilerPublicI::resetExpression           </td>
          <td class="md" valign="top">(&nbsp;</td>
          <td class="mdname1" valign="top" nowrap>          </td>
          <td class="md" valign="top">&nbsp;)&nbsp;</td>
          <td class="md" nowrap><code> [pure virtual]</code></td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<table cellspacing="5" cellpadding="0" border="0">
  <tr>
    <td>
      &nbsp;
    </td>
    <td>

<p>
Set a null expression. 
<p>
Note: Evaluating a null expression always returns NaN.
<p>
Implemented in <a class="el" href="classMTParser.html#z3_8">MTParser</a>, and <a class="el" href="classMTParserCompiler.html#a7">MTParserCompiler</a>.
<p>
Referenced by <a class="el" href="MTParser_8cpp-source.html#l00554">MTParser::resetExpression()</a>, and <a class="el" href="MTParser_8cpp-source.html#l00293">MTParser::undefineAllVars()</a>.    </td>
  </tr>
</table>
<hr>The documentation for this class was generated from the following file:<ul>
<li><a class="el" href="MTParserPublic_8h-source.html">MTParserPublic.h</a></ul>
<hr size="1"><address style="align: right;"><small>Generated on Sun Mar 9 17:39:36 2008 for MTParserLib by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.4 </small></address>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
Software Engineer working at a fun and smart startup company

Comments and Discussions