Click here to Skip to main content
Licence 
First Posted 14 Feb 2004
Views 34,581
Downloads 492
Bookmarked 13 times

Expression Evaluator

By Werdna | 14 Feb 2004 | Unedited contribution
Evaluate expressions in C#. Simple introduction to building interpreter.
11 votes, 55.0%
1
2 votes, 10.0%
2

3
3 votes, 15.0%
4
4 votes, 20.0%
5
1.73/5 - 20 votes
4 removed
μ 1.93, σa 3.02 [?]

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 PinmemberDanieBeckett22:41 21 Jun '11  
GeneralMy vote of 2 PinmemberJoe McRay9:55 17 May '09  
QuestionCan this code be used in commercial applications freely? Pinmemberfarshad nourozi18:43 24 Sep '08  
AnswerRe: Can this code be used in commercial applications freely? PinmemberWerdna3:57 25 Sep '08  
QuestionOwn parser ? PinmemberWizard_0121:24 15 Feb '04  
AnswerRe: Own parser ? PinmemberWerdna21:28 15 Feb '04  
GeneralArticle is lacking PinmemberRick York19:32 15 Feb '04  
GeneralRe: Article is lacking PinmemberWerdna19:33 15 Feb '04  
GeneralRe: Article is lacking PinsitebuilderUwe Keim21:03 15 Feb '04  
GeneralRe: Article is lacking PinmemberLairton Ballin2:56 16 Feb '04  
GeneralRe: Article is lacking PinsitebuilderUwe Keim3:21 16 Feb '04  
GeneralRe: Article is lacking PinmemberWerdna4:05 16 Feb '04  
GeneralRe: Article is lacking Pinmemberjmarcost7:11 5 May '08  

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
Web03 | 2.5.120209.1 | Last Updated 15 Feb 2004
Article Copyright 2004 by Werdna
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid