From cc2f58454e17a6212a23c27e70e6afcf1e054346 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 17 Nov 2007 22:58:49 +0100 Subject: [PATCH] Added a simple judge on sequences of tokens. --- judge/Makefile | 3 ++- judge/judge-tok.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++ judge/judge.h | 2 +- judge/test-tok.c | 1 + judge/token.c | 12 ++++++++-- 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 judge/judge-tok.c diff --git a/judge/Makefile b/judge/Makefile index e5fcce7..57a688e 100644 --- a/judge/Makefile +++ b/judge/Makefile @@ -2,12 +2,13 @@ CC=gcc-4.1.1 CFLAGS=-O2 -Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Wundef -Wredundant-decls -Winline $(DEBUG) -std=gnu99 CFLAGS+=-Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers -all: test-io test-tok +all: test-io test-tok judge-tok JLIB=utils.o io.o token.o test-io: test-io.o $(JLIB) test-tok: test-tok.o $(JLIB) +judge-tok: judge-tok.o $(JLIB) clean:: rm -f `find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core` diff --git a/judge/judge-tok.c b/judge/judge-tok.c new file mode 100644 index 0000000..bb4e95f --- /dev/null +++ b/judge/judge-tok.c @@ -0,0 +1,58 @@ +/* + * A judge comparing two sequences of tokens + * + * (c) 2007 Martin Krulis + * (c) 2007 Martin Mares + */ + +#include +#include + +#include "judge.h" + +static int trailing_nl(struct tokenizer *t) +{ + // Ignore empty lines at the end of file + if (t->token[0]) + return 0; + t->flags &= ~TF_REPORT_LINES; + return !get_token(t); +} + +int main(int argc, char **argv) +{ + struct tokenizer t1, t2; + int report_lines = 1; + + if (argc != 3 && argc != 4) + die("Usage: judge-tok [-n] "); + + // Check for -n parameter + report_lines = !(argc == 4 && !strcmp(argv[1], "-n")); + + tok_init(&t1, sopen_read(argv[argc-2])); + tok_init(&t2, sopen_read(argv[argc-1])); + if (report_lines) + t1.flags = t2.flags = TF_REPORT_LINES; + + for (;;) + { + char *a = get_token(&t1), *b = get_token(&t2); + if (!a) + { + if (b && !trailing_nl(&t2)) + tok_err(&t1, "Ends too early"); + break; + } + else if (!b) + { + if (a && !trailing_nl(&t1)) + tok_err(&t2, "Garbage at the end"); + break; + } + else if (strcmp(a, b)) + tok_err(&t1, "Found <%s>, expected <%s>", a, b); + } + + return 0; +} diff --git a/judge/judge.h b/judge/judge.h index 4290b44..3ac8e4c 100644 --- a/judge/judge.h +++ b/judge/judge.h @@ -78,7 +78,7 @@ enum tokenizer_flags { void tok_init(struct tokenizer *t, struct stream *s); void tok_cleanup(struct tokenizer *t); -void tok_err(struct tokenizer *t, char *msg) NONRET; +void tok_err(struct tokenizer *t, char *msg, ...) NONRET; char *get_token(struct tokenizer *t); // Parsing functions diff --git a/judge/test-tok.c b/judge/test-tok.c index 10e7b3b..92303bb 100644 --- a/judge/test-tok.c +++ b/judge/test-tok.c @@ -8,6 +8,7 @@ int main(void) struct tokenizer t; tok_init(&t, i); + // t.maxtoken = 1000; t.flags = TF_REPORT_LINES; char *tok; while (tok = get_token(&t)) diff --git a/judge/token.c b/judge/token.c index 2f43ddc..69b9398 100644 --- a/judge/token.c +++ b/judge/token.c @@ -4,7 +4,9 @@ * (c) 2007 Martin Mares */ +#include #include +#include #include #include #include @@ -28,9 +30,15 @@ void tok_cleanup(struct tokenizer *t) free(t->token); } -void tok_err(struct tokenizer *t, char *msg) +void tok_err(struct tokenizer *t, char *msg, ...) { - die("%s:%d: %s", t->stream->name, t->line, msg); + va_list args; + va_start(args, msg); + printf("%s:%d: ", t->stream->name, t->line); + vprintf(msg, args); + putchar('\n'); + va_end(args); + exit(1); } static inline int is_white(int c) -- 2.39.5