-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenizer.java
More file actions
55 lines (46 loc) · 1.65 KB
/
Tokenizer.java
File metadata and controls
55 lines (46 loc) · 1.65 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
//package edu.osu.lisp;
import java.util.StringTokenizer;
public class Tokenizer {
StringTokenizer tokenizer;
String expression;
public void tokenize(String expression) {
this.expression = expression;
this.tokenizer = new StringTokenizer(this.expression, " ().\t\n", true);
}
public String ckNextToken() throws interException{
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (token.charAt(0) == ' ' || token.charAt(0) == '\t' || token.charAt(0) == '\n') {
continue;
}
if (token.charAt(0) == '(') {
return "(";
}
if (token.charAt(0) == ')') {
return ")";
}
if (token.charAt(0) == '.') {
return ".";
}
if (Character.isLetter(token.charAt(0))) {
if (token.matches(Patterns.LITERAL)){
return token;
}
throw new interException("invalid symbol name");
}
if (Character.isDigit(token.charAt(0))) {
if (token.matches(Patterns.NUMERIC_ATOM)) {
return token;
}
throw new interException("invalid number");
}
if (token.charAt(0) == '+' || token.charAt(0) == '-') {
if (token.matches(Patterns.NUMERIC_ATOM)) {
return token;
}
throw new interException("invalid number");
}
}
throw new interException("incomplete expression");
}
}