Hi all again,
Thanks for the great input. Based on your comments, I've given the code another go. This time I've "compiled" the code into the form (op_name, arglist...), which is then run using an "eval". I've also establised a symbol table hash (which currently holds the lvalue type and value). I'd like to have your opinion on this design. (Oh yeah, the grammar syntax is still very ugly and basic. Needs to be redone.)
Thanks for your time.
use Parse::RecDescent; use vars qw(@lines $curline %stash); use strict; $RD_HINT++; $RD_WARN++; #$RD_TRACE++; sub do_print { my $a=shift; print "$a\n"; } sub do_goto { $curline = int(shift)-1; } my $grammar = q{ Start: Expression(s) /\Z/ Expression: LineNum Print { $::lines[int($item{LineNum})]= ["print", [$item{Print}]]; } | LineNum Goto { $::lines[int($item{LineNum})]= ["goto", [$item[2]]]; } | LineNum Ass | <error> Ass: StringAss | NumAss StringAss: Identifier /\$/ /=/ /\"/ String /\"/ { $::stash{$item{Identifier}}{type}="String"; $::stash{$item{Identifier}}{value}=$item{String}; } NumAss: Identifier /=/ Number { $::stash{$item{Identifier}}{type}="Number"; $::stash{$item{Identifier}}{value}=$item{Number}; } Goto: /goto/i LineNum Print: /print/i /\"/ String /\"/ { $item{String}; } | /print/i Identifier /\$/ { if(defined($::stash{$item{Identifier}}{value})) { $::stash{$item{Identifier}}{value}; } else {die "A nasty and horrible death... Bad string identifier.\n" +;} } | /print/i Identifier { if(defined($::stash{$item{Identifier}}{value})) { $::stash{$item{Identifier}}{value}; } else {die "A nasty and horrible death... Bad number identifier.\n" +;} } String: /[\w\s]+/ LineNum: /\d+/ Identifier: /[a-zA-Z][a-zA-Z0-9]*/ Number: /[+-]?\d+(\.\d+)?/ }; my $parser = new Parse::RecDescent($grammar); undef $/; my $text = <>; $parser->Start($text); $curline=0; while(1) { if($lines[$curline]) { my $todo = "do_".${$lines[$curline]}[0]."(\""; my $args = join(', ', @{${$lines[$curline]}[1]}); $todo = $todo.$args."\")"; eval $todo; } last if $curline>$#lines; $curline++; }

Here are some test files:
test1.bas
5 print "This " 10 print "is " 15 print "a " 20 print "test" 25 goto 5

test2.bas
5 a$ = "This is a string" 10 print a$ 15 b = 35 20 print b

In reply to Re: Re: Goto with Parse::RecDescent by bent
in thread Goto with Parse::RecDescent by bent

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.