-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactor.java
More file actions
68 lines (67 loc) · 2.32 KB
/
Factor.java
File metadata and controls
68 lines (67 loc) · 2.32 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
class Factor{
Expr ex;
String name;
int con;
int flag = 0;
void parse(Scanner s){
if(s.currentToken() != Core.ID && s.currentToken() != Core.CONST && s.currentToken() != Core.LPAREN){
System.out.println("Error: Expected ID, CONST, or LPAREN Token. Got: " + s.currentToken());
System.exit(1);
}
if(s.currentToken() == Core.ID){
flag = 0;
name = s.getId();
s.nextToken();
if(s.currentToken() == Core.LBRACE){
flag = 1;
s.nextToken();
if(s.currentToken() == Core.ID || s.currentToken() == Core.CONST|| s.currentToken() == Core.LPAREN){
ex = new Expr();
ex.parse(s);
}else{
System.out.println("Error: Expected ID, CONST, or LPAREN Token. Got: " + s.currentToken());
System.exit(1);
}
if(s.currentToken() != Core.RBRACE){
System.out.println("Error: Expected RBRACE Token");
System.exit(1);
}
s.nextToken();
}
}else if(s.currentToken() == Core.CONST){
flag = 2;
con = s.getConst();
s.nextToken();
}else if(s.currentToken() == Core.LPAREN){
flag = 3;
s.nextToken();
if(s.currentToken() == Core.ID || s.currentToken() == Core.CONST|| s.currentToken() == Core.LPAREN){
ex = new Expr();
ex.parse(s);
}else{
System.out.println("Error: Expected ID, CONST, or LPAREN Token. Got: " + s.currentToken());
System.exit(1);
}
if(s.currentToken() != Core.RPAREN){
System.out.println("Error: Expected RPAREN Token");
System.exit(1);
}
s.nextToken();
}
}
void print(){
if(flag == 0){
System.out.print(name);
}else if(flag == 1){
System.out.print(name + "[");
ex.print();
System.out.print("]");
}else if(flag == 2){
System.out.print(con);
}else if(flag == 3){
System.out.print("(");
ex.print();
System.out.print(")");
}
}
}