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 found here: http://www.codeproject.com/Articles/25069/JSBasic-A-BASIC-to-JavaScript-Compiler, but it seems that the Ast stuff has all been moved/removed. I found the Irony.Interpreter .dll, which has some Ast stuff in it, but it seems all tied up in the Expression sample implementation.
What am I missing here? I want to walk my tree and generate source code, and I'm not sure where to start.
I've seen some mention of using the visitor pattern, which I'm fine with, but I don't know how to implement it and run it in a way that Irony likes.
In Python, the ast. parse() function splits the source code into tokens based on the grammar. These tokens are then transformed to build an Abstract Syntax Tree (AST). It is a tree representation of a source code.
The AST expressions are conceptually divided into two classes: constructive expressions (those that can be used in parsing rules) and destructive expressions (those that can be used in pretty printing rules).
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); }
Init
(mostly on terminals) or InitChildren
(non-terminals) as appropriate. This is where the AST magic happens.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With