代码着色器错误

时间:2013-09-05 06:01:45

标签: c# parsing abstract-syntax-tree irony

我正在尝试编写一个语法来解析像

这样的语句
b.a := 7;

我的语法如下所示

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("(", ")");
    }
}

当我尝试解析

时,它工作正常
b := 7;

出现错误Fatal Error in code colorizer. Colorizing had been disabled.即使我发表评论

LanguageFlags = LanguageFlags.CreateAst;

错误仍然存​​在。 详细错误是

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: 

有人可以帮我解决错误吗? 我正在使用Irony_2013_03_10版本。

1 个答案:

答案 0 :(得分:0)

应该为所有非终结符添加Ast节点类型,瞬态除外。在每个Ast节点类中重写Init()和DoEvalute()函数。详细信息可在this discussion

中找到