toolset: parser and unparser

We can achieve our goal with following main routines for the parser resp. unparser.

Intended usage from the shell command line

$ echo '(1+1) * (1+1)' | parser | unparser
or even:
$ echo '(1+1) * (1+1)' | parser | unparser | parser | unparser

Parser main routine:


#include "y.tab.h" /* yacc (token) definitions */ #include "k.h" /* data type defs */ #include "csgiok.h" /* structure file io defs */ expr the_expr; /* root of the parse tree */ int main() { char *io; /* error message, or == 0 */ yyparse(); /* invoke parser */ io = CSGIOwrite_expr(stdout, the_expr); if (io != 0) /* error! */ fatal("Write error: %s\n", io); exit(0); /* success! */ }
Unparser main routine:

#include "k.h" /* data type defs */ #include "csgiok.h" /* structure file io defs */ #include "unpk.h" /* unparse (view) defs */ #include "printer.h" /* printer function def */ int main() { char *io; /* error message, or == 0 */ expr the_expr; /* root of the parse tree */ io = CSGIOread_expr(stdin, &the_expr); if (io != 0) /* error! */ fatal("Read error: %s\n", io); unparse_expr(the_expr, printer, base_view); exit(0); /* success! */ }
Where routine fatal takes a printf-like list of arguments which are printed to stderr, after which it exits with an error code.


next overview up
Last updated at 03 February '98 by kimwitu@cs.utwente.nl