-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.txt
213 lines (186 loc) · 6.15 KB
/
template.txt
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//PACKAGE
import java.io.*;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
import java.util.ArrayList;
/**
* @author Dokyme
*/
public class Parser {
//REDUCTIONS
private static final String TOKEN_SPLITER = ",";
private static final int TOKEN_TOKS_NUMBER = 3;
private Stack<Integer> stateStack;
private Stack<Symbol> symbolStack;
private String outputPath;
private String inputPath;
private BufferedWriter outputWriter;
private BufferedReader inputReader;
private boolean debug = false;
private String raw;
private int innerCode;
private Symbol token;
private int newState;
//VARIABLE
public Parser() {
stateStack = new Stack<>();
symbolStack = new Stack<>();
}
private int gott(int state, Symbol symbol) {
//GOTO
}
private void run() {
try {
stateStack.push(0);
token = readToken();
while (true) {
if (token == null) {
token = new DOLLARR();
}
if (symbolStack.size() != 0) {
debug("The symbol on the top is " + symbolStack.peek().getName() + ".");
}
//SWITCH
}
} catch (Exception e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
private void output(Symbol...productions) {
try{
String out = productions[0] + "->";
for (int i=1;i<productions.length;i++) {
out += productions[i] + " ";
}
System.out.println(out);
if (outputWriter != null) {
outputWriter.write(out + "\n");
}
return;
}catch(Exception e){
e.printStackTrace();
error();
return;
}
}
private void end() {
debug("Finish parsing.");
try {
inputReader.close();
if (outputWriter != null) {
outputWriter.close();
}
} catch (Exception e) {
}
System.exit(0);
}
private int error() {
System.out.println("Error in parsing.");
System.exit(1);
return -1;
}
private Symbol readToken() {
try{
String line = inputReader.readLine();
if (line == null) {
return null;
}
int lp = line.indexOf('(');
int rp = line.lastIndexOf(')');
if (lp == -1 || rp == -1 || (!line.contains(TOKEN_SPLITER))) {
throw new Exception("Token file wrong format!");
}
String[] toks = line.substring(lp + 1, rp).split(",");
if (toks.length != TOKEN_TOKS_NUMBER) {
throw new Exception("Token file wrong format!");
}
raw = toks[0];
innerCode = Integer.valueOf(toks[2]);
debug("Read token:<" + toks[0] + "," + toks[1] + "," + toks[2] + ">");
return (Symbol) Class.forName(this.getClass().getPackage().getName() + "." + toks[1]).newInstance();
}catch(Exception e){
e.printStackTrace();
error();
return null;
}
}
private void debug(String content) {
if (debug) {
System.out.println("[DEBUG]" + content);
}
}
private void pushAndGoto(Symbol symbol){
symbolStack.push(symbol);
newState = gott(stateStack.peek(),symbol);
stateStack.push(newState);
}
private void pushAndReadNext(int state){
symbolStack.push(token);
stateStack.push(state);
token = readToken();
}
private void popStates(int num){
for(int i=0;i<num;i++) {
stateStack.pop();
}
}
private void parseCmdArgs(String[] args) {
if (args.length < 1) {
System.out.println("Token file generated by lexical scanner unspecified.");
System.out.println("Using -h for help.");
System.exit(1);
}
for (int i = 0; i < args.length; i++) {
String arg = args[i];
if ("-h".equals(arg)) {
System.out.println("Usage : java Parser [Options] FILE");
System.out.println("Options : ");
System.out.println("\t-o\tOUTPUT\tSpecify the output path of file which contains AST.\n\t\t\t\tIf unspecified,the AST will only be printed on SYSOUT.");
System.out.println("\t-d\t\t\tShow debug information.");
System.out.println("\t-h\t\t\tShow help document.");
System.out.println("\t-v\t\t\tShow version information.");
System.exit(0);
} else if ("-v".equals(arg)) {
System.out.println("Parser generated by DokymeYacc.2018/1/2.\nHappy new year~");
System.exit(0);
} else if ("-o".equals(arg)) {
i++;
try {
outputPath = args[i];
outputWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath)));
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(1);
}
} else if ("-d".equals(arg)) {
debug = true;
} else {
if (i != args.length - 1) {
System.out.println("Wrong arguement for '" + arg + "'");
System.out.println("Using -h for help.");
} else {
inputPath = args[i];
try {
inputReader = new BufferedReader(new InputStreamReader(new FileInputStream(inputPath)));
} catch (IOException e) {
System.out.println(e.getMessage());
System.exit(1);
}
}
}
}
}
//PROGRAM
}
abstract class Symbol {
public String name;
public String getName() {
return this.getClass().getSimpleName();
}
public String toString(){
return this.getClass().getSimpleName();
}
}
//CLASS