-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
65 lines (48 loc) · 1.99 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import java.io.File;
import java.io.Writer;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.lang.StringBuilder;
public class Main {
public static void main(String[] args) throws Exception{
String inputFile = null;
String outputFile = null;
if ( args.length>0 ) {
inputFile = args[0];
outputFile = args[1];
}
InputStream is = System.in;
if ( inputFile!=null ) is = new FileInputStream(inputFile);
// create a CharStream that reads from standard input
ANTLRInputStream input = new ANTLRInputStream(is);
// create a lexer that feeds off of input CharStream
Tex_grammarLexer lexer = new Tex_grammarLexer(input);
// create a buffer of tokens pulled from the lexer
CommonTokenStream tokens = new CommonTokenStream(lexer);
// create a parser that feeds off the tokens buffer
Tex_grammarParser parser = new Tex_grammarParser(tokens);
ParseTree tree = parser.root(); // begin parsing at init rule
ParseTreeWalker walker = new ParseTreeWalker(); //create standard walker
LatexTranslateListener extractor = new LatexTranslateListener();
walker.walk(extractor,tree);
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outputFile), "utf-8"));
writer.append(extractor.sb);
} catch (IOException ex) {
// Report
} finally {
try {writer.close();} catch (Exception ex) {/*ignore*/}
}
System.out.println(tree.toStringTree(parser)); // print LISP-style tree
}
}