Skip to content

Commit d0b9f4a

Browse files
committed
Make parser-lalr quiet by default, output is enabled by "-v"
1 parent 56c8b4b commit d0b9f4a

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ grammar specification is divided into five parts:
3030
5. Macros and misc. rules
3131

3232
Like the standalone lexer, it reads from stdin and outputs to
33-
stdout. In addition to being a recognizer for Rust, the parser from
34-
this grammar also builds and prints an AST in an s-expression format.
33+
stdout. In addition to being a recognizer for Rust, if "-v" is passed
34+
in as a command line argument, the parser from this grammar also
35+
builds and prints an AST in an s-expression format.
3536

3637
## Building
3738

@@ -41,7 +42,7 @@ later to both be installed.
4142

4243
On OS X, the Xcode toolchain provides an older version of bison
4344
(2.3). This will not work with the grammar in this project, so you
44-
will have to download and install version 3.0.2.
45+
will have to download and install version 3.0.2 or later.
4546

4647
Building of rlex and rparse do not (yet) support cargo, use make or
4748
just invoke directly with rustc.

parser-lalr-main.c

+30-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ extern int rsparse();
99
static const int PUSHBACK_LEN = 4;
1010

1111
static char pushback[PUSHBACK_LEN];
12+
static int verbose;
13+
14+
void print(const char* format, ...) {
15+
va_list args;
16+
va_start(args, format);
17+
if (verbose) {
18+
vprintf(format, args);
19+
}
20+
va_end(args);
21+
}
1222

1323
// If there is a non-null char at the head of the pushback queue,
1424
// dequeue it and shift the rest of the queue forwards. Otherwise,
@@ -54,7 +64,7 @@ struct node *mk_node(char const *name, int n, ...) {
5464
unsigned sz = sizeof(struct node) + (n * sizeof(struct node *));
5565
struct node *nn, *nd = (struct node *)malloc(sz);
5666

57-
printf("# New %d-ary node: %s = %p\n", n, name, nd);
67+
print("# New %d-ary node: %s = %p\n", n, name, nd);
5868

5969
nd->own_string = 0;
6070
nd->prev = NULL;
@@ -70,8 +80,8 @@ struct node *mk_node(char const *name, int n, ...) {
7080
va_start(ap, n);
7181
while (i < n) {
7282
nn = va_arg(ap, struct node *);
73-
printf("# arg[%d]: %p\n", i, nn);
74-
printf("# (%s ...)\n", nn->name);
83+
print("# arg[%d]: %p\n", i, nn);
84+
print("# (%s ...)\n", nn->name);
7585
nd->elems[i++] = nn;
7686
}
7787
va_end(ap);
@@ -95,8 +105,8 @@ struct node *ext_node(struct node *nd, int n, ...) {
95105
unsigned sz = sizeof(struct node) + (c * sizeof(struct node *));
96106
struct node *nn;
97107

98-
printf("# Extending %d-ary node by %d nodes: %s = %p",
99-
nd->n_elems, c, nd->name, nd);
108+
print("# Extending %d-ary node by %d nodes: %s = %p",
109+
nd->n_elems, c, nd->name, nd);
100110

101111
if (nd->next) {
102112
nd->next->prev = nd->prev;
@@ -110,13 +120,13 @@ struct node *ext_node(struct node *nd, int n, ...) {
110120
nodes->prev = nd;
111121
nodes = nd;
112122

113-
printf(" ==> %p\n", nd);
123+
print(" ==> %p\n", nd);
114124

115125
va_start(ap, n);
116126
while (i < n) {
117127
nn = va_arg(ap, struct node *);
118-
printf("# arg[%d]: %p\n", i, nn);
119-
printf("# (%s ...)\n", nn->name);
128+
print("# arg[%d]: %p\n", i, nn);
129+
print("# (%s ...)\n", nn->name);
120130
nd->elems[nd->n_elems++] = nn;
121131
++i;
122132
}
@@ -129,9 +139,9 @@ int const indent_step = 4;
129139
void print_indent(int depth) {
130140
while (depth) {
131141
if (depth-- % indent_step == 0) {
132-
printf("|");
142+
print("|");
133143
} else {
134-
printf(" ");
144+
print(" ");
135145
}
136146
}
137147
}
@@ -140,24 +150,29 @@ void print_node(struct node *n, int depth) {
140150
int i = 0;
141151
print_indent(depth);
142152
if (n->n_elems == 0) {
143-
printf("%s\n", n->name);
153+
print("%s\n", n->name);
144154
} else {
145-
printf("(%s\n", n->name);
155+
print("(%s\n", n->name);
146156
for (i = 0; i < n->n_elems; ++i) {
147157
print_node(n->elems[i], depth + indent_step);
148158
}
149159
print_indent(depth);
150-
printf(")\n");
160+
print(")\n");
151161
}
152162
}
153163

154-
int main() {
164+
int main(int argc, char **argv) {
165+
if (argc == 2 && strcmp(argv[1], "-v") == 0) {
166+
verbose = 1;
167+
} else {
168+
verbose = 0;
169+
}
155170
int ret = 0;
156171
struct node *tmp;
157172
memset(pushback, '\0', PUSHBACK_LEN);
158173
/* rsdebug = 1; */
159174
ret = rsparse();
160-
printf("--- PARSE COMPLETE: ret:%d, n_nodes:%d ---\n", ret, n_nodes);
175+
print("--- PARSE COMPLETE: ret:%d, n_nodes:%d ---\n", ret, n_nodes);
161176
if (nodes) {
162177
print_node(nodes, 0);
163178
}

0 commit comments

Comments
 (0)