forked from leethater/ADS4_2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LookAhead1.java
59 lines (46 loc) · 1.31 KB
/
LookAhead1.java
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
import java.io.*;
class LookAhead1 {
/* Simulating a reader class for a stream of Token */
private Token current;
private Lexer lexer;
public LookAhead1(Lexer l)
throws Exception {
lexer=l;
current=lexer.yylex();
}
public boolean check(Sym s)
throws Exception {
/* check whether the first character is of type s*/
return (current.getSym() == s);
}
public Token getCurrent(){
return current;
}
public Lexer getLexer(){
return lexer;
}
public void eat(Sym s)
throws Exception {
/* consumes a token of type s from the stream,
exception when the contents does not start on s. */
if (!check(s)) {
throw new Exception("\n" + lexer.getPosition()+": Can't eat "+s+" current being "+current);
}
//for debug
// System.out.println(current);
current=lexer.yylex();
}
/* public String getValue()
throws Exception {
// it gives the value of the ValuedToken, or it rises an exception if not ValuedToken
if (current instanceof ValuedToken) {
ValuedToken t = (ValuedToken) current;
return t.getValue();
} else {
throw new Exception("\n"+ lexer.getPosition()+": LookAhead error: get value from a non-valued token");
}
}*/
public boolean isEmpty(){
return current==null;
}
}