-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckedSymbolLexer.java
executable file
·64 lines (47 loc) · 1.73 KB
/
CheckedSymbolLexer.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
60
61
62
// File: CheckedSymbolLexer.java
// Date: October 2015
// Java source file provided for Informatics 2A Assignment 1 (2015).
// Thin wrapper for token streams: checks if a symbol token is
// among those of MH, and renames its lexical class to be the symbol itself.
// (Helpful for the parser, which only looks at lexical classes.)
import java.io.* ;
class CheckedSymbolLexer implements LEX_TOKEN_STREAM {
GenLexer tokStream ;
CheckedSymbolLexer (GenLexer tokStream) {
this.tokStream = tokStream ;
}
static String[] validTokens = new String[]
{"::", "->", "=", "==", "<=", "+", "-"} ;
static String checkString (String s) throws UnknownSymbol {
for (int i=0; i<validTokens.length; i++) {
if (s.equals(validTokens[i])) return s ;
}
throw new UnknownSymbol(s) ;
}
static LexToken checkToken (LexToken t) throws UnknownSymbol {
if (t != null && t.lexClass().equals("SYM")) {
return new LexToken (t.value(), checkString(t.value())) ;
} else return t ;
}
public LexToken pullToken ()
throws LexError, StateOutOfRange, IOException, UnknownSymbol {
return checkToken (tokStream.pullToken()) ;
}
public LexToken pullProperToken ()
throws LexError, StateOutOfRange, IOException, UnknownSymbol {
return checkToken (tokStream.pullProperToken()) ;
}
public LexToken peekToken ()
throws LexError, StateOutOfRange, IOException, UnknownSymbol {
return checkToken (tokStream.peekToken()) ;
}
public LexToken peekProperToken ()
throws LexError, StateOutOfRange, IOException, UnknownSymbol {
return checkToken (tokStream.peekProperToken()) ;
}
}
class UnknownSymbol extends Exception {
public UnknownSymbol (String s) {
super ("Unknown symbolic token " + s) ;
}
}