I am trying to write a grammar to parse a statement like
b.a := 7;
My grammar is as shown below
public class STGrammar : InterpretedLanguageGrammar
{
public STGrammar() : base(caseSensitive: false)
{
LanguageFlags = LanguageFlags.CreateAst;
# region Terminals
Terminal n = new NumberLiteral("number");
Terminal v = new IdentifierTerminal("variable");
KeyTerm assignment = ToTerm(":=");
KeyTerm left_paren = ToTerm("(");
KeyTerm right_paren = ToTerm(")");
KeyTerm terminate = ToTerm(";");
KeyTerm left_sq = ToTerm("[");
KeyTerm right_sq = ToTerm("]");
KeyTerm dot = ToTerm(".");
#endregion Terminals
#region Nonterminals
NonTerminal actionBlock = new NonTerminal("actionBlock", typeof(ActionBlock));
NonTerminal assignmentExpr = new NonTerminal("assignmentExpr", typeof(AssignmentStNode));
NonTerminal arrayOrStruct = new NonTerminal("arrayOrStruct", typeof(ArrayNode));
NonTerminal arrayOrStructRHS = new NonTerminal("arrayOrStructRHS", typeof(AstNode));
NonTerminal arrayOrStructRHSs = new NonTerminal("arrayOrStructRHSs");
NonTerminal comparatorExp = new NonTerminal("comparatorExpression", typeof(ExpressionNode));
#endregion Nonterminals
#region BNFRules
actionBlock.Rule =
assignmentExpr + terminate
| arrayOrStruct + terminate
;
assignmentExpr.Rule =
v + assignment + v
| v + assignment + n
| arrayOrStruct + assignment + v
| v + assignment + arrayOrStruct
| arrayOrStruct + assignment + arrayOrStruct
| arrayOrStruct + assignment + n
;
arrayOrStruct.Rule =
v + arrayOrStructRHSs
;
arrayOrStructRHS.Rule =
"." + v
| "[" + v + "]"
;
arrayOrStructRHSs.Rule =
MakePlusRule(arrayOrStructRHSs, arrayOrStructRHS)
;
this.Root = actionBlock;
#endregion BNFRules
MarkPunctuation(",", ";", "[", "]", ")", "(", "..", ".");
RegisterBracePair("(", ")");
}
}
It works fine when I am trying to parse
b := 7;
The error coming is Fatal Error in code colorizer. Colorizing had been disabled.
Even if I comment out
LanguageFlags = LanguageFlags.CreateAst;
the error still persists. The detailed error is
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=Irony
StackTrace:
at Irony.Ast.AstBuilder.BuildAst(ParseTreeNode parseNode) in C:\Sreeja\downloads\Irony_2013_03_10\Irony\Ast\AstBuilder.cs:line 97
at Irony.Ast.AstBuilder.BuildAst(ParseTreeNode parseNode) in C:\Sreeja\downloads\Irony_2013_03_10\Irony\Ast\AstBuilder.cs:line 86
at Irony.Ast.AstBuilder.BuildAst(ParseTreeNode parseNode) in C:\Sreeja\downloads\Irony_2013_03_10\Irony\Ast\AstBuilder.cs:line 86
at Irony.Ast.AstBuilder.BuildAst(ParseTreeNode parseNode) in C:\Sreeja\downloads\Irony_2013_03_10\Irony\Ast\AstBuilder.cs:line 86
at Irony.Ast.AstBuilder.BuildAst(ParseTree parseTree) in C:\Sreeja\downloads\Irony_2013_03_10\Irony\Ast\AstBuilder.cs:line 38
at Irony.Interpreter.InterpretedLanguageGrammar.BuildAst(LanguageData language, ParseTree parseTree) in C:\Sreeja\downloads\Irony_2013_03_10\Irony.Interpreter\InterpretedLanguageGrammar.cs:line 60
at Irony.Parsing.Parser.Parse(String sourceText, String fileName) in C:\Sreeja\downloads\Irony_2013_03_10\Irony\Parsing\Parser\Parser.cs:line 88
at Irony.GrammarExplorer.fmGrammarExplorer.ParseSample() in C:\Sreeja\downloads\Irony_2013_03_10\Irony.GrammarExplorer\fmGrammarExplorer.cs:line 348
at Irony.GrammarExplorer.fmGrammarExplorer.btnParse_Click(Object sender, EventArgs e) in C:\Sreeja\downloads\Irony_2013_03_10\Irony.GrammarExplorer\fmGrammarExplorer.cs:line 507
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
Can someone help me to resolve the error? I am using Irony_2013_03_10 version.
One should add Ast node type for all nonterminals, except transient ones. In each Ast node class override Init() and DoEvalute() functions. The details can be found in this discussion