SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

AST, Visitor and CompilationUnit

Last post 06-27-2008 2:30 PM by punkdevil. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 06-25-2008 3:23 PM

    AST, Visitor and CompilationUnit

    Hello,

     i have written a Lexer/Parser for a specific language.

    But i don't know how to build the compilationUnit and how to walk throw the compilationUnit or AST.

    Can anyone help? 

  • 06-25-2008 3:27 PM In reply to

    Re: AST, Visitor and CompilationUnit

    A visitor is just a pattern for walking through a tree where the nodes have different object types without requiring huge switch-statements for the node types. (basically it emulates double-dispatch by doing two virtual dispatches)

    What are you trying to do? Did you create your own AST or are you using NRefactory? What kind of compilation unit are you trying to build (NRefactory/SharpDevelop.Dom/something else)?

    Daniel Grunwald
  • 06-25-2008 3:37 PM In reply to

    Re: AST, Visitor and CompilationUnit

    Hello Daniel, 

     

    I want to use my own AST.

    I use the class CompilationUnit from SharpDevelop. The nodes of the AST have to implement the INode interface.

    I figured out, that i can add nodes to the AST with compilationUnit.AddChild(). For a block of statements i had to use compilationUnit.BlockStart() and BlockEnd().

     

    The question is:

    How should i implement a visitor?

    How can i walk through the AST?

  • 06-26-2008 7:59 PM In reply to

    Re: AST, Visitor and CompilationUnit

    If you use your own AST, there's no point in using the CompilationUnit class from NRefactory (what features do you think would you get from SharpDevelop?).

    You can read about the visitor pattern on Wikipedia: http://en.wikipedia.org/wiki/Visitor_pattern

    Daniel Grunwald
  • 06-27-2008 8:18 AM In reply to

    Re: AST, Visitor and CompilationUnit

     Do i need the CompilationUnit, for what it is good?

  • 06-27-2008 12:23 PM In reply to

    Re: AST, Visitor and CompilationUnit

     It's just the root node of the NRefactory AST.

    You don't need it for your own AST.

    Daniel Grunwald
  • 06-27-2008 1:06 PM In reply to

    Re: AST, Visitor and CompilationUnit

     Thank you so far.

     

    I decide to use the NRefactory.AST.

    Can you explain how i use (in the parser) CompilationUnit and NRefactory.AST correctly? 

  • 06-27-2008 1:45 PM In reply to

    Re: AST, Visitor and CompilationUnit

    What language are you parsing? Using the NRefactory AST will limit you to C# / VB idioms.

    Just build the AST... CompilationUnit is the root node, in its Children collection put the UsingDeclarations and TypeDeclarations, in the TypeDeclaration's children put MethodDeclarations etc.

    There's a sample program in SharpDevelop\samples\NRefactoryDemo (in the source code download) where you can parse C# and VB code and see the resulting AST; you can use that to learn about the AST (which class is used to represent which construct etc.)

    Daniel Grunwald
  • 06-27-2008 2:30 PM In reply to

    Re: AST, Visitor and CompilationUnit

     I have my own language which i have to parse.

    I will do it similar to SharpDevelop, so i can learn from the implementation in SharpDevelop. 

Page 1 of 1 (9 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.