Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# parser on C# that takes an input .cs file, read it, parse it and then prints a parse tree in console. Preprocessing is not included.
using Antlr4.Runtime;
using System;
class Program
{
static void Main(string[] args)
{
string fileName = args[0];
var code = System.IO.File.ReadAllText(fileName);
var codeStream = new AntlrInputStream(code);
var lexer = new CSharpLexer(codeStream);
var tokens = lexer.GetAllTokens();
var tokensSource = new ListTokenSource(tokens);
var tokensStream = new CommonTokenStream(tokensSource);
var parser = new CSharpParser(tokensStream);
ParserRuleContext ast = parser.compilation_unit();
var stringTree = ast.ToStringTree(parser);
Console.WriteLine("Tree {0}", stringTree);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment