using System.Text; /// /// A Token represents a terminal symbol. /// Tokens are provided by the scanner for the parser. /// They hold additional information about the symbol. /// public class Token { // token codes public const int //error token NONE = 0, //terminal classes IDENT = 1, NUMBER = 2, CHARCONST = 3, //+ - * / % ADD = 4, SUB = 5, MUL = 6, DIV = 7, MOD = 8, //relational operators (must be consecutive numbers, see Parser.Relop) //== >= > <= < != EQ = 9, GE = 10, GT = 11, LE = 12, LT = 13, NE = 14, //&& || AND = 15, OR = 16, //= ++ -- ASSIGN = 17, INC = 18, DEC = 19, //; , . SEMCOL = 20, COMMA = 21, DOT = 22, //( ) [ ] { } LPAR = 23, RPAR = 24, LBRACK = 25, RBRACK = 26, LBRACE = 27, RBRACE = 28, //KEYWORDS WRITE = 29, CLASS = 30, CONST = 31, ELSE = 32, IF = 33, NEW = 34, READ = 35, RETURN = 36, VOID = 37, WHILE = 38, //end of file EOF = 39; // List of printable names for all kinds of tokens (e.g. for error messages). // The token codes must index correctly into this array! public static readonly string[] names = { "?", "identifier", "number", "character constant", "+", "-", "*", "/", "%", "==", ">=", ">", "<=", "<", "!=", "&&", "||", "=", "++", "--", ";", ",", ".", "(", ")", "[", "]", "{", "}", "write", "class", "const", "else", "if", "new", "read", "return", "void", "while", "end of file" }; public int kind; // token code (NONE, IDENT, ...) public int line; // token line number (for error messages) public int col; // token column number (for error messages) public int val; // numerical value (for numbers and character constants) // We keep the string representation of numbers for error messages in case the // number literal in the source code is too big to fit into an int. public string str; // string representation of token (for numbers and identifiers) public Token (int kind, int line, int col, int val, string str) { this.kind = kind; this.line = line; this.col = col; this.val = val; this.str = str; } // Returns a string representation of this Token object. // Always includes all attributes of the token in the string. // When string or val are irrelevant, they are put in parenthesis. public override string ToString () { StringBuilder sb = new StringBuilder(); sb.AppendFormat("line {0}, col {1}: {2}", line, col, names[kind]); switch (kind) { case IDENT: sb.AppendFormat(" = {0}", str); break; case NUMBER: sb.AppendFormat(" = {0} (\"{1}\")", val, str); break; case CHARCONST: sb.AppendFormat(" = '{0}'", (char) val); break; } return sb.ToString(); } }