Some Things to Try


As mentioned early on in this chapter, only minimal error checking is performed by the parser. You might want to add detailed error reporting. For example, you could highlight the point in the expression at which an error was detected. This would allow the user to easily find and correct a syntax error.

As the parser now stands, it can evaluate only numeric expressions. However, with a few additions, you can make it evaluate other types of expressions, such as strings, spatial coordinates, or complex numbers. For example, to allow the parser to evaluate string objects, you must make the following changes:

  1. Define a new token type called STRING.

  2. Enhance GetToken( ) so that it recognizes strings.

  3. Add a new case inside Atom( ) that handles STRING type tokens.

  4. Adjust EvalExp1( ) and EvalExp2( ) to operate on strings.

After implementing these steps, the parser could handle string expressions like these:

 a = “one” b = “two” c = a + b

The result in c should be the concatenation of a and b, or “onetwo”.

Here is one good application for the parser: Create a simple, pop-up mini-calculator that accepts an expression entered by the user and then displays the result. This would make an excellent addition to nearly any commercial application.

Finally, try converting Parser into a component. This is easy to do. First, have Parser inherit Component. Then, implement Dispose(bool). That’s it! If you do this, you will have a parser available to any application you write.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net