/* * token.c - lexical token functions for D compiler * CSE 413 au06 *TODO: add cases for lexical classes added to token.h and * complete other cases */ #include #include #include #include "token.h" /* Store a string representation of token t in s. */ /* pre: s must have enough space for the result. */ void tok2str(Token t, char* s) { switch (t.kind) { /* EOF, id, integer constants */ case TOK_EOF: strcpy(s, "EOF"); return; case TOK_ID: strcpy(s, "ID("); strcat(s, t.id); strcat(s, ")"); return; case TOK_INTGR: /* not implemented yet */ return; /* keywords */ case TOK_INT: strcpy(s, "INT"); return; case TOK_IF: strcpy(s, "IF"); return; case TOK_ELSE: strcpy(s, "ELSE"); return; /* Operators, punctuation, and delimiters */ case LPAREN: strcpy(s, "LPAREN"); return; case RPAREN: strcpy(s, "RPAREN"); return; case COMMA: strcpy(s, "COMMA"); return; case BECOMES: strcpy(s, "BECOMES"); return; case EQUALS: strcpy(s, "EQUALS"); return; case PLUS: strcpy(s, "PLUS"); return; case MINUS: strcpy(s, "MINUS"); return; /* default: should never happen (indicates programming error) */ default: fprintf(stderr, "error: default case reached in tok2str\n"); exit(EXIT_FAILURE); } }