Irony - Parse files from separate class file fails

Jul 25, 2012 at 1:04 PM

Hi Folks,

I have a problem regarding the parsing of files through Irony while I tried to outsoource my parsing function to a secondary class. What I want to do is nothing very complicated, I just want to get the Parse Tree of files as a first step.

But when I moved

public static void parseFile(string file, bool report = true)
        {
            Grammar g = new CSharpGrammar(); // full c# 3.0 grammar (w/o linq, preprocessor queries)
            LanguageData language = new LanguageData(g);
            Parser parser = new Parser(language);
            ParseTree parseTree = parser.Parse(file);
            //ParseTreeNode root = parseTree.Root;
            //ParseRoot = null;
            ParseRoot = parseTree.Root;
            //toolStripStatusLabel1.Text = (root == null ? "ERROR: Parse tree unsuccessful!" : "Parse tree created!");
            if (ParseRoot == null)
            { // root
                Console.Out.WriteLine("ERR: " + file);
                //return;
            }
            else
            {
                //mainForm.toolStripStatusLabel1.Text = "test";
                Console.Out.WriteLine("SUC: " + file);
            };

            Console.Out.WriteLine(ParseRoot.ChildNodes[1].ChildNodes[0].ChildNodes.Count());

            if(report == true) mainForm.backgroundWorker2.ReportProgress(50);//(100 * i) / nlc);
        }

 

I am getting no error ,the program runs, but is not able to determine the ParseRoot, it is always "null".

Do you have an idea what I may improve to solve this problem?

Greetz,

Markus

Coordinator
Jul 25, 2012 at 4:55 PM

Before reading ParseTree.Root, check for errors in ParseTree object. With a new language grammar - always start with verifying/debugging it in grammar explorer.

Another thing - 'language = new LanguageData(g)' - this statement create parsing automaton from grammar. This is very expensive operation, you should create languageData once and save it somewhere in global field and reuse then.

Jul 26, 2012 at 8:02 AM

Thanks for this hint, I fixed the bug regarding LanguageData. But my problem remains. When I do the parsing in Form1.cs, evething goes right. But when I try to call

ParseTree parseTree = mainForm.parser.Parse(file);
ParseRoot = parseTree.Root;
if (ParseRoot == null)
{
  Console.Out.WriteLine("ERR: " + file);
}
else
{
  Console.Out.WriteLine("SUC: " + file);
};

from another class file, then the ParseRoot is null.

Do you have any suggestion? How may I debug the ParseTree?

Coordinator
Jul 26, 2012 at 4:48 PM

have no idea, but it seems some simple mismatch in your code. Try to step through in debugger and see what's different in both scenarios

Roman