Alternate java language - java

Are there compilers around that use a syntax other then the java language syntax to produce JVM bytecode?

Lots! scala, jruby, jython, Groovy, Clojure etc.
See http://en.wikipedia.org/wiki/List_of_JVM_languages

I don't know what you want to do but there are lots. You can even port Java code to other platforms like android.
Depending on what you want to do with the byte code you might like to look at Jasmin which is a Byte Code assembly language and ASM which is a byte reader/minuplator/writer.
You can even compile LOLCode based on the language of LOLCats :D
HAI
CAN HAS STDIO?
PLZ OPEN FILE "LOLCATS.TXT"?
AWSUM THX
VISIBLE FILE
O NOES
INVISIBLE "ERROR!"
KTHXBYE

Plenty. Look at this list of JVM languages for some examples.

There are plenty of them ...
Lisp : Lisp as an Alternative to Java
Scala,
Jython,
JRuby,
Groovy etc ..
Have a look at the following link :
Modern Alternatives to Java

Have you looked at Scala?
Alternatively, there is JRuby, Groovy and lots more arriving all the time. Scala seems to be the one that gets the most press at the moment though.

Related

Is it possible to automatically convert java code to PHP?

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.

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.

Can a second implementation of a programming language be written in that language?

This question is just something that I have been thinking about lately. Can a programming language be written in that language as a second implementation? e.g. Java. Is it possible to rewrite the java programming language using the java programming language?
Apologies if this is a silly question but I need to know!
GF
Always. Any Turing-Complete language is -- well -- a Turing-Complete language. If you can write the compiler in one complete language, you can write it in any equivalent language.
Yes, it is possible. Check out BootStrapping.
Yes for any Turing Complete language. Lisp comes to mind as one of the easiest languages to write an interpreter/compiler for itself.
It can. A recent example is that python has pypy. A little more information is on the Wikipedia page and some good links.
Sure.
Many many years ago one of my first home computers, a Vic 20, came with a built-in BASIC interpreter but that was it. So I wrote the first version of an assembler for it in BASIC. Then I used my first primitive assembler to write a better assembler.
Yes. As long as the language is Turing Complete, you can implement the language in itself.
There are many practical examples of this, one example is the Oberon Language, which is of interest in this discussion because the compiler code is very readable it's in the book Project Oberon available for free:
http://www.oberon.ethz.ch/bibliography/publications
http://en.wikipedia.org/wiki/Bootstrapping_(compilers)
The GCC compilers are written in C.
It has been a long time since anyone built any C compilers from assembly.
Not just possible, but for native-code compilers, this is the most common implementation technique. A good how-to guide is Andrew Appel's paper Axiomatic Bootstrapping: A Guide for Compiler Hackers.
It not only can, it is. ecj (Eclipse's compiler) is one example, and I think the SDK itself comes with a pure Java compiler, though I could be wrong about that.
write a java compiler in java - no problem at all. actually I think Sun's javac is written in java.
however, 'java' usually means more things than just the javac, so your question isn't very clear.
Sure. I've even seen someone write a COBOL compiler written in COBOL! (OK, not a full compiler ... but at least a parser.)
Check out 3-LISP

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,

Has any language been implemented in Java?

Do you know of a runtime written in Java/J2ME, that is capable of reading and executing a script/binary file?
Wikipedia has a complete list. However, you sound like you're probably interested in Jython and JRuby.
I wrote just such a language designed to be small enough for J2ME, and to not use reflection/code generation/etc...
http://www.hecl.org
It's open source under a liberal license, so you're welcome to take it, study it, include it in your own programs, or hack it to make it behave like you want.
For 'regular' Java, there are other languages that do more and are faster and more complete.
I know of an x86 emulator written in Java, JPC
Many JVM Languages - Clojure, for example. There are pretty much hundreds of JVM languages floating around, most of which were implemented in Java - Scala, Rhino, etc.
In terms of unique languages, the major ones are Clojure and Scala. Additionally, there are ports of many major languages to the JVM platform, mostly high-level languages. These include Ruby -> JRuby, Python -> Jython, and JavaScript -> Rhino. A more complete list is here.
This is an impressive list of programming languages for the Java virtual machine :
Programming languages for the Java Virtual Machine JVM
The problem is that j2me can be too limited in its use of reflection to enable this, so you need to investigate your specific target.
In terms of java in general, there are many, such as JRuby, Beanshell, Jython, etc.
I just listened to a Software Engineering Radio podcast where a Sun developer talked about Maxine which is a JVM that is mostly implemented in Java itself. It was a very interesting interview and technology.
So it's feasible that someday Java itself (meaning the standard JVM) will be implemented in Java much like C compilers are written in C (after a bit of bootstrapping).

Categories

Resources