Reference


doge_code.c

  1. #include "mpc.h"
  2. int main(int argc, char** argv) {
  3. /* Build a parser 'Adjective' to recognize descriptions */
  4. mpc_parser_t* Adjective = mpc_or(4,
  5. mpc_sym("wow"), mpc_sym("many"),
  6. mpc_sym("so"), mpc_sym("such")
  7. );
  8. /* Build a parser 'Noun' to recognize things */
  9. mpc_parser_t* Noun = mpc_or(5,
  10. mpc_sym("lisp"), mpc_sym("language"),
  11. mpc_sym("book"), mpc_sym("build"),
  12. mpc_sym("c")
  13. );
  14. mpc_parser_t* Phrase = mpc_and(2, mpcf_strfold,
  15. Adjective, Noun, free);
  16. mpc_parser_t* Doge = mpc_many(mpcf_strfold, Phrase);
  17. /* Do some parsing here... */
  18. mpc_delete(Doge);
  19. return 0;
  20. }

doge_grammar.c

  1. #include "mpc.h"
  2. int main(int argc, char** argv) {
  3. mpc_parser_t* Adjective = mpc_new("adjective");
  4. mpc_parser_t* Noun = mpc_new("noun");
  5. mpc_parser_t* Phrase = mpc_new("phrase");
  6. mpc_parser_t* Doge = mpc_new("doge");
  7. mpca_lang(MPCA_LANG_DEFAULT,
  8. " \
  9. adjective : \"wow\" | \"many\" \
  10. | \"so\" | \"such\"; \
  11. noun : \"lisp\" | \"language\" \
  12. | \"book\" | \"build\" | \"c\"; \
  13. phrase : <adjective> <noun>; \
  14. doge : <phrase>*; \
  15. ",
  16. Adjective, Noun, Phrase, Doge);
  17. /* Do some parsing here... */
  18. mpc_cleanup(4, Adjective, Noun, Phrase, Doge);
  19. return 0;
  20. }