The Roslyn project aims to expose the functionalities of C#/VB complier as API/Services. The Roslyn CTP was released this October.One of the important components of Roslyn is the Compiler API. This exposes an object model which provides access to information generated at the different stages of compilation.The first phase of any compilation process involves parsing and generating the syntax tree. In this post we will discuss about the basics of the Syntax Tree API.
The major components of a syntax tree are shown below:
A sample node in a code fragment is shown in the figure below:
In this post we will develop a small sample syntax tree walker to start with. But before that let’s take a quick look how the Roslyn Syntax API is structured for SyntaxNodes.
We will develop a syntax walker which will traverse the different declaration syntax nodes and print their text contents.
static void Main(string[] args)
{
SyntaxTree tree = SyntaxTree.ParseCompilationUnit(
@"
using System;
using System.Collections;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
var a = 10;
Console.WriteLine(""Hello, World!"");
}
public int i {get;set;}
var i = 11;
}
}");
var root = (CompilationUnitSyntax)tree.Root;
foreach (var n in root.Members)
{
if (n is NamespaceDeclarationSyntax)
{
foreach (var t in (n as NamespaceDeclarationSyntax).Members)
{
if (t is ClassDeclarationSyntax)
{
foreach (var m in (t as ClassDeclarationSyntax).Members)
{
if (m is PropertyDeclarationSyntax)
{
Console.WriteLine("Property::" + m.GetText());
}
if (m is MethodDeclarationSyntax)
{
Console.WriteLine("Method::" + m.GetText());
}
if (m is FieldDeclarationSyntax)
{
Console.WriteLine("Variable::" + m.GetText());
}
}
}
}
}
}
Console.Read();
}
The programs does the following:
- Invokes SyntaxTree.ParseCompilationUnit method with source code as input to generate SyntaxTree.
- Loops through all the members of the root node
- Checks the type of each member to determine the type of declaration syntax node and prints the contained text of that node.
The output is as shown below:
Now we will take a look into the different tokens of the method declaration. Every method has modifiers, return type, identifier and parameter list as shown below:
The code below shown how Roslyn Syntax API can be used to explore and walkthrough the same:
if (m is MethodDeclarationSyntax)
{
var meth = (m as MethodDeclarationSyntax);
foreach (var mt in (m as MethodDeclarationSyntax).Modifiers)
{
Console.WriteLine("Modifier::" + mt.GetText());
}
Console.WriteLine("Identifier::" + meth.ReturnType);
Console.WriteLine("Identifier::" + meth.Identifier);
foreach (var p in meth.ParameterList.Parameters)
{
Console.WriteLine("Parameter Type::" + p.TypeOpt.PlainName + "::" + p.TypeOpt.Kind );
Console.WriteLine("Parameter Identifier::" + p.Identifier);
}
}
The output now changes to:
In the next post we will how we can use Syntax API in source code analysis and checking.


Roslyn CTP–A Walk Through The Syntax Tree–Part I « Sankarsan’s Journal…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
[…] the last post related to Roslyn we developed a very basic syntax walker. In this post we will see how we can use […]
[…] The Roslyn project aims to expose the functionalities of C#/VB complier as API/Services. The Roslyn CTP was released this October.One of the important components of Roslyn is the Compiler API. This exposes an object model which provides access to information generated at the different stages of compilation.The first phase of any compilation process involves parsing and generating the syntax tree. In this post we will discuss about the basics of the Syntax Tree API. The major components of a syntax tree … .NET, C# Read the original post on DotNetShoutout… […]
[…] Roslyn CTP – A Walk Through The Syntax Tree – Part I & Part II – Sankarsan takes a look at the Roslyn CTP release, exploring the compiler API and exploring the use of syntax trees to allow you to explore code programmatically, looking at an example to show how you can use it for code analysis by looking for uses of singletons in your code. […]
Howdy! I know this is kinda off topic nevertheless I’d figured I’d ask.
Would you be interested in exchanging links or maybe guest
writing a blog article or vice-versa? My site goes over a lot of the same subjects as yours
and I feel we could greatly benefit from each other.
If you’re interested feel free to send me an e-mail. I look forward to hearing from you! Excellent blog by the way!