-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpr.java
More file actions
43 lines (43 loc) · 1.51 KB
/
Expr.java
File metadata and controls
43 lines (43 loc) · 1.51 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
class Expr{
Term tr;
Expr ex;
int flag = 0;
void parse(Scanner s){
//Checks if token is beginning of a term, if it is, parses the term
if(s.currentToken() != Core.ID && s.currentToken() != Core.CONST && s.currentToken() != Core.LPAREN){
System.out.println("1Error: Expected ID, CONST, or LPAREN Token. Got: " + s.currentToken());
System.exit(1);
}
tr = new Term();
tr.parse(s);
if(s.currentToken() == Core.ADD){
flag = 1;
s.nextToken();
if(s.currentToken() != Core.ID && s.currentToken() != Core.CONST && s.currentToken() != Core.LPAREN){
System.out.println("2Error: Expected ID, CONST, or LPAREN Token. Got: " + s.currentToken());
System.exit(1);
}
ex = new Expr();
ex.parse(s);
}else if( s.currentToken() == Core.SUBTRACT){
flag = 2;
s.nextToken();
if(s.currentToken() != Core.ID && s.currentToken() != Core.CONST && s.currentToken() != Core.LPAREN){
System.out.println("3Error: Expected ID, CONST, or LPAREN Token. Got: " + s.currentToken());
System.exit(1);
}
ex = new Expr();
ex.parse(s);
}
}
void print(){
tr.print();
if(flag == 1){
System.out.println("+");
ex.print();
}else if(flag == 2){
System.out.println("-");
ex.print();
}
}
}