Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Re: recursive parsing techniques

by rob_au (Abbot)
on Dec 29, 2003 at 08:40 UTC ( #317390=note: print w/replies, xml ) Need Help??

in reply to Re: recursive parsing techniques
in thread recursive parsing techniques

In addition to the excellent comments by castaway above, I would only add that it is additionally advantageous to include an escape clause within the recursive subroutine in order to ensure that the parsing routine does pass beyond a specified recursion depth - This is a relatively simple safeguard to implement from a defensive programming standpoint.

For example, a one-liner which (eventually) exhausts the memory available to the Perl process:

rob@development:/home/rob/workspace$ perl -e 'sub a { &a }; a' Out of memory!

This can be prevented by the incorporation of a maximum depth for subroutine recursion such as follows:

sub parsevcard { my ($inputstr, $inputpos, $depth) = @_; if ($depth > $max_depth) { # Recursion beyond allowable depth error handling here } . . $currentcard->{AGENT} = parsevcard($inputstr, $pos, ++$depth); }

Depending upon the recursive problem at hand, this error trapping can be performed by way of examination of the output data structure alone, for example, the number of elements within an output array, rather than by a separate counter per se.


perl -le "print+unpack'N',pack'B32','00000000000000000000001010011110'"

Replies are listed 'Best First'.
Re: Re: Re: recursive parsing techniques
by exussum0 (Vicar) on Dec 29, 2003 at 16:30 UTC
    I originally thought this was pointless, since you can only recursively store data as far as it's actually done. But it all depends. ala...
    node1 subnode1 subsubnode1 ...
    But if your language can refer to itself...
    nodeA nodeB nodeC referenceToNodeA
    If the data's relations can be put into a graph that is not in the form of a tree, some recursive checks like the above can be invaluable. If it's in the form of a tree, w/ no cycles, things should be pretty safe so long as the data can be loaded into memory :)

    Play that funky music white boy..

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://317390]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-05-24 02:38 GMT
Find Nodes?
    Voting Booth?