Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C# parser on Java (ANTLR 4.7) that take an input .cs file, read it, parse it and then prints a parse tree in console. Preprocessing is not included.
import org.antlr.v4.runtime.*;
import java.io.IOException;
import java.util.List;
public class Main {
public static void main(String[] args) {
try {
CharStream codeStream = CharStreams.fromFileName(args[0]);
CSharpLexer lexer = new CSharpLexer(codeStream);
List<? extends Token> tokens = lexer.getAllTokens();
ListTokenSource tokensSource = new ListTokenSource(tokens);
CommonTokenStream tokensStream = new CommonTokenStream(tokensSource);
CSharpParser parser = new CSharpParser(tokensStream);
ParserRuleContext ast = parser.compilation_unit();
String stringTree = ast.toStringTree(parser);
System.out.print("Tree " + stringTree);
}
catch (IOException e) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment