Click here to Skip to main content
15,885,309 members
Articles / Programming Languages / C#
Article

Expression Evaluator

Rate me:
Please Sign up or sign in to vote.
1.53/5 (21 votes)
14 Feb 2004 48.5K   759   15   13
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


Written By
Chief Technology Officer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLicense Pin
DanieBeckett21-Jun-11 21:41
DanieBeckett21-Jun-11 21:41 
GeneralMy vote of 2 Pin
Joe McRay17-May-09 8:55
Joe McRay17-May-09 8:55 
QuestionCan this code be used in commercial applications freely? Pin
farshad nourozi24-Sep-08 17:43
farshad nourozi24-Sep-08 17:43 
AnswerRe: Can this code be used in commercial applications freely? Pin
Werdna25-Sep-08 2:57
Werdna25-Sep-08 2:57 
QuestionOwn parser ? Pin
Wizard_0115-Feb-04 20:24
Wizard_0115-Feb-04 20:24 
AnswerRe: Own parser ? Pin
Werdna15-Feb-04 20:28
Werdna15-Feb-04 20:28 
GeneralArticle is lacking Pin
Rick York15-Feb-04 18:32
mveRick York15-Feb-04 18:32 
GeneralRe: Article is lacking Pin
Werdna15-Feb-04 18:33
Werdna15-Feb-04 18:33 
GeneralRe: Article is lacking Pin
Uwe Keim15-Feb-04 20:03
sitebuilderUwe Keim15-Feb-04 20:03 
GeneralRe: Article is lacking Pin
Lairton Ballin16-Feb-04 1:56
professionalLairton Ballin16-Feb-04 1:56 
GeneralRe: Article is lacking Pin
Uwe Keim16-Feb-04 2:21
sitebuilderUwe Keim16-Feb-04 2:21 
GeneralRe: Article is lacking Pin
Werdna16-Feb-04 3:05
Werdna16-Feb-04 3:05 
GeneralRe: Article is lacking Pin
jmarcost5-May-08 6:11
jmarcost5-May-08 6:11 

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

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