ParseTreeNode.AstNode is NULL after parse

Dec 30, 2009 at 5:33 PM

Hi,

 

I finally managed to implement the complete grammar for my language.

Next step for me would be to get a feeling for the ParseTree / AST.  But the AstNode is NULL and I can't figure out why, even after reading through the ExpressionEvaluator Sample, GrammarExplorer sources etc.

Could anyone please give me a hint ?

Parser tParser = new Parser(tGrammar);

            
            ParseTree tTree = tParser.Parse(tExpression);
            cLogger.Debug("can parse:" + tParser.Language.CanParse());

            //tGrammar.RunSample(tTree);
            tTree = tParser.Context.CurrentParseTree;
            
            foreach (ParserMessage tMsg in tTree.ParserMessages)
                cLogger.Warn("message:" + tMsg);

            cLogger.Debug("root:" + tTree.Root);

            foreach (ParseTreeNode tChild in tTree.Root.ChildNodes)
            {
                cLogger.Debug("child:" + tChild.Token + "|");
            }


Coordinator
Dec 30, 2009 at 5:51 PM

You should do 2 things:

1. Specify AstNodeType for every non-terminal in the grammar (except those marked as transient)

2. Set the flag CreateAstTree in Grammar.Language flags.

See expressionEvaluator grammar

 

Jan 1, 2010 at 10:39 AM

Thank you for your ongoing help!

I have yet another question:

The ParseTreeNode.AstNode property gets set, but AstNode.ChildNodes returns an empty list.

So I can't traverse the AST, but have to rely on the ParseTree instead.

 

How can I initialize the children?

Coordinator
Jan 2, 2010 at 12:09 AM

Well, that's the responsibility of AstNode itself (Parent AST node) - to fill it's child nodes collection from child ParseTreeNodes - each contains the AST node already when parent node is created; this code should be in IAstNode.Init implementation. Look at standard AST nodes, you'll get the idea

Roman