Added flags to language specifiction. Added optional expect string to language specification. Added some exaple grammars for testing and demos

This commit is contained in:
Daniel Holden
2014-01-26 11:25:50 +00:00
parent 8931782986
commit d5e2bdf977
17 changed files with 942 additions and 269 deletions

47
examples/maths.c Normal file
View File

@@ -0,0 +1,47 @@
#include "../mpc.h"
int main(int argc, char **argv) {
mpc_parser_t *Expr = mpc_new("expression");
mpc_parser_t *Prod = mpc_new("product");
mpc_parser_t *Value = mpc_new("value");
mpc_parser_t *Maths = mpc_new("maths");
mpca_lang(MPC_LANG_PREDICTIVE,
" \
expression : <product> (('+' | '-') <product>)*; \
product : <value> (('*' | '/') <value>)*; \
value : /[0-9]+/ | '(' <expression> ')'; \
maths : /^/ <expression> /$/; \
",
Expr, Prod, Value, Maths);
if (argc > 1) {
mpc_result_t r;
if (mpc_parse_contents(argv[1], Maths, &r)) {
mpc_ast_print(r.output);
mpc_ast_delete(r.output);
} else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
} else {
mpc_result_t r;
if (mpc_parse_pipe("<stdin>", stdin, Maths, &r)) {
mpc_ast_print(r.output);
mpc_ast_delete(r.output);
} else {
mpc_err_print(r.error);
mpc_err_delete(r.error);
}
}
mpc_cleanup(4, Expr, Prod, Value, Maths);
return 0;
}