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.
- Define your grammar (example)
- Create an abstract base class (
MyBaseNode
) deriving fromAstNode
(example). Copy/Paste the methods from the example For each terminal and non-terminal create a new class derived from
MyBaseNode
and- Override
Accept
method (example):
public override void Accept(IMyNodeVisitor visitor) { visitor.Visit(this); }
- Override
Init
(mostly on terminals) orInitChildren
(non-terminals) as appropriate. This is where the AST magic happens.
- Override
Add an interface
IMyNodeVisitor
and add aVisit
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.In the grammar enable AST creation: (example)
LanguageFlags = LanguageFlags.CreateAst;