http://qs1969.pair.com?node_id=555986

tomazos has asked for the wisdom of the Perl Monks concerning the following question:

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.