5 ms·
Reminds me of perl6. Why don't more languages support grammars like this? Fun example: https://github.com/perl6/perl6-examples/blob/master/categories/interpret
by wcummings 10y ago
Reminds me of perl6. Why don't more languages support grammars like this?
Fun example: https://github.com/perl6/perl6-examples/blob/master/categories/interpreters/lisp.pl#L34 https://github.com/perl6/perl6-examples/blob/master/categori...
- throwaway7645 10y agoUsually they do through a PEG library if you search for that. Lua has a nice one and Python a few ok ones. They aren't native to the language like Rebol and Perl6 though.
- wcummings 10y agoYeah, I meant natively.
- throwaway7645 10y agoOh, yea. P6, Rebol, Red...any lisp with a little work.
- draegtun 10y agoAnd Perl5. Here are two examples of grammar engines built out of perl regex... * Regexp::Grammars - https://metacpan.org/pod/Regexp::Grammars https://metacpan.org/pod/Regexp::Grammars * Parse::RecDescent - https://metacpan.org/pod/Parse::RecDescent https://metacpan.org/pod/Parse::RecDescent
- throwaway7645 10y agoYea, not native which is what the poster ended up meaning, but I hear that one you linked from Damian Conway is great.
- draegtun 10y agoNo external library is used in these modules just Perl5 native regex engine. NB. I think thats also same for Parsec (built out of Haskell's parser combinators) BTW - Damian created both of them, Regexp::Grammars was his replacement for his Parse::RecDescent.
- throwaway7645 10y agoInteresting. I didn't know they had no dependencies. Is it still considered native though since you have to import another module? Rebol & Perl6 don't require that although I'm probably grasping at straws lol. Thanks for pointing out Damian replaced Parse::RecDescent with the other one as I was reading doc on it today at work.
- draegtun 10y agoYou can write grammars in pure Perl5 regex but it can get a bit fugly :) Here's a (very) simple example I did in Perl5 - https://news.ycombinator.com/item?id=6895126 https://news.ycombinator.com/item?id=6895126 And here's a (nicer!) port to Rebol I also did - https://www.reddit.com/r/programming/comments/1smpa1/why_rebol_red_and_the_parse_dialect_are_cool/ce035cv/ https://www.reddit.com/r/programming/comments/1smpa1/why_reb...
- throwaway7645 10y agoThank you!
- draegtun 10y agoHere's that fun example written in Rebol parse: digits: charset "1234567890" char: charset [#"a" - #"z" "+/-*"] ;; chars allowed in a symbol not-quot: complement charset {"} esc-string: [{\"} | not-quot] bool: ["#t" | "#f"] float: [opt "+-" any digits "." some digits] number: [opt "+-" some digits] symbol: [some char] quot: [#"'" [sexp | atom]] string: [{"} any esc-string {"}] statement: [sexp | atom | space | newline] sexp: ["(" any statement ")"] atom: [bool | float | number | quot | symbol | string] lisp-rule: [some statement] NB. I wrote this "off the cuff" so I don't expect it to be flawless! Also there are no capture rules provided in my example (unlike perl6 grammars which provides this automatically).
- yellowapple 10y agoIt's worth noting that this grammar is a bit more restrictive than the Perl 6 equivalent. In particular, "\d" in Perl includes not only 0 through 9, but any Unicode character that's classified as a "digit". The Perl 6 grammar documentation features this in an example of using arbitrary methods in grammars: https://docs.perl6.org/language/grammars.html#Methods_in_Grammar https://docs.perl6.org/language/grammars.html#Methods_in_Gra...
- draegtun 10y agoI think the Perl 6 example should really use [0-9] instead of \d because it's unlikely that unicode numerals are allowed (in the Common Lisp spec). Anyway neither example fully covers the Common Lisp spec for a number atom (for eg. scientific notation or rationals).
- yellowapple 10y agoI don't think covering all of the Common Lisp spec is required to be Greenspun-compliant ;)
- draegtun 10y agoIn that case Rebol is already Greenspun-compliant ;-) (print (multiply (add 1 2) 3)) ... will print an answer of 9
- heimp 10y agoRascal MPL is another one that comes to mind. http://www.rascal-mpl.org/ http://www.rascal-mpl.org/