4 ms·
I'm a bit disappointed about grammars. Usually, when I try to use them, it gets difficult. Especially when recursion is involved. Lately, for instance, I was
by grondilu 9y ago
I'm a bit disappointed about grammars. Usually, when I try to use them, it gets difficult. Especially when recursion is involved. Lately, for instance, I was reading the ECMAScript 2016 reference, and in it they define a repetition of elements with recursion[1]:
ArgumentList:
AssignmentExpression
ArgumentList , AssignmentExpression
I've never managed to do something like that with Perl 6's grammar. Basically every time I try to use recursion, it doesn't quite work unless I do some tricks.
Maybe I ask too much, I don't know.
1. http://www.ecma-international.org/ecma-262/7.0/index.html#sec-ecmascript-language-lexical-grammar http://www.ecma-international.org/ecma-262/7.0/index.html#se...
- Ultimatt 9y agoHuh? Not sure I know what you mean, because something like the following trivially works? grammar LOL { rule TOP { <string> } rule string { <[a..z]><string>? } } my @lulz = LOL.parse("abcdef"); say @lulz; --- [「abcdef」 string => 「abcdef」 string => 「bcdef」 string => 「cdef」 string => 「def」 string => 「ef」 string => 「f」]
- grondilu 9y agoI didn't say recursion never works. I say most of the times, it doesn't. There is no alternation in the example you provide, for instance. Had you written it like this: grammar { rule TOP { <string> } rule string { <[a..z]> | <string><[a..z]> } } as inspired by the rule mentioned above in the ECMAscript reference, it would fail.
- zengargoyle 9y agoI'm probably off the mark a little bit.... but 'rule' implies that whitespace is really <.ws> (totally ignored white space matching thingy). '|' is the Longest-Match-First (different from Perl/PCRE/etc). LMF is not possible in the case where it is not a DFA... the implicit <.ws> that is there by using 'rule' and having a space might be your problem... :) TL;DR Don't forget that 'rule' does <.ws> magic. This may or may not actually be part of the problem... I have seen a similar sort of thing and only half understand the answer i got.. Check your implicit <.ws> and '|' LMF.
- grondilu 9y agoI had tried replacing 'rule' by 'token' and it did not change much.