in reply to Keyword parser function mapping module

Parse::RecDescent is the universal response to everyone needing a fast implementation of *jet another*(tm) mini-language.

BEGINNER
I would translate it into code employing some CPAN module mentioned above.

ELITE
I would translate it into xml and then translate that again into code (with XSLT) employing some CPAN module mentioned above. *Yeah, throw your eggs on me*

Cheers, Murat
  • Comment on Re: Keyword parser function mapping module

Replies are listed 'Best First'.
Re: Re: Keyword parser function mapping module
by dragonchild (Archbishop) on Jan 08, 2004 at 13:13 UTC
    Why on earth would you want the overhead and complication of Parse::RecDescent for a simple macro language?? Gods, NO!

    What's wrong with something like this:

    sub parse_commands { my ($filename) = @_; my $fh = IO::File->new($filename) || die ...; while (<$fh>) { # All commands being with a letter and must be the first chara +cter next if /^[^A-Za-z]/; # A command is space-delimited and must be on one line. # We want to deal with the entire command in uppercase. my ($command, @line) = split /\s+/, uc; unless (exists $DispatchTable{$command}) { warn "'$command' isn't a valid command.\n"; next; } push @commands, [$command, \@line]; } return \@commands; }

    All of a sudden, you have a parsed list of commands. Obviously, you'd do something a little more in-depth when dealing with WHILE/ENDWHILE, IF/ELSE/ENDIF, SUB/ENDSUB, and FOR/ENDFOR, but it's not that much more complicated. It can even be a one-pass compiler if you

    • do all includes during the parsing phase (this requires reading the initial file first into an array for splicing)
    • require all subroutines be defined before usage (if SUB is even allowed)
    • require all labels to be on a line by themselves. (Something along the lines of /^([A-Z]\w*):$/)
    • don't care if a label is defined until runtime.

    If interested, I'll write one. Remember - the handler for each command is the one that cares about parameters and the like. This isn't a fully-featured language (though it is Turing-complete).

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.