Click here to Skip to main content
6,305,776 members and growing! (17,690 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Expression Evaluator

By Werdna

Evaluate expressions in C#. Simple introduction to building interpreter.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:14 Feb 2004
Views:26,032
Bookmarked:8 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
20 votes for this article.
Popularity: 2.51 Rating: 1.93 out of 5
11 votes, 55.0%
1
2 votes, 10.0%
2

3
3 votes, 15.0%
4
4 votes, 20.0%
5

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


Member

Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralMy vote of 2 PinmemberJoe McRay9:55 17 May '09  
GeneralCan this code be used in commercial applications freely? Pinmemberfarshad nourozi18:43 24 Sep '08  
GeneralRe: Can this code be used in commercial applications freely? PinmemberWerdna3:57 25 Sep '08  
GeneralOwn parser ? PinmemberWizard_0121:24 15 Feb '04  
GeneralRe: 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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Feb 2004
Editor:
Copyright 2004 by Werdna
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project