-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxTree.java
More file actions
97 lines (84 loc) · 2.63 KB
/
SyntaxTree.java
File metadata and controls
97 lines (84 loc) · 2.63 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
import java.util.ArrayList;
public class SyntaxTree extends Token{
public static final String CompUnit ="CompUnit"
,FuncDef = "FuncDef"
,FuncType = "FuncType"
,Block = "Block"
,BlockItem = "BlockItem"
,Decl="Decl"
,ConstDecl="ConstDecl"
,ConstDef="ConstDef"
,ConstInitVal="ConstInitVal"
,ConstExp="ConstExp"
,BType="BType"
,VarDecl="VarDecl"
,VarDef="VarDef"
,InitVal="InitVal"
,Stmt="Stmt"
,LVal="LVal"
,Exp="Exp"
,AddExp="AddExp"
,MulExp="MulExp"
,UnaryExp="UnaryExp"
,FuncRParams="FuncRParams"
,PrimaryExp="PrimaryExp"
,Cond = "Cond"
,LOrExp = "LOrExp"
,LAndExp ="LAndExp"
,EqExp="EqExp"
,RelExp="RelExp"
,FuncFParams="FuncFParams"
,FuncFParam="FuncFParam";
public String name = null;
public ArrayList<SyntaxTree> subtree;
public SyntaxTree(String name) {
this.name = name;
this.subtree = new ArrayList<>();
}
public SyntaxTree(int type){
super(type);
}
public SyntaxTree(int type,String content){
super(type,content);
}
public void addSubtree(SyntaxTree tree){
//System.out.println(tree.name);
this.subtree.add(tree);
}
public void addSubtree(int type){
//System.out.println(type);
if(Parser.token.type == type){
if(type == Token.NUMBER || type == Token.IDENT){
this.subtree.add(new SyntaxTree(type,Parser.token.content));
}else
this.subtree.add(new SyntaxTree(type));
Parser.token = Lexer.getToken();
//System.out.println(Parser.token.type);
} else {
/*if(name!=null)
System.out.println(name);
System.out.println(type);
System.out.println(Parser.token.type);*/
System.exit(1);
}
}
public SyntaxTree getSubtree(int index){
if(index<0 ||index>=subtree.size())
return null;
return subtree.get(index);
}
public int searchSubtree(String name){
for (int i = 0; i < subtree.size(); i++) {
if(subtree.get(i).name!=null && subtree.get(i).name.equals(name))
return i;
}
return -1;
}
public int searchSubtree(int type){
for (int i = 0; i < subtree.size(); i++) {
if(subtree.get(i).type == type)
return i;
}
return -1;
}
}