5 ms·
I looked quickly through the slides and saw that perl has now grammar build in the language... Truly an interesting feature
by txdv 9y ago
I looked quickly through the slides and saw that perl has now grammar build in the language...
Truly an interesting feature
- perlgeek 9y agoIt is. You can build recursive descending parsers with a declarative syntax, and all the benefits from having it in OO (role composition, inheritance). Here is a working grammar for INI config files: https://github.com/tadzik/perl6-Config-INI/blob/master/lib/Config/INI.pm#L5 https://github.com/tadzik/perl6-Config-INI/blob/master/lib/C...
- dmytrish 9y agoIs the language itself a good place for parsing/grammar handling? I could not even find in the standard documentation which kind of parsers it supports, is it LR(k)? LL(k)? Any CFG? What parsing method does it use: recursive descent, shift-reduce, something more general like CYK/Earley algorithms? How does it handle ambiguities? It turns out that `grammar` keyword generates recursive descent parsers and anything even a bit fancier still requires a dedicated parsing library.
- riffraff 9y agoperl6 grammars have to be seen in context of perl5 regexp. Basically, regular expressions have grown to impossible to understand, write and modify. Mostly due to perl. Yet, they are used daily by millions of developers, because they are a ton better than manual character checks. Perl6 grammars are basically an attempt to bring developers another step forward.
- vram22 9y agoAre you saying that Perl6 grammars are a replacement (maybe eventually) for Perl5 regexps?
- riffraff 9y agopartly, you still build Grammars out of "token", "regex" and "rule" definitions, but some things are more natural with one or the other. So, if you are doing "test if string has whitespace" you'd use a regex, while for "parse an apache log line" you'd probably use a grammar instead of a very nasty regex. IANA perl6 dev though, just a casual observer.
- kbenson 9y ago> while for "parse an apache log line" you'd probably use a grammar instead of a very nasty regex. That's actually a fairly easy endeavor with perl regex capabilities, especially named captures. Parsing HTML, or a programming language, or other much more complex structures is much easier with a grammar. For example, JSON parsing[1]. That said, there are ways to approximate some features of grammars with Perl5 regexes[2]. 1: https://github.com/moritz/json/blob/master/lib/JSON/Tiny/Grammar.pm https://github.com/moritz/json/blob/master/lib/JSON/Tiny/Gra... 2: http://blogs.perl.org/users/brian_d_foy/2013/10/parsing-json-with-a-single-regex.html http://blogs.perl.org/users/brian_d_foy/2013/10/parsing-json...
- vram22 9y agoThanks, riffraff and kbenson.
- raiph 9y ago> It turns out that `grammar` keyword generates recursive descent parsers Yes, P6 grammars generate recursive ascent/descent parsers. (It allows arbitrary mixes of ordinary P6 closures, using the 'Main' DSL's syntax, with parsing P6 closures, using the 'Regex' DSL's parsing syntax. Purely declarative parts of the latter are true regular expressions and are mapped to automata by the compiler.) > and anything even a bit fancier still requires a dedicated parsing library. Huh? Please explain what it is about the Perl 6 grammar (the grammar for Perl 6 that is itself a Perl 6 grammar) that isn't fancy. In my recent SO post [1] I wrote "In most cases, the practical answer when you want to parse anything beyond the most trivial of file formats -- especially a well known format that's several decades old -- is to find and use an existing parser.". Is this what led to your "bit fancier" conclusion about P6 parsing? > Is the language itself a good place for parsing/grammar handling? Yes. Not only does this allow devs to more easily write and use parsing code, just as P5 made it easier to write and use regexes, but it also makes it easier to mutate Perl 6 and write DSLs. [1] https://stackoverflow.com/questions/45172113/extracting-from-bib-file-with-perl-6/45181464#45181464 https://stackoverflow.com/questions/45172113/extracting-from...
- throwaway7645 9y agoThere is an old (infoq? I think) presentation with Damian Conway where he calls it a new paradigm "grammar based programming" where you write a grammar parser object easily using the built-in functionality and then add a little code to get with it and BAM you're finished. I think this elegantly solves quite a few programming tasks of mine.