-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpal20.java
More file actions
143 lines (123 loc) · 4.12 KB
/
rpal20.java
File metadata and controls
143 lines (123 loc) · 4.12 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import rpal.AST;
import rpal.CSEMachine;
import rpal.Parser;
import rpal.LexicalAnalyzer;
public class rpal20 {
public static String fileName;
public static void main(String[] args){
boolean listFlag = false;
boolean astFlag = false;
boolean stFlag = false;
boolean noOutFlag = false;
fileName = "";
AST ast = null;
for(String cmdOption: args){
if(cmdOption.equals("-help")){
printHelp();
return;
}
else if(cmdOption.equals("-l"))
listFlag = true;
else if(cmdOption.equals("-ast"))
astFlag = true;
else if(cmdOption.equals("-st"))
stFlag = true;
else if(cmdOption.equals("-noout"))
noOutFlag = true;
else
fileName = cmdOption;
}
//calling P2 without any switches should evaluate the program and print the result
if(!listFlag && !astFlag && !stFlag && !noOutFlag){
ast = buildAST(fileName, true);
ast.standardize();
evaluateST(ast);
return;
}
if(listFlag){
if(fileName.isEmpty())
throw new RuntimeException("Please specify a file. Call P2 with -help to see examples");
printInputListing(fileName);
}
if(astFlag){
if(fileName.isEmpty())
throw new RuntimeException("Please specify a file. Call P2 with -help to see examples");
ast = buildAST(fileName, true);
printAST(ast);
if(noOutFlag)
return;
ast.standardize();
evaluateST(ast);
}
if(stFlag){
if(fileName.isEmpty())
throw new RuntimeException("Please specify a file. Call P2 with -help to see examples");
ast = buildAST(fileName, true);
ast.standardize();
printAST(ast);
if(noOutFlag)
return;
evaluateST(ast);
}
//-noout without -ast or -st produces no output
if(noOutFlag && (!astFlag || !stFlag)){
if(fileName.isEmpty())
throw new RuntimeException("Please specify a file. Call P2 with -help to see examples");
}
}
private static void evaluateST(AST ast){
CSEMachine csem = new CSEMachine(ast);
csem.evaluateProgram();
System.out.println();
}
private static void printInputListing(String fileName){
BufferedReader buffer = null;
try{
buffer = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
String s = "";
while((s = buffer.readLine())!=null){
System.out.println(s);
}
}catch(FileNotFoundException e){
throw new RuntimeException("File "+fileName+" not found.");
}catch(IOException e){
throw new RuntimeException("Error reading from file "+fileName);
}finally{
try{
if(buffer!=null) buffer.close();
}catch(IOException e){
}
}
}
private static AST buildAST(String fileName, boolean printOutput){
AST ast = null;
try{
LexicalAnalyzer scanner = new LexicalAnalyzer(fileName);
Parser parser = new Parser(scanner);
ast = parser.buildAST();
}catch(IOException e){
throw new RuntimeException("ERROR: Could not read from file: " + fileName);
}
return ast;
}
private static void printAST(AST ast){
ast.printAST();
}
private static void printHelp(){
System.out.println("Usage: java P2 [OPTIONS] FILE");
System.out.println("Without any switches, prints only the result of evaluating the program");
System.out.println(" -ast: prints the abstract syntax tree generated followed by the result");
System.out.println(" of evaluating the program");
System.out.println(" with -noout, prints only the abstract syntax tree generated");
System.out.println(" -st: prints the standardized syntax tree generated followed by the result");
System.out.println(" of evaluating the program");
System.out.println(" with -noout, prints only the standardized syntax tree generated");
System.out.println(" -l: prints the source code listing");
}
}