error message Deep recursion on subroutine

Discussion in 'Perl Misc' started by NamSa, May 4, 2009.

  1. NamSa

    NamSa Guest

    my code printing error message this <data> 6 line it's TIME function.
    TIME function is recursive computations .
    Deep recursion on subroutine "main::Value" at toy.pl line 104, <DATA>
    line 6.
    Deep recursion on subroutine "main::Fun" at toy.pl line 40, <DATA>
    line 6.
    Deep recursion on subroutine "main::Fun" at toy.pl line 40, <DATA>
    line 6.
    Deep recursion on subroutine "main::Subst" at toy.pl line 62, <DATA>
    line 6.



    my code Lisp-Like TOY Language
    base function 2 function it's MINUS and IF. another function make is
    base IF or MINUS

    use strict;

    use warnings;



    my @fun_list = ("MINUS","IF");

    my %fun_hash = (

    MINUS => 'Minus',

    IF=> 'If',

    AA=> '(x y)(IF x y)',

    BB=> '(x y)(MINUS x y)',

    ADD=> '(x y)(MINUS x (MINUS 0 y))',

    EQUAL=> '(v2 v3)(MINUS (MINUS 1 (IF ( MINUS v2 v3) 1)) (IF (MINUS
    v3 v2) 1))',

    "IF/THEN/ELSE"=> '(v7 v8 v9)(ADD (IF v7 v8) (IF (MINUS 1 v7) v9))'

    );



    #main

    while (<DATA>){

    print Value($_) ;



    }



    sub Value{

    my $exp=$_[0];

    my $result;

    while($exp !~ /^-?\d+/){

    if (my @array = $exp =~ /\(\s*((?:(?!\s+\))[^()])+)\s*\)/xg){

    for (@array){

    $result = Fun($_);

    $exp =~ s/\(\s*$_\s*\)/$result/;

    }

    }

    }

    return $exp;

    }





    sub Fun{

    my $exp = $_[0];

    my @arg = split / /,$exp;

    for my $key (keys %fun_hash){

    if( $exp =~ /^$key/){

    if($key eq "MINUS"){

    return Minus($arg[1],$arg[2]);

    }elsif($key eq "IF"){

    return If($arg[1],$arg[2]);

    }else{

    return Subst($key,$exp);

    }

    }

    }

    }



    sub Minus{

    my $x = $_[0];

    my $y = $_[1];

    return $x-$y;

    }





    sub If{

    my $x = $_[0];

    my $y = $_[1];

    if($x <= 0){

    return 0;

    }else{

    return $y;

    }

    }



    sub Subst{

    my $key = $_[0];

    my $exp = $_[1];

    my $i=0;

    my @arg = split / /,$exp;

    my $fun = $fun_hash{$key};

    my @ar = $fun=~ /\(\s*((?:(?!\s+\))[^()])+)\s*\)/xg;

    my @arg2 = split / /,$ar[0];

    my $arg_size = @arg2;

    $fun =~ s/\(\s*$ar[0]\s*\)//g;

    for (@arg2){

    $fun =~ s/$_/$arg[$i+1]/g;

    $i++;

    }

    return Value($fun);

    }



    __DATA__

    (AA 0 5 )

    (BB 0 5 )

    (ADD 5 5 )

    (EQUAL 6 5 )

    (IF/THEN/ELSE 1 2 5)
     
    NamSa, May 4, 2009
    #1
    1. Advertising

  2. On 2009-05-04 03:18, NamSa <> wrote:
    > my code printing error message this <data> 6 line it's TIME function.
    > TIME function is recursive computations .
    > Deep recursion on subroutine "main::Value" at toy.pl line 104, <DATA>
    > line 6.


    This is not an error message, just a warning. You can turn it off with
    "no warnings 'recursion';". See perldoc warn, perldoc perllexwarn and
    perldoc perldiag for details.

    hp
     
    Peter J. Holzer, May 4, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arun
    Replies:
    1
    Views:
    459
    Lee Fesperman
    Jan 26, 2005
  2. Chet Nichols III
    Replies:
    3
    Views:
    216
    Eric I.
    Nov 14, 2007
  3. king
    Replies:
    5
    Views:
    210
  4. Replies:
    8
    Views:
    803
    John Reye
    Apr 26, 2012
  5. Tim Watts

    TIEHANDLE and deep recursion

    Tim Watts, May 9, 2012, in forum: Perl Misc
    Replies:
    11
    Views:
    888
    Tim Watts
    May 10, 2012
Loading...

Share This Page