News for hackers. Stuff that matters.
Re:PEG Parsers by mamcx on Tuesday July 23, 2019 @12:47AM (#20504452)
Wait... what is a "cut"? A minimal sample that show how it help with the error messages?
Re:PEG Parsers by drothlis on Tuesday July 23, 2019 @06:39AM (#20505713)
Cut means "if you matched this far, don't consider other options even if the current option fails to parse".

With this grammar:

    expr: expr '+' term | term
And this input:

    5 +
It will first consider the first alternative (expr '+' term) but will fail to match that branch because it's missing the "term" after the "+". That's fine because there's another alternative to try (term). That alternative doesn't match either, and you'll get the error message from the last alternative that it tried (probably something like "unexpected +").

But with this grammar:

    expr: expr '+' <cut> term | term
Then with the same input ("5 +") it will consider the first alternative, fail to match, and then fail immediately with the error message from that first alternative (something like "missing term after +").
Re:PEG Parsers by mamcx on Wednesday July 24, 2019 @11:01AM (#20515924)
yeah, I get the purposed point of a "cut" but how I put one? Exist a basic sample? (ie: Using a peg parser how define what is a "cut"???)

    expr: expr '+' API_call_here? term | term
Re:PEG Parsers by pacaro on Wednesday July 24, 2019 @01:00PM (#20517174)
Hmm, if you're using a peg parser that doesn't directly support this you can emulate it by choosing between the rhs of the cut and a production that will always match

so

  expr : atom (op <cut> expr)*
Would be replaced with

  expr : atom (op (expr / cut))*
  cut  : "XXX"?
or perhaps simpler

  expr : atom (op expr?)*
Either way when you process the expr rule there will be an empty match where the rhs should be, and an appropriate error can be generated
Re:PEG Parsers by pacaro on Tuesday July 23, 2019 @11:32AM (#20507545)
Drothlis comment describes it well, but to perhaps explain where the term comes from, Prolog has a related concept and calls it the cut operator