Click here to Skip to main content
Licence 
First Posted 14 Feb 2004
Views 35,455
Bookmarked 13 times

Expression Evaluator

By | 14 Feb 2004 | Article
Evaluate expressions in C#. Simple introduction to building interpreter.

Introduction

What this example shows:
- how to take input string and execute it as expression
- how to tokenize a string into tokens (lexer class implemented as Tokenizer.cs)
- how to take series of tokens from lexer and build abstract syntax tree (AST).
This is a very simple parser, implemented as AParser.cs
- how to take AST and evaluate it as expression. This also allows
variable assignment and usage and function calls.
- how to build pretty printer class to print back expressions

Operators supported:
binary operators: +, -, *, /
unary operators: +, -
parenthesis grouping
built in functions: sin, cos, abs, pow, sqrt, exp, log (easy to add more functions in Evaluator.cs)
type: exit to quit program

Example usage:
x = sin(3.3) + 6 * sqrt(23)
y = cos(0.343)
pow(x, 3) + y / 3

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Werdna

Chief Technology Officer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionLicense PinmemberDanieBeckett21:41 21 Jun '11  
GeneralMy vote of 2 PinmemberJoe McRay8:55 17 May '09  
QuestionCan this code be used in commercial applications freely? Pinmemberfarshad nourozi17:43 24 Sep '08  
AnswerRe: Can this code be used in commercial applications freely? PinmemberWerdna2:57 25 Sep '08  
QuestionOwn parser ? PinmemberWizard_0120:24 15 Feb '04  
AnswerRe: Own parser ? PinmemberWerdna20:28 15 Feb '04  
GeneralArticle is lacking PinmemberRick York18:32 15 Feb '04  
GeneralRe: Article is lacking PinmemberWerdna18:33 15 Feb '04  
GeneralRe: Article is lacking PinsitebuilderUwe Keim20:03 15 Feb '04  
GeneralRe: Article is lacking PinmemberLairton Ballin1:56 16 Feb '04  
GeneralRe: Article is lacking PinsitebuilderUwe Keim2:21 16 Feb '04  
GeneralRe: Article is lacking PinmemberWerdna3:05 16 Feb '04  
GeneralRe: Article is lacking Pinmemberjmarcost6:11 5 May '08  
I agree: This is not the best article I've read. But the code was really useful for me.
 
Thanks a lot!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 15 Feb 2004
Article Copyright 2004 by Werdna
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid