Parsing Expressions
Because we’re now thinking in S-Expressions, and not Polish Notation we need to update our parser. The syntax for S-Expressions is simple. It is just a number of other Expressions between parentheses, where an Expression can be a Number, Operator, or other S-Expression . We can modify our existing parse rules to reflect this. We also are going to rename our operator
rule to symbol
. This is in anticipation of adding more operators, variables and functions later.
mpc_parser_t* Number = mpc_new("number");
mpc_parser_t* Symbol = mpc_new("symbol");
mpc_parser_t* Sexpr = mpc_new("sexpr");
mpc_parser_t* Expr = mpc_new("expr");
mpc_parser_t* Lispy = mpc_new("lispy");
mpca_lang(MPCA_LANG_DEFAULT,
" \
number : /-?[0-9]+/ ; \
symbol : '+' | '-' | '*' | '/' ; \
sexpr : '(' <expr>* ')' ; \
expr : <number> | <symbol> | <sexpr> ; \
lispy : /^/ <expr>* /$/ ; \
",
Number, Symbol, Sexpr, Expr, Lispy);
We should also remember to cleanup these rules on exit.
mpc_cleanup(5, Number, Symbol, Sexpr, Expr, Lispy);
当前内容版权归 orangeduck 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 orangeduck .