Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Language Conversion

by Steve_BZ (Chaplain)
on Sep 09, 2009 at 14:59 UTC ( #794377=perlquestion: print w/ replies, xml ) Need Help??
Steve_BZ has asked for the wisdom of the Perl Monks concerning the following question:

I wrote a little macro (in Komodo) to convert some VBScript subroutines to Perl (see below). It missed a few things, but then executing the Perl in the debugger soon got it working and it was much quicker than converting by hand. I wondered if anyone else has written routine to translate between other languages. Specifically, I'm looking for something to take the pain out of converting a few subroutines into C or C++ for my current Perl project for security reasons.

Here it is:

// Macro recorded on Thu Aug 13 2009 22:13:22 GMT-0300 komodo.assertMacroVersion(2); if (komodo.view && komodo.view.scintilla) { komodo.view.scintilla.focu +s(); } Find_ReplaceAllInMacro(window, 1, '\'', '#', true, 0, 2, true, false); Find_ReplaceAllInMacro(window, 0, '\'', '#', true, 0, 2, true, false); Find_ReplaceAllInMacro(window, 0, 'If', 'if (', true, 0, 2, true, fals +e); Find_ReplaceAllInMacro(window, 0, 'then ', ') {', true, 0, 2, true, fa +lse); Find_ReplaceAllInMacro(window, 0, 'then', ') {', true, 0, 2, true, fal +se); Find_ReplaceAllInMacro(window, 0, 'End If', '}', true, 0, 2, true, fal +se); Find_ReplaceAllInMacro(window, 0, 'End if', '}', true, 0, 2, true, fal +se); Find_ReplaceAllInMacro(window, 0, 'End IF', '}', true, 0, 2, true, fal +se); Find_ReplaceAllInMacro(window, 0, 'Else', '} else {', true, 0, 2, true +, false); Find_ReplaceAllInMacro(window, 0, 'Function', 'sub', true, 0, 2, true, + false); Find_ReplaceAllInMacro(window, 0, 'Fn_', '__', true, 0, 2, true, false +); Find_ReplaceAllInMacro(window, 0, 'Loc_', '$loc_', true, 0, 2, true, f +alse); Find_ReplaceAllInMacro(window, 0, 'Len(', 'length(', true, 0, 2, true, + false); Find_ReplaceAllInMacro(window, 0, 'Mid(', 'substr(', true, 0, 2, true, + false); Find_ReplaceAllInMacro(window, 0, 'mid(', 'substr(', true, 0, 2, true, + false); Find_ReplaceAllInMacro(window, 0, 'Left(', 'substr(', true, 0, 2, true +, false); Find_ReplaceAllInMacro(window, 0, 'true', '1', true, 0, 2, true, false +); Find_ReplaceAllInMacro(window, 0, 'TRUE', '1', true, 0, 2, true, false +); Find_ReplaceAllInMacro(window, 0, 'false', '0', true, 0, 2, true, fals +e); Find_ReplaceAllInMacro(window, 0, 'FALSE', '0', true, 0, 2, true, fals +e); Find_ReplaceAllInMacro(window, 0, '<>', '!=', true, 0, 2, true, false) +; Find_ReplaceAllInMacro(window, 0, ' mod ', ' % ', true, 0, 2, true, fa +lse);

Comment on Language Conversion
Download Code
Re: Language Conversion
by halfcountplus (Hermit) on Sep 09, 2009 at 15:41 UTC
    If you don't find anything, I would start by writing C/C++ functions to replace all the perl built-ins used. This is a very very complicated project IMO.

    Hashes, if not basically all variables, will have to be the product of some function, as will regex's. I do not think any kind of "find and replace" strategy will work very well in the end: you should investigate lexing and parsing if you are not already familiar with the topic. This might give you the general idea, read supplementary to something else:

    http://www.intergate.com/~halfcountplus/CBCP/parsing.html

    (there's some simple examples in C of how a language parser works, based on the "Dragon Book" stuff mentioned by almut; fascinating and fun -- good luck!)
      ...for further reading i.e. as the exercise for the interested reader, Steve_BZ could do worse than get a copy of the O'Reilly lex & yacc nutshell reference - IMO, most enlightening.

      A user level that continues to overstate my experience :-))

        ...for even further reading, the next step would be the classical Dragon book on compiler design...

        Essentially, a compiler translates from one language into another — typically from a higher level language to a lower one, but there's no reason (in theory) it couldn't convert from Perl to C++ source. It's just that compilers are usually non-trivial to write, so no one may have done it yet...

        In this particular case (as parsing Perl is a particularly non-trivial task), it would probably be a good idea to reuse existing code (such as the regular source of the Perl interpreter, or PPI) for the side of parsing and constructing a syntax tree.  You'd "just" :) have to add code generation for the target language from the syntax tree, paying particular attention on how to realize the highly dynamic facilities which Perl provides in a more "static" language like C/C++.

Re: Language Conversion
by ikegami (Pope) on Sep 09, 2009 at 16:38 UTC

    Specifically, I'm looking for something to take the pain out of converting a few subroutines into C or C++ for my current Perl project for security reasons.

    "For security reasons"?

    I don't understand that, especially since every single piece of Perl code is implemented in C already. The only thing that's not in C is the order in which the C code is to be executed.

      s/For security reasons/to obfuscate and placate/g


      -Waswas
        No, just interfaces to other systems.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2015-07-03 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls