Creating a NumberLiteral Using TerminalFactory

Jan 6, 2012 at 10:27 PM


I think I may have found a bug with the TerminalFactory.CreateCSharpNumber method. It doesn't allow signs to begin with.

I had the following code:

NumberLiteral numberLiteral = TerminalFactory.CreateCSharpNumber("numberLiteral");

And the following unit test:

String expression = @"-300.0 == -300.0";
result = Expression.Evaluate<bool>(expression);


The unit test failed until I changed the above code to:

NumberLiteral numberLiteral = new NumberLiteral("numberLiteral", NumberOptions.AllowSign);

Jan 6, 2012 at 10:41 PM

It should not allow sign I think, unary minus is expressed in grammar as unary operation, explicitly

Jan 11, 2012 at 3:48 PM

It seems to me that if you are generating a C# number you should allow a sign since the C# language does allow positive and negative numbers.

Otherwise am I just using this method incorrectly?

Jan 11, 2012 at 4:38 PM

Languages like c# define unary operator +/- that can be applied not only to numbers but to expressions, like:


In this case, allowing signed numbers introduces ambiguity. How the string "-3" should be parsed? - as a signed number or as an unary expression "unop + expr", where expr is unsigned number "3"? 

To avoid this ambiguity, numbers in c# and most other languages are defined as unsigned. 

Why we need this signed flag for numbers then?

There are cases when language allows only numbers in some places, and number can be preceded with sign. In this case it would be a trouble to define an extra unary expression rule just for this, so Irony provides this shortcut.