Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import org.antlr.v4.runtime.CharStream;
  2. import org.antlr.v4.runtime.ANTLRInputStream;
  3. import org.antlr.v4.runtime.CommonTokenStream;
  4. import org.antlr.v4.runtime.Token;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7.  
  8. public class ANTLRDemo {
  9.  
  10. public static void main(String[] args) throws Exception {
  11. // Read in input from the source code file
  12. String currentLine = null;
  13. String sourceCode = "";
  14. BufferedReader readerIn = new BufferedReader(new FileReader("HelloWorld.java"));
  15.  
  16. while ((currentLine = readerIn.readLine()) != null) {
  17. sourceCode += currentLine;
  18. }
  19.  
  20. // Lexical Analysis - generate the tokens for the compiler
  21. CharStream in = new ANTLRInputStream(sourceCode);
  22. JavaLexer lexer = new JavaLexer(in);
  23. CommonTokenStream tokens = new CommonTokenStream(lexer);
  24. tokens.fill();
  25.  
  26. // Syntax Analysis - Parse the tokens into a parse tree
  27. JavaParser parser = new JavaParser(tokens);
  28. System.out.println(parser.compilationUnit().toStringTree());
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement