Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.antlr.v4.runtime.CharStream;
- import org.antlr.v4.runtime.ANTLRInputStream;
- import org.antlr.v4.runtime.CommonTokenStream;
- import org.antlr.v4.runtime.Token;
- import java.io.BufferedReader;
- import java.io.FileReader;
- public class ANTLRDemo {
- public static void main(String[] args) throws Exception {
- // Read in input from the source code file
- String currentLine = null;
- String sourceCode = "";
- BufferedReader readerIn = new BufferedReader(new FileReader("HelloWorld.java"));
- while ((currentLine = readerIn.readLine()) != null) {
- sourceCode += currentLine;
- }
- // Lexical Analysis - generate the tokens for the compiler
- CharStream in = new ANTLRInputStream(sourceCode);
- JavaLexer lexer = new JavaLexer(in);
- CommonTokenStream tokens = new CommonTokenStream(lexer);
- tokens.fill();
- // Syntax Analysis - Parse the tokens into a parse tree
- JavaParser parser = new JavaParser(tokens);
- System.out.println(parser.compilationUnit().toStringTree());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement