![]() |
Languages »
C# »
General
Intermediate
Expression EvaluatorBy WerdnaEvaluate expressions in C#. Simple introduction to building interpreter. |
C#, Windows, .NET 1.1VS.NET2003, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Feb 2004 Editor: |
Copyright 2004 by Werdna Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |