-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseTree.java
More file actions
131 lines (119 loc) · 4.08 KB
/
Copy pathParseTree.java
File metadata and controls
131 lines (119 loc) · 4.08 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
package dendron.tree;
import dendron.Errors;
import dendron.machine.Machine;
import dendron.tree.ActionNode;
import dendron.tree.ExpressionNode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;
/**
* Operations that are done on a Dendron code parse tree.
*
* THIS CLASS IS UNIMPLEMENTED. All methods are stubbed out.
*
* @author Liang,Yu(yxl5521)
*/
public class ParseTree {
private Program pro;
private HashMap<String,Integer> map;
/**
* Parse the entire list of program tokens. The program is a
* sequence of actions (statements), each of which modifies something
* in the program's set of variables. The resulting parse tree is
* stored internally.
* @param program the token list (Strings)
*/
public ParseTree( List< String > program ) {
this.pro = new Program();
this.map=new HashMap<>();
while (program.size()!=0) {
pro.addAction(parseAction(program));
}
}
/**
* Parse the next action (statement) in the list.
* (This method is not required, just suggested.)
* @param program the list of tokens
* @return a parse tree for the action
*/
private ActionNode parseAction( List< String > program ) {
String s = program.remove(0);
if (s.equals(":=")){
if (program.size()==0){
Errors.report(Errors.Type.PREMATURE_END, ":=");
}
return new Assignment(program.remove(0),parseExpr(program));
}
else if (s.equals("@")){
if (program.size()==0){
Errors.report(Errors.Type.PREMATURE_END, "@");
}
return new Print(parseExpr(program));
}
else {
Errors.report(Errors.Type.ILLEGAL_VALUE,s);
return null;
}
}
/**
* Parse the next expression in the list.
* (This method is not required, just suggested.)
* @param program the list of tokens
* @return a parse tree for this expression
*/
private ExpressionNode parseExpr( List< String > program ) {
if (program.get(0).equals("_") || program.get(0).equals("#")){
if (program.size()==1){
Errors.report(Errors.Type.ILLEGAL_VALUE, program.get(0));
}
return new UnaryOperation(program.remove(0),parseExpr(program));
}
else if(program.get(0).equals("+") || program.get(0).equals("-") || program.get(0).equals("*") || program.get(0).equals("/")) {
if (program.size()==2){
Errors.report(Errors.Type.ILLEGAL_VALUE, program.get(0));
}
return new BinaryOperation(program.remove(0),parseExpr(program),parseExpr(program));
}
else if (program.get(0).matches("^[a-zA-Z].*")){
return new Variable(program.remove(0));
}
else if (program.get(0).matches("[-+]?\\d+")){
return new Constant(Integer.parseInt(program.remove(0)));
}
else {
Errors.report(Errors.Type.ILLEGAL_VALUE,program.get(0));
return null;
}
}
/**
* Print the program the tree represents in a more typical
* infix style, and with one statement per line.
* @see dendron.tree.ActionNode#infixDisplay()
*/
public void displayProgram() {
System.out.println("The Program, with expressions in infix notation:");
System.out.println();
pro.infixDisplay();
}
/**
* Run the program represented by the tree directly
* @see dendron.tree.ActionNode#execute(Map)
*/
public void interpret() {
System.out.println("Interpreting the parse tree...");
pro.execute(map);
System.out.println("Interpretation complete.");
System.out.println();
Errors.dump(map);
}
/**
* Build the list of machine instructions for
* the program represented by the tree.
* @return the Machine.Instruction list
* @see Machine.Instruction#execute()
*/
public List< Machine.Instruction > compile() {
return pro.emit();
}
}