Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Code Generation

by hackdaddy (Hermit)
on Dec 16, 2005 at 08:00 UTC ( [id://517164]=perlquestion: print w/replies, xml ) Need Help??

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

Tonight I wrote a Perl script to perform code generation the old-fashioned way by concatenating strings and pushing the strings on an array of text lines.

I used Java code as input, parsed it, and generated XML and Java code for a project at work. (Don't ask me why I am doing this, it's just what the project requires.) I had to do some mapping between function calls. The XML describes function calls but its types are much more primitive. I used a hash to map between types.

Anyway, I can sense there is a better way to generate chunks of XML and Java code using object-oriented Perl.

It would be nifty if I could build an Esperanto or intermediate object and then output it as XML or Java code as needed.

Any ideas or code samples are appreciated. Thanks.

Replies are listed 'Best First'.
Re: Code Generation
by spurperl (Priest) on Dec 16, 2005 at 09:36 UTC
    I don't think you defined your problem well enough to get detailed advice.

    Some points are quite unclear:

    • What is Esperanto ? (I assume you don't mean the spoken language)
    • Did you actually write code that parses Java ? Using a module ? A full parser or some partial regex-hack ?

    Anyways, an intermediate representation for Java code, like for any code really, is a syntax tree. For XML, a good internal representation is the one used by Perl's modules - if your XML isn't too sophisticated, the HoAoH(...) approach of XML::Simple may be what you're looking for.

      When I said Esperanto, I meant figuratively an intermediate class that can output both XML and Java.

      I am parsing the method signatures of a Java class and creating XML that describes the signature. I am also creating Java from the signatures to create Java code for another Java class.
Re: Code Generation
by GrandFather (Saint) on Dec 16, 2005 at 11:11 UTC

    If you use XML as the "Esperanto" the generation of the XML version is fairy simple and with tools like XML::TreeBuilder and XML::Twig, parsing the "Esperanto" (XML) is easy and, very likely, given a good design for the XML, the Java drops out easily too.


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://517164]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2024-04-24 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found