I'm trying to work out how to use Parse::RecDescent and am a little overwhelmed.

I can see how to use it as a recognizer, but to do a simple syntax-directed translation, all of the *items, $return, $text, real return value, etc, stuff has thrown me off.

Let's say you want a translator for languages over ('a','b','c','d','e') such that any balanced pairs of equal length sequences of a's and b's are replaced by c's and d's.

my $start = "eeeeaaaabbbeeee"; my $end = translate($start); # $end eq "eeeeacccdddeeee" (aaabbb -> cccddd)

(Side note: This is the "classic example" of something that can't be done with Update: regular grammars, and needs a context-free grammar. This is because within the regexp /(a*)(b*)/ there is no way to assert that length($1) == length($2).)

The syntax-directed translation (in pseudo-code) would be:

start -> part(s) { start.t := join ('', part(s).t) } part -> AnB { part.t := AnB.t } part -> 'a' { part.t := 'a' } part -> /[^a]+/ { part.t := /[^a]+/.t } AnB -> 'a' AnB 'b' { AnB.t := 'c' . AnB.t . 'd' } AnB -> 'ab' { AnB.t := 'cd' }

Any ideas on how this translates into Parse::RecDescent? Is there a more appropriate parsing module to use for cases where the input language is very similiar to the output language?

-Andrew.


In reply to Parse::RecDescent for simple syntax-directed translation by tomazos

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.