0

I'm trying to use MPC to define a grammar for a language called Wittgen (https://esolangs.org/wiki/Wittgen)

I defined the following grammar:

mpc_parser_t* Variable        = mpc_new("variable");
mpc_parser_t* Assign_Operator = mpc_new("assign");
mpc_parser_t* Remind_Operator = mpc_new("remind");
mpc_parser_t* Expr            = mpc_new("expr");
mpc_parser_t* Envinronment    = mpc_new("envinronment");

mpca_lang(MPCA_LANG_DEFAULT,
  " variable     : /[a-zA-Z0-9]+/ ;"                                                     
  " assign       : '=' ;"                                         
  " remind       : '@' ;"                                                                
  " expr         : <variable> | <remind> <variable> '}' | <variable> <assign> <expr>+ '}' ;"
  " envinronment : /^/<expr>+/$/ ;",
  Variable, Assign_Operator, Remind_Operator, Expr, Envinronment);

when I try to input a variable or a remind operator (like "foo247" or "@foo247}") it parses it correctly, but when I try to parse an assignment ("foo247=foo}"), it returns me just

WITTGEN> foo357=foo}
<stdin>:1:7: error: expected one of 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ', one or more of one of 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ', '@' or end of input at '='

I can't find the error, I'm sure something is wrong defined in the grammar, but I can't find any clue in the official documentation or in the examples

GodTaxist
  • 71
  • 6
  • So, you provide MPC with a grammar and it parses. But it appears to have a lot of runtime overhead. Why are you using this, rather than a more conventional parser generator (e.g., "yacc") where you give it a grammar, and it parses, without all that overhead? Even if you need it at runtime, you can always shell out to a make script that runs the parser generator for you. – Ira Baxter Jan 05 '17 at 09:21
  • http://buildyourownlisp.com/ this was the reason, moreover it's very easy to use compared to yacc or antlr – GodTaxist Jan 05 '17 at 14:37
  • Why does "building LISP with C" require you to use a runtime configurable parser? Lisp syntax is *simple* and *static*, and especially at the level you are talking about here, could be coded directly in C in about 2 hours. You're just adding a peculiar parser to your process. "Easy compared to ANTLR?" How so? I don't buy your argument. Is this based on long experience with MPC, ANTLR and YACC? – Ira Baxter Jan 05 '17 at 15:31
  • Is there a real point in your comments or are you just trying to start a useless flame war? – GodTaxist Jan 05 '17 at 16:38
  • Believe it or not, I think I'm trying to help. I have a LOT of experience with parsers and parser generators. I just don't see the point of MPC or how it really provides you with any help over the many other available approaches. With that, I'll shut up. – Ira Baxter Jan 05 '17 at 16:52

2 Answers2

0

I'm not an expert on mpc and I may be wrong, in fact I'm having my own problems with it at the moment, but I don't think it supports left recursion. So since expr is contained within the expr rule it causes an error.

Edit* I was able to solve my issue by moving part of my expansion over. So the equivalent for yours would be to move variable all the way to the right so it tries to parse using the other two expansions first. I can't say for sure if that's causing your issue, but it could be worth a shot.

Dustin Gulley
  • 123
  • 1
  • 7
0

I had my question answered from the author of mpc here:

I simply changed the part of rule definition from

" expr         : <variable> | <remind> <variable> '}' | <variable> <assign> <expr>+ '}' ;"

to:

" expr         : <remind> <variable> '}' |  <variable> <assign> <expr>+ '}' | <variable>;"

it was happening because there's no backtracking in mpc, so the evaluation rule order is important

GodTaxist
  • 71
  • 6