in reply to Lexical Analyzer with Actions : Continue Development?

My version:

use strict; use warnings; my %VALID_ELEMENTS = map { $_ => 1 } qw( b uc ); my %state; my @to_close; sub output_text { my ($text) = @_; $text =~ s/([[:print:]])/$1$1/g if $state{b}; $text = uc($text) if $state{uc}; print($text); } sub open_tag { /\G ( \w+ ) /xgc or die("Expecting tag name\n"); my $ele = lc($1); $VALID_ELEMENTS{$ele} or die("Unknown element name $ele\n"); /\G > /xgc or die("Expecting closing bracket\n"); !$state{$ele} or die("Attempting to open previously opened element\n"); $state{$ele} = 1; push @to_close, $ele; } sub close_tag { /\G (\w+) /xgc or die("Expecting tag name\n"); my $ele = lc($1); $VALID_ELEMENTS{$ele} or die("Unknown element name $ele\n"); /\G > /xgc or die("Expecting closing bracket\n"); $state{$ele} or die("Attempting to close unopened element\n"); my $to_close = $to_close[$#to_close]; $to_close eq $ele or die("Missing closing tag for element $to_close\n"); $state{$ele} = 0; pop @to_close; } sub process { # The following "for" aliases $_ to $text and # provides a target for the upcoming "redo" stmts. for ($_[0]) { /\G <\/ /xsgc && do { close_tag(); redo }; /\G < /xsgc && do { open_tag(); redo }; /\G ( [^<]+ ) /xsgc && do { output_text("$1"); redo }; # Falls through at the end of the string. } } process("This is a nifty <b><uc>uppercase</uc> test</b> to see what <u +c>this</uc> thing can do.\n");

Replies are listed 'Best First'.
Re^2: Lexical Analyzer with Actions : Continue Development?
by Velaki (Chaplain) on Sep 18, 2006 at 10:54 UTC

    Yes, I noticed that, too -- the exponential code lines to cover the complex tag combinations; hence, a parser is needed at that point. The thing is that the tags, in the order I see them, are much simpler, and the "grammar" I'm using is such that I was hoping to get away with a simple lexer. As for why I'm not using the other tools out there, as you read in my original post, I'm not in an environment where I can install CPAN modules at will. However, thanks for the ideas. From what everyone has said, it appears that not only am I reinventing the wheel, but my wheel is pretty awful.

    I think my module will find a happy home in the coding graveyard, but might prove useful enough for the project for which it was created.

    Thanks again,
    -v

    "Perl. There is no substitute."