News for hackers. Stuff that matters.
Re:How Difficult is it to Write a Compiler? by russell on Monday December 15, 2008 @04:55PM (#398887)
He's grandstanding, for example "limited error checking." I have written commercial compilers for a computer manufacturer and a number of translators, DSL compilers, and an application generation framework. I wrote a Pascal to C translator in a week, but it took another month's work before it could translate any Pascal program, not just MY programs. The reason it took only a week is that Pascal's data structures and types map easily to C. Most importantly, I wanted to get rid of Pascal's variety of strict typing. It is brain damaged. By using C, I let the C compiler do the heavy lifting like code optimization. If I had been trying to map Python to Java, it would have been way more difficult, because of the typing mismatch. Python has dynamic typing and Java has static typing. Python to Java is so difficult that I doubt any sane person would attempt it.

Yes, you can write a compiler in a few days, if you have done it before, if your target language semantics are compatible, if you wave you hands about usability, if you are the only user, and you don't give a rat's ass about performance. If you want to translate to a low level virtual machine like JVM or to machine binary, you have a lot more work to do: compiler optimization, instruction scheduling, register scheduling, etc.

Tratt's three steps are something that you might find in a Wikipedia article (but I haven't looked.) Generally you don't create a parse tree, unless are doing something like mapping back into a newer version of the target language or doing sophisticated macro expansion. Using Bison or a similar parser generator, you directly create an Abstract Syntax Tree (AST). Using the AST, you perform semantic checking (has the variable been declared, type compatibility in expressions...); language based optimizations like removing redundant expressions, loop hoisting, tail recursion; generate abstract machine code. Using the AMC, do machine specific optimizations like instruction scheduling (reordering to take advantage of the architecture); register scheduling; and finally generate the binary. All of which take more than a couple of days

Re:How Difficult is it to Write a Compiler? by akeefer on Monday December 15, 2008 @07:23PM (#399023)
Both JRuby and Jython compile dynamic languages into the JVM; the instruction set of the VM makes it fairly difficult to deal with dynamic types and to deal with reloading methods on the fly (I believe that in Java the only real way to unload a class is to throw away the whole classloader associated with it). If you're really curious about how it's done, check out Charles Nutter's blog (http://blog.headius.com/2008/09/first-taste-of-invokedynamic... is pretty interesting) or download the JRuby source and check it out.

That said, it's certainly not trivial, so it still might not qualify as "sane."

Re:How Difficult is it to Write a Compiler? by russell on Monday December 15, 2008 @10:47PM (#399164)
Sorry. I meant source to source, as in Python source to Java source. Python to java byte code, as in Jython, is a reasonable thing to do, but not easy.
Re:How Difficult is it to Write a Compiler? by kragen on Tuesday December 16, 2008 @01:00PM (#399742)
I think "if you wave your hands about usability, if you are the only user, and you don't give a rat's ass about performance," is sufficient --- Ur-Scheme took me a few weeks, and the source and target languages were fairly different, and I hadn't done it before. It compiles its subset of Scheme about 100× slower than gas assembles assembly language.

If you have actual compiler-writing tools then it doesn't have to take weeks or even days to write a compiler for a simple language --- if you don't care about performance, if the backend language isn't too much of a pain in the ass, if error reporting is not a major concern.

Thing is, though, performance doesn't matter any more. Oh, sometimes it does, yes. If you're writing a C compiler for a computer manufacturer, it certainly does. (Tip: port GCC instead.) But all those people who are using Ruby or Python or Perl or PHP? They aren't going to care if your compiler generates code that's 10× slower than it could be, because it's still 10× faster than what they're used to, and you can probably get them not to care that your compiler executes a million instructions per line of code that it compiles.

Re:How Difficult is it to Write a Compiler? by tome on Monday December 15, 2008 @05:43PM (#398944)
Python to Java what is difficult? Python code to Java code, maybe, but not interpretation. The Jython interpreter is written in Java.
Re:How Difficult is it to Write a Compiler? by dill_day on Monday December 15, 2008 @07:11PM (#399007)
Re:How Difficult is it to Write a Compiler? by tome on Tuesday December 16, 2008 @04:21AM (#399364)
Thanks!