orangeduck/mpc

Greedy matching causes token matching issue

joshuacrotts opened this issue · 2 comments

I'm writing an interpreter for a subset of Scheme, and have ran into a bit of a roadblock when implementing conditions, i.e., cond. The parser is greedily matching the first case of my cond rule instead of attempting to terminate the condition via "else". Here's what I mean:

"symbol : /[a-zA-Z_+\-\/\\=<>!&]+[a-zA-Z0-9_+\-\/\\=<>!&]/ ;\n"
"cond : '(' "cond" ('(' ')')+ '('"else" ')'')' ;\n"
"application : '('
')' ;\n"

Here's an example:
(cond ((= 3 4) 3) ((= 4 5) 4) (else 5))

Because "else" in the last clause is registered as a "symbol", it is getting pattern-matched as an application, meaning the entire thing is misinterpreted. I'm not sure if I just need to write my rules differently or if there's a way to non-greedily match the rule.

As stated, that cond rule only allows for strings like (cond () (else)), (cond ()() (else)) ...

Yeah, that’s something I figured out after a while of debugging. I ended up just making “cond” a semantic action rather than one that the parser should figure out.