1

I'm looking to make a small compiler for personal/friend usage. Just some basic input output console functions. And all that other stuff like Strings, ints, floats, chars, bools and and so on. But I want to do this in java. I know what your going to say, I should use something practical and fast, like C. But I want a little OOP in it too. Now, you must be thinking, C++. There is one issue I have with that. It's finished programs are .exe files. Now I know that you don't need anything installed to run .exe, and to run a .jar, the file I want it to be, you need the JVM installed. But I have some friends on Apple computers, and I don't have an Apple, so I couldn't have another version written in Obj-c for those people. You can get the JVM on Apple, and even linux if you want, and this would be a cross platform. I know it would be slower, but its just a small language, and the difference is such a small amount, compapred to things like the first calcuator, that took minutes. Any and all help would be apricciated, unless the help is something like "Get a life" or "Learn C". I'm best at Java, and love the cross platform, so if you suggest anything that is not java, I'll be very annoyed. Thanks!

EDIT: Not to many people can see the question I'm asking, so here it is again. I want to know how to make A compiler in java. That's it.

The_Steve13
  • 119
  • 2
  • 11
  • 7
    Nothing prevents you from writing it in Java indeed - do you have a question? – assylias Aug 16 '12 at 15:30
  • 1
    Whats your question. And have you ever heard of line breaks to break a long text into sections to make it *readable*? – Durandal Aug 16 '12 at 15:30
  • @assylias Yes, I wanted to know how to make a compiler, and if anyone knew how. Sorry if it was not clear enough. – The_Steve13 Aug 16 '12 at 15:31
  • 1
    @Durandal The question was how to make the compiler. And yes, I know line breaks, but I only put those between seperate thoughts. – The_Steve13 Aug 16 '12 at 15:32
  • @Durandal you have like 40 different thoughts in that question. – ewok Aug 16 '12 at 15:34
  • @ewok It was one thought, with many different subthoughts, or extra points I tried to make. – The_Steve13 Aug 16 '12 at 15:35
  • @The_Steve13 Not to sound rude, but its general courtesy towards persons you communicate with to adhere to *proper* protocol. Some readers (like me) will inevitably arrive at the conclusion the writer is either stupid or lazy or both. Neither entices to read through a wall of poorly organized text, thus your chances of getting an answer are considerably reduces. Thats not even counting your question is missing *vital* details. – Durandal Aug 16 '12 at 15:39
  • @Durandal, noone is forcing you to waste your precious time commenting on how he could've asked the question - instead, rely on other people understanding and answering his question... So yea steve13, you'll need plenty of things. Mainly, start with a scanner (for syntax), a parser (for semantics), learn how to do accept valid input and THEN get into making a compiler... – Shark Aug 16 '12 at 15:50
  • @Shark I don't feel I've wasted my time. Or rather I am on SO to *waste* my time, so don't you worry about *my* time xD. I'm still surprised that people try answering it while to TC still hasn't even specified *what* he wants to compile and what the *target* is :) – Durandal Aug 16 '12 at 15:57
  • Heh, same here... I've got some background in this matter so I'm giving out general tips. Once he makes the SymbolTable and ensures that fields can shadow one another properly, it doesn't matter WHAT he's compiling or the TARGET he's compiling against... – Shark Aug 16 '12 at 16:00

4 Answers4

10

Have a look at these tutorial(s)/link(s):

If you want to create a compiler to compile your own language you'll have to be doing more work then creating a compiler. You need language syntax, heaps, stacks, mnemonics, op-codes, debuggers etc and lastly the compiler.

If you however want to create your own custom compiler for the Java language see:

should do it.

You might look at the Byte Code Engineering Library (BCEL ) for some inspiration as well as Findbugs (it has to read/understand class files).

EDIT:

Here is another similar question with some great answers: Learning to write a compiler

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • No need to apologize, I just figured the question to be a generalized 'how to make a compiler in java' with no mentions to compiling bytecode that comforms to the abysmal JVM standards and specs :) I don't think he needs bytecode format for it, but it's a fine read as he might see the benefit of doing it that way (to say, to compile code that will be interpreted instead of assembled for a specific target) – Shark Aug 16 '12 at 16:03
  • 1
    Thanks, this is exactly what I was looking for. Someone who could give a straight answer, not critique formatting, or suggest to use C, even when I said don't bother. Thanks again – The_Steve13 Aug 16 '12 at 19:04
  • @The_Steve13 not a problem atleast i could be of help. I too want to make my own language and compiler. I wish you luck :) – David Kroukamp Aug 16 '12 at 19:27
2

Here some resources to read that i found

here you can find the source code for javac (the java compiler) which really should be a huge wealth of knowledge.

Dragon book, reading about compilers

owen gerig
  • 6,165
  • 6
  • 52
  • 91
2

This is a good book to get you started, and it's really good for beginners. It start with easy-to-understand theory and shows you how to write tiny compilers and how to improve it step-by-step.

Compiler Construction Using Java, JavaCC, and Yacc

And here's another one, it includes long code listing (you may or may not like it), it's easy to follow.

Writing Compilers and Interpreters: A Software Engineering Approach

There are also other Java compiler books and tons of non-Java compiler books (I personally own a number of them). But if you really want to stick with Java, I recommend you the above two.

su-
  • 3,116
  • 3
  • 32
  • 42
-2

http://code.google.com/p/javamicko/

It's unfinished but you'll be able to base stuff off of it I hope. It's pretty much what you want, a C compiler in Java.

EDIT: You will need the following - a Scanner - using jFlex for that

a Parser - using Bison for that

the language grammar - you will use C synthax here, I think it's uploaded in the project

the "intermediate code" will probably be some ASM dump in a .txt file. Later on you can generate machine code for your target machine using that ASM txt file (essentially disconnecting 'compilation' from 'assembling'); breaking those two apart will also leave you room to run optimizers on the intermediate code, to further optimize it before you translate it into hex bytes (opcodes).

Though looking at the code now... I see much less has been comitted than what I originally though. It's a port of the micro C compiler, from C and not a full one. This was one of my school projects so take it for what it's worth...

Shark
  • 6,513
  • 3
  • 28
  • 50