-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamle.java
More file actions
110 lines (107 loc) · 3.49 KB
/
camle.java
File metadata and controls
110 lines (107 loc) · 3.49 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.*;
import java.lang.reflect.Array;
import antlr.collections.AST;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.StringTemplate;
class camle {
public static void main(String[] args)
{
System.out.println("CAMLE - Compiler to Abstract Machine for Language Engineering");
String opt = "", inFile = "", outFile = "";
int pos;
if (Array.getLength(args) == 1 && args[0].charAt(0) != '-') {
opt = "";
inFile = args[0];
}
else if (Array.getLength(args) == 2 && args[0].charAt(0) == '-' &&
args[1].charAt(0) != '-') {
opt = args[0];
inFile = args[1];
}
else {
System.out.println("Usage: antlr Main [option] filename");
System.out.println("");
System.out.println("Options:");
System.out.println(" -lex");
System.out.println(" -syn");
System.out.println(" -irt");
System.exit(1);
}
outFile = inFile;
if ((pos = outFile.lastIndexOf('.')) != -1)
outFile = outFile.substring(0, pos);
outFile = outFile+".ass";
try {
int numSynErrors;
int numErrors;
CharStream cs = new ANTLRFileStream(inFile);
Lex lexO = new Lex(cs);
if (opt.equals("-lex")) {
Token T;
T = lexO.nextToken();
while (T.getText() != null) {
System.out.println(T.getType()+" \""+T.getText()+"\"");
T = lexO.nextToken();
}
numErrors = lexO.getLexErrors();
if(numErrors != 0) {
System.out.println("Lexer failed: " + numErrors + " errors, Exiting...");
}
System.exit(0);
}
CommonTokenStream tokens = new CommonTokenStream(lexO);
Syn synO = new Syn(tokens);
Syn.program_return parserResult = synO.program();//start rule
numErrors = lexO.getLexErrors();
if(numErrors != 0) {
System.out.println("Lexer failed: " + numErrors + " errors");
}
CommonTree parserTree = (CommonTree) parserResult.getTree();
if (opt.equals("-syn")) {
System.out.println(parserTree.toStringTree());
numErrors = synO.getSynErrors();
if(numErrors != 0) {
System.out.println("Syn stage failed: " + numErrors + " errors,Exiting...");
}
System.exit(0);
}
numSynErrors = synO.getSynErrors();
if(numSynErrors != 0) {
System.out.println("Syn stage failed: " + numErrors + " errors");
System.exit(0);
}
if(numErrors != 0 || numSynErrors != 0) {
System.out.println("Exiting...");
System.exit(0);
}
if (opt.equals("-tree")) {
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(parserTree);
try {
FileWriter treeFile = new FileWriter("tree.dot");
PrintWriter out = new PrintWriter(treeFile);
out.println(st);
out.close();
System.out.println("Tree written to file.");
} catch (Exception e) {
System.out.println("Tree generation failed.");
}
System.exit(0);
}
CommonTreeNodeStream ast = new CommonTreeNodeStream(parserTree);
IRTree newIrt = Irt.convert(parserTree);
if (opt.equals("-irt")) {
System.out.println(newIrt);
Memory.dumpData(System.out);
System.exit(0);
}
PrintStream o = new PrintStream(new FileOutputStream(outFile));
Cg.program(newIrt, o);
}
catch(Exception e) {
System.err.println("exception: "+e);
//System.exit(0);
}
}
}