-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToken.java
More file actions
122 lines (110 loc) · 4.01 KB
/
Copy pathToken.java
File metadata and controls
122 lines (110 loc) · 4.01 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
*
* Represents a lexical token for 312 exercise.
*
* This class has been provided to students
*
* @Author: Roger Garside, John Mariani, John Vidler, Paul Rayson
*
*
**/
public class Token
{
public static final int becomesSymbol = 1 ;
public static final int beginSymbol = 2 ;
public static final int callSymbol = 3 ;
public static final int colonSymbol = 4 ;
public static final int commaSymbol = 5 ;
public static final int divideSymbol = 6 ;
public static final int doSymbol = 7 ;
public static final int endSymbol = 8 ;
public static final int elseSymbol = 9 ;
public static final int eofSymbol = 10 ;
public static final int equalSymbol = 11 ;
public static final int errorSymbol = 12 ;
public static final int floatSymbol = 13 ;
public static final int greaterEqualSymbol = 14 ;
public static final int greaterThanSymbol = 15 ;
public static final int identifier = 16 ;
public static final int ifSymbol = 17 ;
public static final int integerSymbol = 18 ;
public static final int isSymbol = 19 ;
public static final int leftParenthesis = 20 ;
public static final int lessEqualSymbol = 21 ;
public static final int lessThanSymbol = 22 ;
public static final int loopSymbol = 23 ;
public static final int minusSymbol = 24 ;
public static final int notEqualSymbol = 25 ;
public static final int numberConstant = 26 ;
public static final int plusSymbol = 27 ;
public static final int procedureSymbol = 28 ;
public static final int rightParenthesis = 29 ;
public static final int semicolonSymbol = 30 ;
public static final int stringConstant = 31 ;
public static final int stringSymbol = 32 ;
public static final int timesSymbol = 33 ;
public static final int thenSymbol = 34 ;
public static final int untilSymbol = 35 ;
public static final int whileSymbol = 36 ;
public static final int forSymbol = 37 ;
private static final String[] names = {
":=", "begin", "call", ":", ",",
"/", "do", "end", "else", "EOF",
"=", "ERROR", "float", ">=", ">",
"IDENTIFIER", "if", "integer", "is", "(",
"<=", "<", "loop", "-", "/=",
"NUMBER", "+", "procedure", ")", ";",
"STRING", "string", "*", "then", "until",
"while", "for"
} ;
/** The symbol this token instance represents */
public int symbol ;
/** The original text. */
public String text ;
/** The line number of the original text in the source file. */
public int lineNumber ;
/** Constructs a new token with a given token type and line number.
@param s The type of symbol, typically as a class constant from Token.
@param t The original string recognised from the source file.
@param l The line number of the original string.
*/
public Token(int s, String t, int l)
{
symbol = s ;
text = t ;
lineNumber = l ;
} // end of constructor method
/** Constructs a new token from a StringBuffer, given type and line number.
@param s The type of symbol, typically as a class constant from Token.
@param t The original string recognised from the source file.
@param l The line number of the original string.
*/
public Token(int s, StringBuffer t, int l)
{
symbol = s ;
text = new String(t) ;
lineNumber = l ;
} // end of constructor method
/** Returns a string representation of a symbol type.
@param i The value of a symbol, typically as a class constant from Token.
@return The name of this symbol.
*/
public static String getName(int i)
{
if ((i < 1) || (i > names.length))
return "UNKNOWN" ;
else
return names[i - 1] ;
} // end of method getName
/** @see Object.toString */
public String toString()
{
String tt = "token " + getName(symbol) ;
if ((symbol == identifier) ||
(symbol == numberConstant) ||
(symbol == stringConstant))
tt += ": " + text ;
tt += " (line " + lineNumber + ")" ;
return tt ;
} // end of method toString
} // end of class Token