perl2py - In progress! :-)

François Pinard pinard at iro.umontreal.ca
Thu Jul 26 10:25:21 EDT 2001


Hi, people.

I merely want to announce that I started a Perl to Python converter,
and slowly begin to feel it _might_ end up into something usable.

I'm using the nice SPARK environment, authored by John Aycock, for analysing
Perl sources.  In fact, this `perl2py' project has, for me, the main goal
of getting more experience with SPARK.  I'm not in great need for `perl2py',
as I already rewritten "by hand" the bulk part of my own Perl code base.

The problem is a bit interesting, as many Perl syntax peculiarities, and
language concepts are, hum, unusual.  Doing the whole of Perl is serious
undertaking, I'm not sure I want to attempt this.  But if I can produce a
mix of Python and Perl which is Python oriented enough, the remaining of
the translation could be easier, maybe still tedious.  Too soon to tell...

Someone asked me, in a previous email, how I would do with Perl "eval".
In fact, this one looks easy in Python: one imports the translation module
from `perl2py', gets a Python source string out of the Perl string, and
"exec" the result.  (One problem might be that the name clash resolution
choices are not around anymore, at execution time.)

If one seeks difficulties against this project, I easily found one: I do
not have the slightest idea about how to proceed about Perl "goto"s!

Here is where that "progress" stands:

* Scanner: a good part of it is written.  Some difficult issues remain, like
  the disambiguation of '/' from patterns, and various `s' like operators.
  For sure, it will never be fully Perl compatible, yet I think we could
  get similar interpretations most of times, in practice.

* Parser: a good part of it is written, among which the complex hierarchy
  of expressions and the strange rules about commas.  A few things are
  still missing, to be added along the way.  I'm not sure how stable is the
  parser behaviour if I alter the grammar, this is part of my SPARK learning.

* Semantic: almost nothing written.  The main goals are to resolve name
  clashes for user variables, infer scalar or string context, get clues
  about variable types, and how references could be simplified (maybe).

* Translation: not much written.  In fact, the Parser does some on-the-fly
  rewriting, to fake that the original Perl source was slightly Pythonic.
  Semantic driven translation will require work.  SPARK offers me some AST
  matching, but not the AST rewriting facilities I would like; or maybe I
  did not understand this correctly yet.  I'll have to figure out something.

* Generator: the textual mechanics is written and works well, it includes a
  simple template mechanism well suited for Python, giving me macros.
  Remain to see how to equilibrate in-line expansions versus library calls,
  and how to manage temporary variables for in-line expansions, this has
  to be managed with the Translation step.

The whole thing is inter-connected enough so I could use it to develop.
It currently produces a mix of garbage and Perl fragments within Pythonic
structures, enough for me to check how well or bad Perl was analyzed.
My projection is that, once the main translation engine gets ready,
the overall translator will grow big and boring, yet this growth will
not significantly slow SPARK down.  Why big and boring?  Perl is like an
adventure game: a twisted maze of features, all alike, all different. :-)

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list