Fork me on GitHub
#instaparse
<
2017-07-06
>
matan17:07:13

@aengelberg many thanks for the confirmation! of course, it stems from the difference between what regular expression language is and what grammars are, not from how instaparse works doesn't it 🙂 Would you agree to this?

aengelberg17:07:46

You mean regexes’ greediness? I would say that’s an artifact of regexes’ supported use case not matching what Instaparse needs it to do. If regexes had a way to lazily emit multiple parse results of the same string, we could maybe use that to make regex non-terminals behave more intuitively.

aengelberg17:07:47

e.g. if I match #"^a+" on "aaa" I’d like to see "a", "aa", "aaa"

matan17:07:15

Yes, well, at a higher level, regular expressions and grammars generate disparate automatons, whereas what you mention is one difference in the detail thereof (the most relevant one). This relates to two area I'm lately looking at ― fuzzy parsing and scannerless parsing

matan17:07:44

Oh, not really, ignore that last one

aengelberg17:07:17

That’s returning one match for each of a variety of starting indexes, which is not quite what I want

matan17:07:36

Yes, again, ignore that

aengelberg17:07:08

You could theoretically do (for [i (range (count s))] (re-match re (subs s 0 i)))

aengelberg17:07:23

but that is super slow and might not actually work depending on the regex

aengelberg17:07:22

the automaton structure of regexes is simply not designed to (efficiently) reason about the set of all possible matches for one string, using backtracking and laziness

aengelberg17:07:53

actually maybe if you compile a NDFA

aengelberg17:07:02

then run it through one character at a time

aengelberg17:07:56

and mark every time you are in a success state

matan17:07:03

The interaction of a grammar with the need to account for syntactic categories (a.k.a variables, and/or non-terminals) that translate to non-finite sets of productions is very interesting. Right now, we typically "escape" to regular expressions for that. Of course, this goes beyond the scope of my original question with its particular silly use case.

aengelberg17:07:11

In this case, the set of productions is not infinite; for the current index i, all I really need to know is, the set of indexes j \in [0,N) such that s[i:j] is a complete parse according to the regex.

matan17:07:22

Right, obviously, when looking at a specific input text (or string).

matan17:07:13

But in the general sense, I also meant to say that we use regular expressions for where we want to stipulate a non-finite set, whereas a plain CFG doesn't provide (IIRC) that kind of support, which is why we use regex along with it. I have to refresh and brush up more on formal languages though, maybe what I just said is totally incorrect, or just sufficiently inaccurate to be incorrect.