From ea512c3b1d2d85ff76aa7d4979c73dd7f1217757 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 3 Feb 2023 22:34:04 -0600 Subject: begin refactor with bison xml parser --- yy/demo.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ yy/main.c | 65 ------------------------------------------------------------- yy/makefile | 8 ++++---- 3 files changed, 69 insertions(+), 69 deletions(-) create mode 100644 yy/demo.c delete mode 100644 yy/main.c (limited to 'yy') diff --git a/yy/demo.c b/yy/demo.c new file mode 100644 index 0000000..52058d3 --- /dev/null +++ b/yy/demo.c @@ -0,0 +1,65 @@ +#include + +#define YYLTYPE KALMIALTYPE +#define YYSTYPE KALMIASTYPE +#include "kalmia.tab.h" +#include "kalmia.lex.h" + + +void print_attrs(struct kai_attr_t *attr) +{ + while(attr != NULL) { + printf("%s=\"%s\" ", attr->key, attr->value); + attr = attr->next; + } +} + + +void print_tag(char *indent, struct kai_tag_t *tag) +{ + printf("%s%s[ ", indent, tag->type); + print_attrs(tag->attrs); + printf("]\n"); +} + + +void print_level(int indent_level, struct kai_tag_t *tag) +{ + char indent[64]; + for (int i=0; ichildren != NULL) { + print_level(indent_level+1, tag->children); + } + if (tag->content != NULL) { + printf("%s\t%s\n", indent, tag->content); + } + tag = tag->next; + } +} + + +int main(int argc, char **argv) +{ + if (argc < 2) { + fprintf(stderr, "You must specify a file to parse!\n"); + return -1; + } + + struct kai_tag_t *document = kai_parse_file(argv[1]); + if (document == NULL) { + return -1; + } + + print_tag("", document); + print_level(1, document->children); + + kai_tag_destroy(document); + + return 0; +} diff --git a/yy/main.c b/yy/main.c deleted file mode 100644 index 52058d3..0000000 --- a/yy/main.c +++ /dev/null @@ -1,65 +0,0 @@ -#include - -#define YYLTYPE KALMIALTYPE -#define YYSTYPE KALMIASTYPE -#include "kalmia.tab.h" -#include "kalmia.lex.h" - - -void print_attrs(struct kai_attr_t *attr) -{ - while(attr != NULL) { - printf("%s=\"%s\" ", attr->key, attr->value); - attr = attr->next; - } -} - - -void print_tag(char *indent, struct kai_tag_t *tag) -{ - printf("%s%s[ ", indent, tag->type); - print_attrs(tag->attrs); - printf("]\n"); -} - - -void print_level(int indent_level, struct kai_tag_t *tag) -{ - char indent[64]; - for (int i=0; ichildren != NULL) { - print_level(indent_level+1, tag->children); - } - if (tag->content != NULL) { - printf("%s\t%s\n", indent, tag->content); - } - tag = tag->next; - } -} - - -int main(int argc, char **argv) -{ - if (argc < 2) { - fprintf(stderr, "You must specify a file to parse!\n"); - return -1; - } - - struct kai_tag_t *document = kai_parse_file(argv[1]); - if (document == NULL) { - return -1; - } - - print_tag("", document); - print_level(1, document->children); - - kai_tag_destroy(document); - - return 0; -} diff --git a/yy/makefile b/yy/makefile index a2022a2..6567b56 100644 --- a/yy/makefile +++ b/yy/makefile @@ -1,6 +1,6 @@ WARNINGS = -Wall -Wextra -Wpedantic -all: kalmia +all: kalmia.tab.c kalmia.lex.c kalmia.tab.c: kalmia.y bison -o "$@" --header=kalmia.tab.h kalmia.y @@ -8,8 +8,8 @@ kalmia.tab.c: kalmia.y kalmia.lex.c: kalmia.l flex -o "$@" --header-file="kalmia.lex.h" kalmia.l -kalmia: kalmia.tab.c kalmia.lex.c main.c - gcc -g -o kalmia ${WARNINGS} main.c kalmia.tab.c kalmia.lex.c +demo: kalmia.tab.c kalmia.lex.c demo.c + gcc -g -o demo ${WARNINGS} demo.c kalmia.tab.c kalmia.lex.c clean: - rm kalmia.lex.c kalmia.lex.h kalmia.tab.c kalmia.tab.h kalmia + rm kalmia.lex.c kalmia.lex.h kalmia.tab.c kalmia.tab.h demo -- cgit v1.2.1