First of all, the fix is this:
Cons: $/.len = { $/; $<head>.len + $<tail>.len }
As you can see mentioning $/, the attribute's invocant fixes this.

You're probably asking yourself why...

use Data::Dumper; $Data::Dumper::Deparse = 1; warn Dumper($grammar->{engine}{cases}{Cons});
Comparing the two versions shows us something interesting:
$_AG_ATTR->get($_AG_SELF)->get('len')->set(sub { $_AG_SELF; $_AG_N1->get('len', 'grammar line 4') + $_AG_N2->get('len', 'gramm +ar line 4'); }
versus
$_AG_ATTR->get($_AG_SELF)->get('len')->set(sub { $_AG_N1->get('len', 'grammar line 4') + $_AG_N2->get('len', 'gramm +ar line 4'); }
So, what is the void thingy doing in there, you are probably wondering? Aha!
'visit' => [ sub { ... # snipped my($_AG_SELF, $_AG_ATTR) = @_; ... # snipped $_AG_ATTR->get($_AG_SELF)->get('len')->set(sub { $_AG_SELF;
Basically, $_AG_SELF causes the value to remain non-garbage collected (since it's captured in the closure it's reference count stays up). Since luqui is using a lazy evaluation strategy to produce results, and this probably means that somewhere inside $_AG_ATTR or whatever there's a table of objects to their attr values, the moment that $_AG_SELF dies (and it will die, because you are creating Cons thingies on the fly inside 'children') it can no longer be referenced in a thunk, because it's StrVal cannot be reproduced. Anyway, long story short, the evaluation scheme is somehow landing on an edge case with two children in the Cons/Nil thing, and thus it's causing the value to be recalculated over and over and over and over again. I think.

The more elegant fix is to cache your linked list, something like

sub children{ return @{ $_[0]{_ll_cache} ||= [ list( @{ $_[0]->{_list} } ) ] }; }
so that the values don't go away. Remember to invalidate _ll_cache whenever new children are added.

Ciao!

-nuffin
zz zZ Z Z #!perl

In reply to Re: Trees and Language::AttributeGrammar by nothingmuch
in thread Trees and Language::AttributeGrammar by rg0now

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.