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");

In reply to Re: Lexical Analyzer with Actions : Continue Development? by ikegami
in thread Lexical Analyzer with Actions : Continue Development? by Velaki

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.