You have this:

my @tokens = get_tokens $string; my sub parse_list { state @tokens = @_; my @list; while (my $token = shift @tokens) { if ($token eq '(') { push @list, __SUB__->(); } elsif ($token eq ')') { return \@list; } else { push @list, $token; } } return \@list; } my $list = parse_list @tokens;

Sometimes you pass tokens to it. Sometimes you don't. That's weird and confusing. Fixed:

my @tokens = get_tokens $string; my sub parse_list { my @list; while (my $token = shift @tokens) { if ($token eq '(') { push @list, __SUB__->(); } elsif ($token eq ')') { return \@list; } else { push @list, $token; } } return \@list; } my $list = parse_list;

The lack of error checking is disturbing. Fixed:

my @tokens = get_tokens $string; my sub parse_list { my @list; while ( @tokens && $tokens[0] ne ")" ) { my $token = shift( @tokens ); if ( $token eq "(" ) { push @list, __SUB__->(); die( "Missing `)`" ) if !@token; my $token = shift( @tokens ); die( "Missing `)`" ) if $token ne ")"; } else { push @list, $token; } } return \@list; }; my $list = parse_list(); die( "Unexpected `$tokens[0]`" ) if @tokens;

Without recursion:

my @tokens = get_tokens $string; my sub parse_list { my @stack; push @stack, [ ]; while ( @tokens ) { my $token = shift( @tokens ) { if ( $token eq "(" ) { my $sublist = [ ]; push @{ $stack[ -1 ] }, $sublist; push @stack, $sublist; } elsif ( $token eq ")" ) { die( "Unexpected `)` ) if @stack == 1; pop( @stack ); } else { push @{ $stack[ -1 ] }, $token; } } die( "Missing `)` ) if @stack > 1; return $stack[ 0 ]; } my $list = parse_list(); die( "Unexpected `$tokens[0]`" ) if @tokens;

In reply to Re^10: parse lisp style config by ikegami
in thread parse lisp style config by vincentaxhe

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.