#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; } FILE *in = fopen(argv[1], "r"); if (in == NULL) { fprintf(stderr, "Could not open file \"%s\"\n", argv[1]); return -1; } struct kai_tag_t *document; yyscan_t scanner; kalmialex_init(&scanner); kalmiaset_in(in, scanner); int result = kalmiaparse(scanner, &document); kalmialex_destroy(scanner); fclose(in); if (result != 0) { return result; } print_tag("", document); print_level(1, document->children); kai_tag_destroy(document); return 0; }