import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
import java.nio.charset.*;
import java.nio.file.Paths;
import java.nio.file.Files;

public class Main {
	private static String readFile(String path, Charset encoding) 
	  throws Exception 
	{
	  byte[] encoded = Files.readAllBytes(Paths.get(path));
	  return new String(encoded, encoding);
	}
	
    public static void main(String[] args) throws Exception {
		try {
			String src = readFile("input.tiger", StandardCharsets.UTF_8);
    	    tigerLexer lexer     = new tigerLexer(new ANTLRStringStream(src));
    	    tigerParser parser   = new tigerParser(new CommonTokenStream(lexer));
    	    CommonTree tree      = (CommonTree)(parser.tiger_program().getTree());
    	    DOTTreeGenerator gen = new DOTTreeGenerator();
    	    StringTemplate st    = gen.toDOT(tree);
    	    System.out.println(st);
		} catch (Exception e) {
			System.out.println(e);
		}
    }
}

@@ -112,24 +112,51 @@
 .charset.*;%0A
+import java.nio.file.Path;%0A
 import java.
@@ -198,16 +198,75 @@
 e.Files;
+%0Aimport java.io.BufferedWriter;%0Aimport java.io.IOException;
 %0A%0Apublic
@@ -468,17 +468,441 @@
 ng);%0A%09%7D%0A
-%09
+%0A    private static void writeDotFile(String fileName, String outputString, Charset encoding)%0A        throws Exception%0A    %7B%0A        Path file = Paths.get(fileName);%0A        try (BufferedWriter writer = Files.newBufferedWriter(file, encoding)) %7B%0A                writer.write(outputString, 0, outputString.length());%0A        %7D catch (IOException x) %7B%0A                System.err.format(%22IOException: %25s%25n%22, x);%0A        %7D%0A    %7D%0A
 %0A    pub
@@ -1377,34 +1377,79 @@
     
-%09    System.out.println(st
+        writeDotFile(%22input.dot%22, st.toString(), StandardCharsets.UTF_8
 );%0A%09

write to a dot file