step: generalize grammar

This commit is contained in:
NunoSempere 2023-04-30 23:58:31 -04:00
parent e5913ce78f
commit 007741dad0

View File

@ -179,19 +179,21 @@ int main(int argc, char** argv)
/* Create Some Parsers */
mpc_parser_t* Number = mpc_new("number");
mpc_parser_t* Operator = mpc_new("operator");
mpc_parser_t* Symbol = mpc_new("symbol");
mpc_parser_t* Sexpr = mpc_new("sexpr");
mpc_parser_t* Expr = mpc_new("expr");
mpc_parser_t* Mumble = mpc_new("mumble");
/* Define them with the following Language */
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+/ ; \
operator : '+' | '-' | '*' | '/' ; \
expr : <number> | '(' <operator> <expr>+ ')' ; \
mumble : /^/ <operator> <expr>+ | <expr>/$/ ; \
" \
number : /-?[0-9]+/ ; \
symbol : '+' | '-' | '*' | '/' ; \
sexpr : '(' <expr>* ')' ; \
expr : <number> | <symbol> | <sexpr> ; \
mumble : /^/ <expr>* /$/ ; \
",
Number, Operator, Expr, Mumble);
Number, Symbol, Sexpr, Expr, Mumble);
// Initialize a repl
@ -230,7 +232,7 @@ int main(int argc, char** argv)
}
/* Undefine and Delete our Parsers */
mpc_cleanup(4, Number, Operator, Expr, Mumble);
mpc_cleanup(4, Number, Symbol, Sexpr, Expr, Mumble);
return 0;
}