diff --git a/exercises/02/vslc/src/vslc.c b/exercises/02/vslc/src/vslc.c new file mode 100644 index 0000000..4a4ed91 --- /dev/null +++ b/exercises/02/vslc/src/vslc.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include + + +/* Global state */ + +node_t *root; // Syntax tree + + +/* Command line option parsing for the main function */ +static void options ( int argc, char **argv ); +bool + print_full_tree = false; + + +/* Entry point */ +int +main ( int argc, char **argv ) +{ + options ( argc, argv ); + + yyparse(); // Generated from grammar/bison, constructs syntax tree + + yylex_destroy(); // Free heap used by flex + + if ( print_full_tree ) + print_syntax_tree(); + + destroy_syntax_tree(); // In tree.c +} + + +static const char *usage = +"Command line options\n" +"\t-h\tOutput this text and halt\n" +"\t-t\tOutput the full syntax tree\n"; + + +static void +options ( int argc, char **argv ) +{ + int o; + while ( (o=getopt(argc,argv,"ht")) != -1 ) + { + switch ( o ) + { + case 'h': + printf ( "%s:\n%s", argv[0], usage ); + exit ( EXIT_FAILURE ); + break; + case 't': print_full_tree = true; break; + } + } +} +