Click here to Skip to main content
13,259,539 members (52,648 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 31 Mar 2009

C# RPN Expression

, 31 Mar 2009
Rate this:
Please Sign up or sign in to vote.
Calculate expressions with variables and functions using RPN.


This article describes a simple but powerful expression parser using RPN. Expressions know how to deal with variables and functions. Additional functions can also be implemented.


The calculations are based on RPN - Reverse Polish Notation. Read more about RPN here:

Using the code

The usage is very simple. Just create an environment and an expression, supply the expression string and variables, and calculate.

The environment is used to register the functions. Sometimes, functions are not needed so there so no need to register them.

ExprEnvironment environment = new ExprEnvironment();

RPNExpr expr = new RPNExpr();
expr.Environment = environment;
expr.AddVariable(new Variable("X", 4));
expr.AddVariable(new Variable("Y", 5));
label5.Text = expr.GetValue().ToString();

Once an expression is prepared, you may call GetValue many times with different variables. The expression will just go through the stack and calculate the value rather than parse the whole expression again.

expr.VariableByName("X").value = 9;
expr.VariableByName("Y").value = 3;
label5.Text = expr.GetValue().ToString();

The RPN stack can be extracted from the list and shown to the user.

Points of interest

Try to implement your own function. It's very easy, you can find an example in the demo. Data fields can also be implemented. If you use something like [MyField] in an expression string, the expression will call the environment to return the value. You need to inherit the environment and you are ready to use the expression with data fields.

History and thanks

I should mention that RPNExpression was first written in Delphi and then rewritten to C# with some help from my friends Gregor Jakša and Matjaž Oman.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Slovenia Slovenia
No Biography provided

You may also be interested in...

Comments and Discussions

Generalplease fix this bug Pin
lidujun200817-Jan-10 16:15
memberlidujun200817-Jan-10 16:15 
Generalserious bug [modified] Pin
hoanganh17b@gmail.com1-Sep-09 6:23
memberhoanganh17b@gmail.com1-Sep-09 6:23 
General[My vote of 1] This code has a show-stopper bug Pin
aaava6-May-09 10:00
memberaaava6-May-09 10:00 
GeneralArticle Pin
Mato221-Apr-09 21:24
memberMato221-Apr-09 21:24 
GeneralMy vote of 2 Pin
Pablo Robert1-Apr-09 3:04
memberPablo Robert1-Apr-09 3:04 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 31 Mar 2009
Article Copyright 2009 by odisej
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid