467,146 Members | 1,329 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

How do I make a simple C# expression "pre-parser"?

In my application the user can configure automation-scripts by inserting
different "actions" into a "procedure". These different procedure- and
action-objects are all translated into C# code before execution.

One "action" type is an expression-evaluator. At the moment the expression
the user writes into the action is just inserted into the generated C# code
unchanged.

The problem is the variables in my system and in the "procedures"; they are
not just the basic .NET types but my own types with more properties and
methods.

This means that the expression for incrementing the variable "myHeight"
looks like this: (myHeight.Value)++

I would like the user to be able to write: myHeight++
and my "pre-processor" would then convert it to: (myHeight.Value)++

I could just run through the expression and replace every occurrence of a
variable name with: (varname.Value)
but that would also take any occurrences of a variable name inside a string
constant or an object property or method with the same name as a variable.

Any suggestions how I could do this?
- I have no experience with parser- or compiler-construction.

Cheers,
Jan
May 16 '07 #1
  • viewed: 2533
Share:
4 Replies
Why not let your users add @ in front of all variables and introduce that as
a standard.
That should facilitate parsing the text.

Robert

"Jan" <j0****************@world.dkwrote in message
news:AC**********************************@microsof t.com...
In my application the user can configure automation-scripts by inserting
different "actions" into a "procedure". These different procedure- and
action-objects are all translated into C# code before execution.

One "action" type is an expression-evaluator. At the moment the expression
the user writes into the action is just inserted into the generated C#
code
unchanged.

The problem is the variables in my system and in the "procedures"; they
are
not just the basic .NET types but my own types with more properties and
methods.

This means that the expression for incrementing the variable "myHeight"
looks like this: (myHeight.Value)++

I would like the user to be able to write: myHeight++
and my "pre-processor" would then convert it to: (myHeight.Value)++

I could just run through the expression and replace every occurrence of a
variable name with: (varname.Value)
but that would also take any occurrences of a variable name inside a
string
constant or an object property or method with the same name as a variable.

Any suggestions how I could do this?
- I have no experience with parser- or compiler-construction.

Cheers,
Jan

May 16 '07 #2
That could be a way to make a simple solution. If I ever thought about that,
I completely forgot about it again.

Thanks for your input!

"robert" wrote:
Why not let your users add @ in front of all variables and introduce that as
a standard.
That should facilitate parsing the text.

Robert
May 16 '07 #3
If you need a more robust solution, consider writing a parser. A simple
depth-first one, in C#, can be found at
http://www.frontiernet.net/~fredm/dps/Contents.htm , chapter 3. A more
robust one is at
http://www.frontiernet.net/~fredm/pa...istWebPage.htm .

If these are a little too complex, consider a recursive descent parser.
They are simple to write:
http://www.cs.wm.edu/~noonan/java/parsing/
"Jan" <j0****************@world.dkwrote in message
news:02**********************************@microsof t.com...
That could be a way to make a simple solution. If I ever thought about
that,
I completely forgot about it again.

Thanks for your input!

"robert" wrote:
>Why not let your users add @ in front of all variables and introduce that
as
a standard.
That should facilitate parsing the text.

Robert

May 16 '07 #4
Thank you very much Robert. I will definately have a look at that.

The best solution would absolutely be the one my users like the most and my
personal preference is C++/C#/Java-syntax-like expressions.
That would also give the simplest code gereration for me.

Thanks again.
Jan

"Fred Mellender" wrote:
If you need a more robust solution, consider writing a parser. A simple
depth-first one, in C#, can be found at
http://www.frontiernet.net/~fredm/dps/Contents.htm , chapter 3. A more
robust one is at
http://www.frontiernet.net/~fredm/pa...istWebPage.htm .

If these are a little too complex, consider a recursive descent parser.
They are simple to write:
http://www.cs.wm.edu/~noonan/java/parsing/
"Jan" <j0****************@world.dkwrote in message
news:02**********************************@microsof t.com...
That could be a way to make a simple solution. If I ever thought about
that,
I completely forgot about it again.

Thanks for your input!

"robert" wrote:
Why not let your users add @ in front of all variables and introduce that
as
a standard.
That should facilitate parsing the text.

Robert


May 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by kelvin | last post: by
134 posts views Thread by James A. Donald | last post: by
32 posts views Thread by Stephen | last post: by
48 posts views Thread by mahurshi@gmail.com | last post: by
1 post views Thread by Alex Maghen | last post: by
30 posts views Thread by josh | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.