I have a grammar that works and parses in the Irony console just fine, but I don\'t get anything in the AST treeview. I was following along with the BASIC->Javascript article f
Check out the aptly named Sarcasm project for a reference implementation of a grammar, parser, and AST built on Irony. I found this blog entry by the author to be helpful in building the AST.
The following is a general purpose guide to getting the AST up and running.
MyBaseNode
) deriving from AstNode
(example). Copy/Paste the methods from the exampleFor each terminal and non-terminal create a new class derived from MyBaseNode
and
Accept
method (example): public override void Accept(IMyNodeVisitor visitor) { visitor.Visit(this); }
Add an interface IMyNodeVisitor
and add a Visit
method for each class defined in the previous step (example):
void Visit(MyDerivedNode1 node);
Set the ASTNodeType
for each of your terminals and non-terminals in your grammar from step 1.
For terminals - (example)
MyTerminal1.AstConfig.NodeType = typeof(MyDerivedNode1);
For non-terminals - (example)
var MyNonTerminal2 = new NonTerminal("MyNonTerminal2", typeof(MyDerivedNode2));
In the grammar enable AST creation: (example)
LanguageFlags = LanguageFlags.CreateAst;
In Irony parsing is done in 2 phases. First it creates a parse tree and then it creates your AST tree.
You are only seeing the first step. In order for Irony to create the AST you can:
Tell it how to to map your NonTerminals to AST nodes:
E.g. looking at the Irony sample grammer ExpressionEvaluatorGrammar we see:
var BinExpr = new NonTerminal("BinExpr", typeof(BinaryOperationNode));`
Here the we are telling Irony to map the BinExpr NonTerminal to a BinaryOperationNode which is our AST node.
Make it generate the AST when parsing:
When you set this flag the AST tree will be generated when you parse.
this.LanguageFlags = LanguageFlags.CreateAst;
The root of your AST tree will then be:
parseTree.Root.AstNode
I found this source a great starting point.