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.

10
  • 7
    Nothing prevents you from writing it in Java indeed - do you have a question? Commented Aug 16, 2012 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? Commented Aug 16, 2012 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. Commented Aug 16, 2012 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. Commented Aug 16, 2012 at 15:32
  • @Durandal you have like 40 different thoughts in that question. Commented Aug 16, 2012 at 15:34

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

3 Comments

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)
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 not a problem atleast i could be of help. I too want to make my own language and compiler. I wish you luck :)
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

1 Comment

Dragon book reference is good, javac not so much...
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.

Comments

-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...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.