You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

88 lines
2.6 KiB

/*
* @(#)Token.java
*
* Revisions and updates (c) 2022-2023 Sandy Brownlee. alexander.brownlee@stir.ac.uk
*
* Original release:
*
* Copyright (C) 1999, 2003 D.A. Watt and D.F. Brown
* Dept. of Computing Science, University of Glasgow, Glasgow G12 8QQ Scotland
* and School of Computer and Math Sciences, The Robert Gordon University,
* St. Andrew Street, Aberdeen AB25 1HG, Scotland.
* All rights reserved.
*
* This software is provided free for educational use only. It may
* not be used for commercial purposes without the prior written permission
* of the authors.
*/
package triangle.syntacticAnalyzer;
final class Token extends Object {
protected int kind;
protected String spelling;
protected SourcePosition position;
public Token(int kind, String spelling, SourcePosition position) {
if (kind == Token.IDENTIFIER) {
int currentKind = firstReservedWord;
boolean searching = true;
while (searching) {
int comparison = tokenTable[currentKind].compareTo(spelling);
if (comparison == 0) {
this.kind = currentKind;
searching = false;
} else if (comparison > 0 || currentKind == lastReservedWord) {
this.kind = Token.IDENTIFIER;
searching = false;
} else {
currentKind++;
}
}
} else
this.kind = kind;
this.spelling = spelling;
this.position = position;
}
public static String spell(int kind) {
return tokenTable[kind];
}
@Override
public String toString() {
return "Kind=" + kind + ", spelling=" + spelling + ", position=" + position;
}
// Token classes...
public static final int
// literals, identifiers, operators...
INTLITERAL = 0, CHARLITERAL = 1, IDENTIFIER = 2, OPERATOR = 3,
// reserved words - must be in alphabetical order...
ARRAY = 4, BEGIN = 5, CONST = 6, DO = 7, ELSE = 8, END = 9, FUNC = 10, IF = 11, IN = 12, LET = 13, LOOP = 14, OF = 15,
PROC = 16, RECORD = 17, THEN = 18, TYPE = 19, VAR = 20, WHILE = 21,
// punctuation...
DOT = 22, COLON = 23, SEMICOLON = 24, COMMA = 25, BECOMES = 26, IS = 27,
// brackets...
LPAREN = 28, RPAREN = 29, LBRACKET = 30, RBRACKET = 31, LCURLY = 32, RCURLY = 33,
// special tokens...
EOT = 34, ERROR = 35;
private static String[] tokenTable = new String[] { "<int>", "<char>", "<identifier>", "<operator>", "array",
"begin", "const", "do", "else", "end", "func", "if", "in", "let", "loop", "of", "proc", "record", "then", "type",
"var", "while", ".", ":", ";", ",", ":=", "~", "(", ")", "[", "]", "{", "}", "", "<error>" };
private final static int firstReservedWord = Token.ARRAY, lastReservedWord = Token.WHILE;
}