Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilding own C# compiler using ANTLR: Compilation Unit
    text
    copied!<p>// Create a scanner that reads from the input stream passed to us CSLexer lexer = new CSLexer(new ANTLRFileStream(f)); tokens.TokenSource = lexer;</p> <pre><code>// Create a parser that reads from the scanner CSParser parser = new CSParser(tokens); // start parsing at the compilationUnit rule CSParser.compilation_unit_return x = parser.compilation_unit(); object ast = x.Tree; </code></pre> <p>What can I do with the x which is of compilation_unit_return type, to extract its root , its classes, its methods etc? Do I have to extract its Adaptor out? How do I do that? Note that the compilation_unit_return is defined as such in my CSParser (which is automatically generated by ANTLR):</p> <pre><code>public class compilation_unit_return : ParserRuleReturnScope { private object tree; override public object Tree { get { return tree; } set { tree = (object) value; } } }; </code></pre> <p>However the tree I am getting is of the type object. I run using the debugger and seemed to see that it is of the type BaseTree. But BaseTree is an interface! I don't know how it relates to BaseTree and don't know how to extract details out from this tree. I need to write a visitor which has visit to its class, method, variables.... Tthe ParserRuleReturn class extends from RuleReturnScope and has a start and stop object, which I don't know what it is... Furthermore, there is this TreeVisitor class provided by ANTLR which looks confusing. It requires an Adaptor to be pass as a parameter to its constructor (if not it will use the default CommonTreeAdaptor), tt's why I asked abt the how to obtain the Adaptor eariler on. And other issues too... For the API, u can refer to <a href="http://www.antlr.org/api/CSharp/annotated.html" rel="nofollow noreferrer">http://www.antlr.org/api/CSharp/annotated.html</a></p> <p>Now I am struck here... If u do know anything, pls help out. Thanks a million.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload