Tom Malcolmson | 27 Mar 2009 15:35

Using synopsis to translate python source code to C/C++ or Java

Hi.

It appears to me that synopsis is able to parse Python and C++.  I see
formatters for producing documentation from the AST trees but I don't
see formatters for producing source code.  Do they exist?

I do understand that the ability to correctly interpret the Python types
is limited.  That's OK - I'm not expecting a runnable program as output,
just a starting point.

I saw that someone wrote a tool for translating Java to Python:
http://blog.melhase.net/articles/2007/02/15/automated-translation-of-java-to-python
but there is nothing for translating in the other direction.

I know that there is a tool for translating from Python to C++, but that
is focussed on producing an executable from a Python program.  It only
supports the limited subset of Python that it is able to perfectly
translate into a working C++ program.  What I'm imagining is a tool that
makes a rough pass at translating any Python source code into Java or
C++ source code.
Stefan Seefeld | 27 Mar 2009 16:02
Picon
Favicon

Re: Using synopsis to translate python source code to C/C++ or Java

Hi Tom,

Tom Malcolmson wrote:
> Hi.
>
> It appears to me that synopsis is able to parse Python and C++.  I see
> formatters for producing documentation from the AST trees but I don't
> see formatters for producing source code.  Do they exist?
>   

No. The various language parsers in Synopsis first generate 
language-specific source representations (a C++ parse tree, a Python 
AST, etc.), and then translate that (or rather, a subset of it) into a 
common representation: the 'Abstract Semantic Graph' (ASG). This ASG is 
what most of the processors in Synopsis work on to generate 
documentation. I assume this level is enough if you want to generate API 
bindings (say), because for those you only care about the API 
declarations, which are adequatly represented in the ASG.
For full-blown code translation, however, this is not enough. You need 
to operate one level below, on the parse tree.

I think what you want is very reasonable, and in case of Python even 
straight-forward to do (in contrast to C++, for example).

If you are interested in such a feature I encourage you to submit a 
detailed feature request. And, if you are willing and able to help 
implement it, I would be glad to collaborate.

Thanks for your interest into Synopsis !

(Continue reading)

Tom Malcolmson | 27 Mar 2009 17:24

Re: Using synopsis to translate python source code to C/C++ or Java

On Fri, 27 Mar 2009 11:02 -0400, "Stefan Seefeld" <seefeld <at> sympatico.ca>
wrote:
> Hi Tom,
> 
> Tom Malcolmson wrote:
> > Hi.
> >
> > It appears to me that synopsis is able to parse Python and C++.  I see
> > formatters for producing documentation from the AST trees but I don't
> > see formatters for producing source code.  Do they exist?
> >   
> 
> No. The various language parsers in Synopsis first generate 
> language-specific source representations (a C++ parse tree, a Python 
> AST, etc.), and then translate that (or rather, a subset of it) into a 
> common representation: the 'Abstract Semantic Graph' (ASG). This ASG is 
> what most of the processors in Synopsis work on to generate 
> documentation. I assume this level is enough if you want to generate API 
> bindings (say), because for those you only care about the API 
> declarations, which are adequatly represented in the ASG.
> For full-blown code translation, however, this is not enough. You need 
> to operate one level below, on the parse tree.
> 
> I think what you want is very reasonable, and in case of Python even 
> straight-forward to do (in contrast to C++, for example).

Thanks for the clarification.

I'm surprised to hear you say that it would be relatively easy in
Python.  I thought that the hard part with Python is the type inference.
(Continue reading)

Stefan Seefeld | 27 Mar 2009 17:40
Picon
Favicon

Re: Using synopsis to translate python source code to C/C++ or Java

Tom Malcolmson wrote:
> I'm surprised to hear you say that it would be relatively easy in
> Python.  I thought that the hard part with Python is the type inference.
>   

You are right, and I'm sorry for not expressing myself more clearly. 
What I was thinking of when making the statement was the analysis side 
of things. It's far easier to analyse Python code than it is to analyse 
C++ code.
Of course, there are things that need to be addressed when mapping from 
one to another.

As Synopsis is able to process (and parse) comments, it would be 
possible to embed mapping directives into comments, i.e. you may 
annotate e.g. function declarations with type info, quite similar to how 
API documentation is typically written.

I haven't yet looked into Python 3 in the context of Synopsis, and so 
can't comment on how best to support the new features.

Thanks,
       Stefan

--

-- 

      ...ich hab' noch einen Koffer in Berlin...

Gmane