To expand on chromatic's comment about "too much statefulness", a parser like this would be built something similar to how strtok() works in C. Basically, you create a regex that contains a list of all valid tokens and you iterate over it with /G. The problem here is that "list of all valid tokens" bit. Because, frankly, some tokens are only tokens in the context of other tokens (usually before, but sometimes after or even elsewhere). So, another approach is to start defining patterns. For example, /(class)\s+(\S+)\s*;(.*)endclass/ (or somesuch). Except, that now becomes recursive and you still have the problem of defining all the legal patterns and keeping track of all the state.

The pattern approach is probably what I'd start out with if I was undertaking your project and I really didn't feel that I could use a grammar. Do you have a grammar for the original language? Not all languages (for example, Perl) have a grammar.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

In reply to Re: Help Creating a Code Filter by dragonchild
in thread Help Creating a Code Filter by chon

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.