tokenize.c
1.68 KB
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
#include <stdio.h>
#include <ctype.h>
#define PARSER "y.tab.h"
#define MAX_LEN 10
#include "types.h"
#include "y.tab.h"
YYSTYPE yylval;
extern char yytext[];
char *pname = PARSER;
int max_len = MAX_LEN;
FILE *parser;
char *def[512];
main (argc, argv)
int argc;
char *argv[];
{
register int i, t;
char name[128];
int value;
for (i=1; i<argc; i++) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
case 'l': max_len = atoi(argv[++i]); break;
case 'p': pname = argv[++i]; break;
default:
fprintf (stderr, "usage: tokenize [-l len] [-p parser]\n");
exit(1);
}
}
else {
fprintf (stderr, "usage: tokenize [-l len] [-p parser]\n");
exit (1);
}
}
for (i=0; i<512; i++)
def[i] = (char *) NULL;
if ((parser = fopen (pname, "r")) == NULL) {
perror (pname);
exit (1);
}
while (getc(parser) != '#')
;
ungetc ('#', parser);
while (fscanf (parser, "# define %s %d\n", name, &value) == 2) {
def[value] = (char *) malloc (1+strlen(name));
strcpy (def[value], name);
}
while ((t = yylex()) > 0) {
if (strlen(yytext) > max_len)
printf ("%*.*s... ", max_len-3, max_len-3, yytext);
else
printf ("%*s ", max_len, yytext);
if (def[t])
printf ("<%s", def[t]);
else if (isprint (t))
printf ("<'%c'", t);
else
printf ("<'\\%03.3o'", t);
switch (t) {
case INT_LIT: printf (", %d", yylval.lit.ival); break;
case REAL_LIT: printf (", %g", yylval.lit.rval); break;
case BOOL_LIT: printf (", %d", yylval.lit.bval); break;
case STRING_LIT: printf (", '%s'", yylval.lit.sval); break;
case ID: printf (", %s", yylval.id); break;
default: ;
}
printf (">\n");
}
}