3 ms·
Not directly related, but I've been looking for a human-friendly DSL for creating simple parsers. Can anyone recommend me one? I'm looking for something that w
by fiatjaf 9y ago
Not directly related, but I've been looking for a human-friendly DSL for creating simple parsers. Can anyone recommend me one?
I'm looking for something that would take a parser definition like
<user> (likes|prefers) <fruit>
and generate a parser that will turn
Joanna likes apples
into
{"user": "Joanna", "fruit": "apples"}
Does that make sense? I know it is easy to write something like that, but maybe there is already something battle-tested out there.
- ttd 9y agoFor your example, a regular expression with named capture groups would be all you'd need, e.g.: https://pythex.org/?regex=%5E(%3FP%3Cuser%3E.%2B%3F)%20(likes%7Cprefers)%20(%3FP%3Cfruit%3E.%2B%3F)%24&test_string=Joanna%20likes%20apples&ignorecase=0&multiline=0&dotall=0&verbose=0 https://pythex.org/?regex=%5E(%3FP%3Cuser%3E.%2B%3F)%20(like... If you start to need more complex features in your grammar such as recursive nonterminals, there are many parser generators out there that support input in EBNF, which (in my opinion) is quite human-readable.
- ezekg 9y agoI built a command line tool awhile back for doing things like this: https://github.com/ezekg/xo https://github.com/ezekg/xo You could do something like, echo 'Joanna likes apples' | xo '/^(\w+) (?:likes|prefers) (\w+)/{"user":"$1","fruit":"$2"}/i'