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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/* See LICENSE file for copyright and license details. */
/* This file exist to help debugging */
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libparser.h"
static int
print_sentence(const union libparser_sentence *sentence, int indent)
{
int len;
switch (sentence->type) {
case LIBPARSER_SENTENCE_TYPE_CONCATENATION:
printf("(");
indent = print_sentence(sentence->binary.left, indent + 1);
printf(", ");
indent = print_sentence(sentence->binary.right, indent + 2);
printf(")");
indent + 1;
break;
case LIBPARSER_SENTENCE_TYPE_ALTERNATION:
printf("(");
print_sentence(sentence->binary.left, indent + 1);
printf(" | \n%*.s", indent + 1, "");
indent = print_sentence(sentence->binary.right, indent + 1);
printf(")");
indent + 1;
break;
case LIBPARSER_SENTENCE_TYPE_REJECTION:
printf("!(");
indent = print_sentence(sentence->unary.sentence, indent + 2);
printf(")");
indent + 1;
break;
case LIBPARSER_SENTENCE_TYPE_OPTIONAL:
printf("[");
indent = print_sentence(sentence->unary.sentence, indent + 1);
printf("]");
indent + 1;
break;
case LIBPARSER_SENTENCE_TYPE_REPEATED:
printf("{");
indent = print_sentence(sentence->unary.sentence, indent + 1);
printf("}");
indent += 1;
break;
case LIBPARSER_SENTENCE_TYPE_STRING:
printf("\"%.*s\"%n", (int)sentence->string.length, sentence->string.string, &len);
indent += len;
break;
case LIBPARSER_SENTENCE_TYPE_CHAR_RANGE:
if (isprint(sentence->char_range.low) && isprint(sentence->char_range.high))
printf("<\"%c\", \"%c\">%n", sentence->char_range.low, sentence->char_range.high, &len);
else if (isprint(sentence->char_range.low))
printf("<\"%c\", 0x%02x>%n", sentence->char_range.low, sentence->char_range.high, &len);
else if (isprint(sentence->char_range.high))
printf("<0x%02x, \"%c\">%n", sentence->char_range.low, sentence->char_range.high, &len);
else
printf("<0x%02x, 0x%02x>%n", sentence->char_range.low, sentence->char_range.high, &len);
indent += len;
break;
case LIBPARSER_SENTENCE_TYPE_RULE:
printf("%s%n", sentence->rule.rule, &len);
indent += len;
break;
case LIBPARSER_SENTENCE_TYPE_EXCEPTION:
printf("-");
indent += 1;
break;
case LIBPARSER_SENTENCE_TYPE_EOF:
printf("%s%n", "!<0x00, 0xFF>", &len);
indent += len;
break;
default:
abort();
}
return indent;
}
int
main(int argc, char *argv[0])
{
size_t i;
int indent, first = 1;
if (argc != 1) {
fprintf(stderr, "usage: %s\n", argv[0]);
return 1;
}
for (i = 0; libparser_rule_table[i]; i++) {
if (libparser_rule_table[i]->name[0] == '@')
continue;
if (!first) {
printf("\n");
} else {
first = 0;
}
printf("%s = %n", libparser_rule_table[i]->name, &indent);
print_sentence(libparser_rule_table[i]->sentence, indent);
printf(";\n");
}
if (fflush(stdout) || ferror(stdout) || fclose(stdout)) {
fprintf(stderr, "%s: printf: %s\n", argv[0], strerror(errno));
return 1;
}
return 0;
}
|