11 ms·
Is 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
by dmytrish 9y ago
Is 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...