Is it possible to automatically convert java code to PHP? - java

Is there are tools for converting java code to php? I have source code of java library and I need it to convert to php.

It is possible to automatically convert it. This is called a source to source compiler. Normally when you compile software, the parser will build an abstract syntax tree and convert this into the target machine language code. But it is just as possible to have a compiler convert this into another high level (compilable) language.
Java is a strongly typed language, and PHP is not, so source to source compilers are rare and the code conversion process is incomplete. However this said, there is a reasonably good one with a free demo at: http://javatophp.com

Automatically - No. Now. Maybe in future. Don't spend time, write new code bro.

I don't think there is a solution like this currently.
You might try using a php-java bridge that would allow you to call the java code from within PHP:
http://php-java-bridge.sourceforge.net/pjb/
Zend Server also provides a bridge

Team of 5 folks at Facebook have spent 18 month to write sofrware that converts PHP to C++ (meet: HipHop). There is no such software for transforming from Java to PHP yet.
The answer is: yes... it is possible if you have year and a half and team of pro programmers :)
Otherwise, you rewrite it manually (I think, this is your choise).

There are lots of aspects of Java that cannot be expressed in PHP. Type safety for one. This sounds like a fool's errand to me. If you were looking to go in the opposite direction the question might have some interest.

Related

Syntax Preprocessors for Java

I'm looking for a Java macro language that provides for convenient ways of doing closures (that compile to anonymous inner classes) and list comprehension (that compiles down to basic java loops).
An example of the kind of thing I'm looking for would be Xtend2 http://www.eclipse.org/Xtext/#xtend2
But I want something for general purpose programming (Xtend2 is very specific DSL for Xtext and has a ton of dependencies). Maybe even something that would let me define multiple classes in a single file (which would then get split up into two separate files by the pre-processor).
Does anything like this exist?
Edited to add:
I'm doing Android development so any alternatives have to generate either valid Java source or the byte code has to be compatible with the dalvik recompiler.
Mmm, there used to be the JSE, which was tremendous fun, back in the day.
Mirah is cool, but not ready for primetime, IMO.
You can do a lot with smart templating, although your source view is the Java.
There's a post on SO about using XTend on Android from a few days ago, too.
Frege produces java source code.
I do not know whether dalvik would like it. (But I would be interested to hear ...)
And, of course, you have some runtime library code.
That being said, there are a number of other projects that do closures etc. in java, for example: lambdaj

Java to C/C++, or at least get the Java converted code

Are there any Java -> C/C++ Converters? Well I expect a no.
But I know Java works by converting the Java Byte Code, into code that the OS can understand using JIT. So is there any way to get this "converted code"?
Thanks.
Thanks to Baltasarq, who set me on the right course, I starting looking for Ahead of Time compilers, Amazingly, I found GCJ which is included in GCC (I think the latest?). It does exactly what I want. Take a Java file, turn it into an EXE. But, it needs 44 DLLS for a simple print "Hello World" app. Oh well :D
But I know Java works by converting the Java Byte Code, into code that the OS can understand using JIT. So is there any way to get this "converted code"?
You're talking about compiling code "ahead of time", or at least that's the name it receives in the Mono project (free implementation of .NET/C#). If you are interested on this, you could convert your code from Java to C# (which is at least easier than C++), and then take advantage of this feature. There is even a tool dedicated to this purpose: mkbundle.
I do not know if there is a way to get the JIT code issued ~ it is a runtime conversion that is done after like 1000 or 10,000 through some part of the code path - I am sure one can get this "converted code"! but what you are talking about is source-code and anyway the JVM runs on the byte code for 1000 or 10,000 before it even tries to bring int the JIT or can be made to run on byte code only
seems to me without knowing where your needs are I would think it faster to write the code by hand ~ if I need a short loop or something to explain something to some bode I can do it faster by hand than finding some tool to do it
there actually is a dumper that comes as part of a standard install ~ and I know it works because I have some code saved in source file comments where it did it
Are there any Java -> C/C++ Converters? Well I expect a no.
Well, there are some projects, but none I know of that are production-quality. Mostly they translate to C, as the additional features that C++ offers do not align very well with what Java does. A Google search will give you quite a few projects. Also see this question:
Does a Java to C++ converter/tool exist?
But I know Java works by converting the Java Byte Code, into code that
the OS can understand using JIT. So is there any way to get this
"converted code"?
No, not that I am aware of. You could theoretically take the HotSpot source code (it's available as part of OpenJDK), and insert logging statements to dump the generated machine code. I don't know if anyone has done that yet.

Actionscript to java source converter

is there a tool which can convert actionscript3 source code to java source?
I ran into this page awhile ago because I needed an actionscript to java converter too. I wrote one myself. It's very simple and requires manual adaptation after conversion (as most such converters do), but it does a good job of automating the boring stuff. You can find it on
my blog.
I believe Haxe (which has similar syntax to AS3, also based on the ECMA Specification) is able to compile to Java (as well as a slew of other languages and bytecodes). It requires learning a new language, but it may be worth it.
You should explain why you want to convert from AS3 to Java. I think, the typically way is to convert from Java to AS3, e.g. with Gas3.
You will be able to do this via Haxe in near future. First use as3hx to convert AS3 to Haxe, and then you can convert Haxe to Java via Haxe compiler. Haxe/Java was promised to be relased in this year.

Programming an Interpreter for a Compiler

I'm writing an interpreter for a compiler program in Java. So after checking the source code, syntax and semantics, I want to be able to run the source code, which is the input for my compiler. I'm just wondering if I can just translate some tokens, for example, out (it prints stuff on screen), can I just replace it with System.out.print? then feed the source code again to run it in java?
I've heard of using the Java Compiler API, would this be a good plan?
Thank you very much in advance!
What you asking is a virtual machine implementation technique, to run your Java code in general you should implement following:
The first few steps I guess you already done (Design/describe the language semantics, construct AST and perform required validation of the code)
You need to generate your byte code, original Java works exactly in the same way, it generates another representation of the source code, from human readable to machine readable.
Here you can see how Java byte code looks like http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/
You need to implement virtual aka stack machine that reads byte code and runs it for execution.
So as you can see you should have 3 separated components (projects) for your task:
1. Language grammar
2. Compiler (byte code generator)
3. Virtual machine (interpreter of byte code)
P.S. I have experience in creation of tiny Java similar compiler from scratch (define grammar with ANTlr, implementation of compiler, implementation of virtual machine), so probably I can share more information with you (even source code) if you need something particular
You really need to read some books and/or take courses on compilers - this can't be solved by a two-paragraph answer on SO.
You could create a cross-compiler which reads your language and outputs Java code to do the same thing. This may be the simplest option.
The Java Compiler API can be used to compile Java code. You would need to translate your existing code to Java first to use it.
This would not be the same thing as writing an interpreter. Is this homework? Does the task say you have to write the interpreter or can you have the code run any way which works?
Unfortunately you did not mention which scripting language are you planning to support. If it is one of well known languages, just use its ready interpreter written in pure java. See BSF and Java 5 scripting (http://www.ibm.com/developerworks/java/library/j-javascripting1/)
It it is your own language
think twice: do you really need it?
If you are sure you need your own language think about JavaCC
First of all, thank you very much for the fast replies.
As part of our compiler project, we need to be able to compile and run a program written in our own specified language. The language is very similar to C. I am confused on how an interpreter works, is there a simpler way to implement this? Without generating byte codes? My idea was to translate each statement into Java equivalent statements, and let Java handle the byte code generation.
I would look into the topics mentioned. Again, thank you very much for the suggestions.

Is there a Java to Flash compiler?

GWT is pretty cool: write in Java, we build an Ajax app.
Is there something similar for Flash? Code in Java, we convert it to Actionscript?
Thanks!
I haven't use any myself but found these from osflash.org
This seems to be doing something of the sort. http://www.flagstonesoftware.com/transform/. Then there is haxe which doesn't do Java as far as I know but might be worth looking into.
UPDATE:
I just found out that ANTLR will also talks ActionScript. You'll have to define a formal grammar that translates other languages into ActionScript (http://www.antlr.org/wiki/display/ANTLR3/Antlr3ActionScriptTarget)
I'm not sure is available yet, but Joa Ebert showed a Java to SWF compiler at last Flash on the Beach. You can read about it in Compiling Java and C# to SWF.
Cheers,

Categories

Resources