Not Operator

Aug 11, 2011 at 11:16 PM

I am currently working on creating a grammar that checks logical expressions. I am having a difficult time getting the not operator (!) to work in expressions.

Here is the error message that I am getting:

Irony.Interpreter.RuntimeException : Object reference not set to an instance of an object.
  ----> System.NullReferenceException : Object reference not set to an instance of an object.

Here is the expression that I am using (working is a boolean value):

string expression = @"!(working)";

Here is my grammar:

            this.LanguageFlags |= LanguageFlags.CreateAst | LanguageFlags.CanRunSample;

            // 1. Terminals
            StringLiteral stringLiteral = TerminalFactory.CreateCSharpString("stringValue");
            StringLiteral charLiteral = TerminalFactory.CreateCSharpChar("charValue");
            NumberLiteral numberLiteral = TerminalFactory.CreateCSharpNumber("numberValue");
            IdentifierTerminal variableIdentifier = TerminalFactory.CreateCSharpIdentifier("variableValue");

            // 2. Non-terminals
            NonTerminal Term = new NonTerminal("Term");
            NonTerminal Expression = new NonTerminal("Expression");
            NonTerminal BinaryExpression = new NonTerminal("BinaryExpression", typeof(BinaryOperationNode));
            NonTerminal BinaryOperation = new NonTerminal("BinaryOperation");
            NonTerminal ParenthesesExpression = new NonTerminal("ParenthesesExpression");
            NonTerminal UnaryExpression = new NonTerminal("UnaryExpression", typeof(UnaryOperationNode));
            NonTerminal UnaryOperator = new NonTerminal("UnaryOperator");
            NonTerminal Statement = new NonTerminal("Statement");
            NonTerminal Program = new NonTerminal("Program", typeof(StatementListNode));

            // 3. Backus–Naur Form (BNF) rules
            Expression.Rule = Term | UnaryExpression | BinaryExpression;
            Term.Rule = stringLiteral | numberLiteral | charLiteral | variableIdentifier | ParenthesesExpression;
            ParenthesesExpression.Rule = "(" + Expression + ")";
            UnaryExpression.Rule = UnaryOperator + Term;
            UnaryOperator.Rule = ToTerm("!");
            BinaryExpression.Rule = Expression + BinaryOperation + Expression;
            BinaryOperation.Rule = ToTerm("==") | "!=" | ">" | ">=" | "<" | "<=" | "&&" | "||";
            Statement.Rule = Expression;
            Program.Rule = MakePlusRule(Program, NewLine, Statement);

            this.Root = Program;

            // 4. Operators precedence
            RegisterOperators(1, "||");
            RegisterOperators(2, "&&");
            RegisterOperators(3, "!");
            RegisterOperators(4, "==", "!=");//, "??", "$$");
            RegisterOperators(5, "<", "<=", ">", ">=");

            // 5. Punctuation and transient terms
            MarkPunctuation("(", ")");
            RegisterBracePair("(", ")");
            MarkTransient(Term, Expression, Statement, BinaryOperation, UnaryOperator, ParenthesesExpression);

Any help would be very much appreciated.

Aug 12, 2011 at 3:51 AM

try to debug thru it and see where it happens; try to get the idea why it happens once you know what and where. 

Aug 12, 2011 at 3:56 PM


I have been debugging this and I can't quite figure out what is going on. I have included the stack trace since I forgot it in my previous post.

Here is the stack trace:

System.NullReferenceException: Object reference not set to an instance of an object.
     at Irony.Interpreter.Ast.UnaryOperationNode.EvaluateNot(EvaluationContext context, AstMode mode) in C:\Irony_2011_07_05\Irony\Interpreter\Ast\Expressions\UnaryOperationNode.cs:line 59
     at Irony.Interpreter.Ast.AstNode.Evaluate(EvaluationContext context, AstMode mode) in C:\ThirdParty\Irony_2011_07_05\Irony\Interpreter\Ast\Base\AstNode.cs:line 61

Any help would be greatly appreciated.

Aug 12, 2011 at 9:55 PM

Well, the trouble with it - it is "old" interpreter you're hacking with, it's gone already (not in sources anymore) and soon will be replaced by completely rebuilt version. This old version was quick sample, not a real thing. Can it wait for a few weeks? To give you advice with old one, I'll just have to load old version from zip and debug thru it. Sorry don't have time right now, maybe in next 3-4 days. 


Aug 12, 2011 at 10:02 PM

Does the rebuilt version have support for the not operator (!)?

I can wait 3-4 days for help.

Aug 12, 2011 at 10:05 PM

New version support for NOT - yes. You can try to use the latest version from sources - I believe it does not require any changes in your grammar, see if it works