An AST from a ParseTree

Oct 4, 2009 at 4:45 PM

hi there,

I've spent the last few hours with Irony and until now have stuck with the ParseTree, traversing manually and emitting as I go etc.

However I would like to explore creating an AST. Unfortunately all I've found in the way of examples / instructions (CodeProject articles) refer to deprecated versions of the api.

My problem is that I'm unable to properly initialise my custom AstNodes for ParseTreeNodes so that they contain the corresponding child node data.

I have created custom node classed inheriting from AstNode (OrExprNode & AndExprNode in the code below).

My grammar is set to create an Ast when setting things up:

 var grammar = new MyGrammar();
 grammar.LanguageFlags |= LanguageFlags.CreateAst;
 var languageData = new LanguageData(grammar);
 _parser = new Parser(languageData);

and I've tried both

var orExpression = new NonTerminal("orExpression", typeof(OrExprNode));
var andExpression = new NonTerminal("andExpression", typeof(AndExprNode));

and

 

var orExpression = new NonTerminal("orExpression",
                                   (context, parseNode) =>
                                   {
                                       var astNode = new OrExprNode();
                                       astNode.Init(context, parseNode);
                                   });
var andExpression = new NonTerminal("andExpression",
                                    (context, parseNode) =>
                                    {
                                        var astNode = new AndExprNode();
                                        astNode.Init(context, parseNode);
                                    });

 

in my grammar constructor yet in both cases, after parsing, the AstNode property of the root ParseTreeNode is set but the AstNode is not initialised with any of the token child nodes from the root.

I've had a look at the SchemeGrammar in the source (as suggested elsewhere) but found the AstNodeCreators' bodies commented out, I assume because they use AstNode constructor overloads which are not in the api anymore.

It would be great if someone could give me a hint on how to create and initialise my custom AstNode subclasses properly so I can dive a little deeper.

Thanks for the great work, I am thoroughly enjoying the project.

crs

 

 

 

 

var orExpression = new NonTerminal("orExpression",
                                   (context, parseNode) =>
                                   {
                                       var astNode = new OrExprNode();
                                       astNode.Init(context, parseNode);
                                   });
var andExpression = new NonTerminal("andExpression",
                                    (context, parseNode) =>
                                    {
                                        var astNode = new AndExprNode();
                                        astNode.Init(context, parseNode);
                                    });
Coordinator
Oct 4, 2009 at 4:50 PM

Look at ExpressionEvaluator grammar, it has this stuff fully working. Otherwise, I don't see why it's not working, I would need more info

Roman