title: Antlr4 - Is there a simple example of using the ParseTree Walker?
body: <p>Antlr4 has a new class ParseTreeWalker.  But how do I use it?  I am looking for a minimal working example.  My grammar file is 'gram.g4' and I want to parse a file 'program.txt'</p>
  
  <p>Here is my code so far. (This assumes ANTLR has run my grammar file and created all of the <em>gramBaseListener</em>, <em>gramLexer</em>, etc etc):</p>
  
  <pre><code>import org.antlr.v4.runtime.*;
  import org.antlr.v4.runtime.tree.*;
  import static org.antlr.v4.runtime.CharStreams.fromFileName;
  
  public class launch{
  public static void main(String[] args) {
  
      CharStream cs = fromFileName("gram.g4");  //load the file
      gramLexer lexer = new gramLexer(cs);  //instantiate a lexer
      CommonTokenStream tokens = new CommonTokenStream(lexer); //scan stream for tokens
      gramParser parser = new gramParser(tokens);  //parse the tokens
  
      // Now what??  How do I connect the above with the below? 
  
      ParseTreeWalker walker = new ParseTreeWalker();  // how do I use this to parse program.txt??
  }}
  </code></pre>
  
  <p>I am using java but I assume it is similar in other languages.</p>
  
  <p>The ANTLR documentation (<a href="http://www.antlr.org/api/Java/index.html" rel="noreferrer">http://www.antlr.org/api/Java/index.html</a>) is short on examples.  There are many tutorials on the internet but they are mostly for ANTLR version 3.  The few using version 4 don't work or are outdated (for example, there is no parser.init() function, and classes like ANTLRInputStream are depreciated)</p>
  
  <p>Thanks in advance for anyone who can help.</p>