Dear Monks!

I have largish data structures, organized into trees, and I have to perform rather complex transformations on this data. When I first encounterd with attribute grammars I immediately fell in love with the concept.

So I installed luqui's Language::AttributeGrammar, wrote a little tree data representation:

#!/usr/bin/perl use strict; use warnings; use Language::AttributeGrammar; package Leaf; sub new{ return bless { value => $_[1] }, 'Leaf'; } package Branch; sub new{ return bless { value => $_[1], _list => undef }, 'Branch'; } sub add_child{ my $self = shift; push @{ $self->{_list} }, @_; return $self; } sub list { if (@_) { bless { head => $_[0], tail => list(@_[1..$#_]) }, 'Cons'; } else { bless {}, 'Nil'; } } sub children{ return list( @{ $_[0]->{_list} }); }
and a simple attribute grammar to count the number of nodes in a tree
package main; my $grammar = new Language::AttributeGrammar <<'EOG'; Branch: $/.len = { 1 + $<children>.len } Leaf: $/.len = { 1 } Cons: $/.len = { $<head>.len + $<tail>.len } Nil: $/.len = { 0 } EOG
and an example
my $tree = Branch->new(3)->add_child( Branch->new(1.1)->add_child( Leaf->new(1), Leaf->new(1.2) ), Branch->new(2.0)->add_child( Leaf->new(2.1), # Leaf->new(2.15), Leaf->new(2.2), ) ); my $result = $grammar->apply($tree, 'len'); print "$result\n";
Everything works like charm as long as the line that is commented out remains commented out. As soon as I add the node back to the tree, I get
Deep recursion on subroutine "Language::AttributeGrammar::Thunk::get" +at (eval 29) line 5.
and the script runs into a seemingly infinite loop. Does anyone have a clue why this happens? I would greatly appreciate any ideas.

Update: sorry I mixed up: it works with 3 leafs, but stops working as soon as I comment out the offending line...

p.s.: if luqui reads this: there is a typo in the SYNOPSIS of Language::AttributeGrammar:

# find the global minimum and propagate it back down the tree ROOT: $/.gmin = { $/.min } Branch: $<left>.gmin = { $/.gmin } | $<right>.gmin) = { $/.gmin } ^ | this is superfluous

In reply to 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.