This project has moved and is read-only. For the latest updates, please go here.

Identifiers with embedded spaces

Feb 1, 2014 at 1:08 PM
I've looked at several threads here, including this one:

and I'm not sure how to proceed.

I am creating a custom grammar for calculations. Some of the terms I want to use in the calculations are variables whose names may have spaces embedded within them. In the Variable names are enclosed with braces {}, and their values are looked up externally for use in the calculation.

Here's a simplified example of a statement I'd like to use in my calculator:
1+MyFunction({A Variable With Spaces})+3
So far, I'm doing OK as long as I only use variables whose names don't have spaces. This, for example, works fine:
The grammar code for my variable is currently like this:
  var myVariableName = new IdentifierTerminal("VariableName");
  var myVariableExpr = new NonTerminal("MyVariableExpr", typeof(MyVariableNode));
  var myVariableExpr.Rule = "{" + myVariableName + "}";
I sense from the response in that instead of being an IdentifierTerminal, I need to subclass it or make my own version that allows embedded spaces - but I'm not sure how to do that, and let's just say I'm not the strongest C# programmer in the world. I can go by examples pretty well though.

Can anyone point me in the right direction?
Feb 2, 2014 at 11:58 AM
Update: I took a suggestion I found here and decided to preprocess the equation to ensure there were no embedded spaces in the variable names. (It's just as well, because it turns out I have other problematic characters in the stream as well, so I just deal with them all now.) I can then reverse the preprocessing at the point when the identifiers have to be resolved to actual values for computation.

That will get me going for now, but it seems like an inelegant approach. If there's a relatively straightforward way to "do it right" I'm interested in learning it.