Polish Notation Grammar


Formalising the above rules further, and using some regular expressions, we can write a final grammar for the language of polish notation as follows. Read the below code and verify that it matches what we had written textually, and our ideas of polish notation.

  1. /* Create Some Parsers */
  2. mpc_parser_t* Number = mpc_new("number");
  3. mpc_parser_t* Operator = mpc_new("operator");
  4. mpc_parser_t* Expr = mpc_new("expr");
  5. mpc_parser_t* Lispy = mpc_new("lispy");
  6. /* Define them with the following Language */
  7. mpca_lang(MPCA_LANG_DEFAULT,
  8. " \
  9. number : /-?[0-9]+/ ; \
  10. operator : '+' | '-' | '*' | '/' ; \
  11. expr : <number> | '(' <operator> <expr>+ ')' ; \
  12. lispy : /^/ <operator> <expr>+ /$/ ; \
  13. ",
  14. Number, Operator, Expr, Lispy);

We need to add this to the interactive prompt we started on in chapter 4. Put this code right at the beginning of the main function before we print the Version and Exit information. At the end of our program we also need to delete the parsers when we are done with them. Right before main returns we should place the following clean-up code.

  1. /* Undefine and Delete our Parsers */
  2. mpc_cleanup(4, Number, Operator, Expr, Lispy);

I’m getting an error undefined reference to `mpc_lang'

That should be mpca_lang, with an a at the end!