-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprinttoken.c
85 lines (75 loc) · 3.29 KB
/
printtoken.c
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
/* printtoken.c print tokens for debugging 09 Feb 01 */
/* Functions to allocate token records and print tokens for debugging */
/* Copyright (c) 1998 Gordon S. Novak Jr. and
The University of Texas at Austin. */
/* This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License (file gpl.text) for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "token.h"
/* These arrays are defined for printing debugging information.
You may use them if you wish, or you may copy them and
modify them as you see fit. */
static char* opprnt[] = {" ", "+", "-", "*", "/", ":=", "=", "<>", "<", "<=",
">=", ">", "^", ".", "and", "or", "not", "div",
"mod", "in", "if", "goto", "progn", "label",
"funcall", "aref", "program", "float"};
static char *delprnt[] = { " ", " ,", " ;", " :", " (", " )", " [", " ]",
".."} ;
static char *resprnt[] = { " ", "array", "begin", "case", "const", "do",
"downto", "else", "end", "file", "for",
"function", "goto", "if", "label", "nil",
"of", "packed", "procedure", "program", "record",
"repeat", "set", "then", "to", "type",
"until", "var", "while", "with" };
TOKEN talloc() /* allocate a new token record */
{ TOKEN tok;
tok = (TOKEN) calloc(1,sizeof(struct tokn));
if ( tok != NULL ) return (tok);
else printf("talloc failed.");
}
void printtoken(TOKEN tok)
{
switch (tok->tokentype)
{case OPERATOR:
printf ("tokentype: %2d which: %4d %10s\n",
tok->tokentype, tok->whichval,
opprnt[tok->whichval] );
break;
case DELIMITER:
printf ("tokentype: %2d which: %4d %10s\n",
tok->tokentype, tok->whichval,
delprnt[tok->whichval] );
break;
case RESERVED:
printf ("tokentype: %2d which: %4d %10s\n",
tok->tokentype, tok->whichval,
resprnt[tok->whichval] );
break;
case IDENTIFIERTOK: case STRINGTOK:
printf ("tokentype: %2d value: %16s\n",
tok->tokentype, tok->stringval);
break;
case NUMBERTOK:
switch (tok->datatype)
{case INTEGER:
printf ("tokentype: %2d type: %4d %12d\n",
tok->tokentype, tok->datatype, tok->intval);
break;
case REAL:
printf ("tokentype: %2d type: %4d %12e\n",
tok->tokentype, tok->datatype, tok->realval);
break;
}
}
}