/* * printToken.cc * by CMB * A useful routine for printing tokens. * * WARNING: If token values in espresso.tab.h change -- this will no longer * work: Getting valid output depends on the number and the order of * the values * defined in espresso.tab.h * * MOD 1/12/05 by CMB * Changed to print tokens and linenums in fixed width fields * * MOD 9/14/04 by CMB * took out TRUE token * * MOD 9/15/04 by CMB * moved #include to header file as part of changes to get * code to compile with g++ version 3 * */ #include "printToken.h" #include const int START = 258; // the offset to subtract to get the correct index // in the table // NUMPRINTVALUES describes the size of tokprintvals below. // this depends the value of the last token defined in espresso.tab.h // Why +2? +1 is because of zero-based arrays // +1 is to store the EOF at the end const int NUMPRINTVALUES = (282 - START) + 2; /* * Table of Printvalues for tokens. You can get from a token value * number (defined in espresso.tab.h) to the index of the * corresponding print value by subtracting START (defined above). * E.g., (tokprintvals[IF - START] == "IF") (For single character * tokens, the print value is the character itself.) */ static char * tokprintvals[] = {"BOOLEAN", "CLASS", "ELSE", "IF", "INT", "MAIN", "NEW", "NULLTOK", "OUT", "PRINTLN", "PUBLIC", "RETURN", "STATIC", "THIS", "VOID", "WHILE", "STRING", "SYSTEM", "INT_LITERAL", "BOOL_LITERAL", "ID", "EQUALS", "NOTEQUALS", "AND", "ERROR", "EOF" }; /* See .h file for interface comments */ void printToken (ostream &o, int token) { if (token == 0) { /* special case for bison interface */ o << left << setw(12) << tokprintvals[NUMPRINTVALUES-1]; } else if (token < START) { // single char token o << left << setw(12) << (char) token; } else if (token > START + (NUMPRINTVALUES-2)) { o << left << setw(12) << ""; } else { o << left << setw(12) << tokprintvals[token-START]; } }