Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??
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?


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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.