Hi all ive been thinking of a writing a compiler in java for my final year project. I have started to browse the net for help but to be honest its confused rather than helping me to get a clear image of what I would like to do.

E.g. I have read that a compiler is something which transforms source code (Java) into another computer language. To my knowledge that is a cross compiler? I always thought a compiler is what translates a source code (Java) into machine code. So many website say different things. Also I haven’t really had a module on compiler design so im a blank slate at the moment.

So I was just wondering anyone has written there own compiler in Java what the necessary steps and maby where should I start. I would be very grateful if you could recommend some good reading material

Thanks in advance.

Dani AI

Generated

Good choice for a final-year project — compilers are a great mix of theory and engineering. As clarified, decide the target early: a compiler that produces JVM classfiles, a cross-compiler to native code, or a translator that emits another high-level language (a transpiler). That decision sets the whole architecture and the tooling effort.

A pragmatic development path that fits a single-person project:

  • Define a very small language spec (expressions, variables, assignments, if/while, simple functions).
  • Implement an interpreter first to lock down semantics; this makes later bugs far easier to diagnose.
  • Add a lexer and parser (hand-written recursive-descent or a generator like ANTLR/JavaCC).
  • Build an explicit AST, then a semantic phase (symbol tables, scope rules, basic type checks).

Architecture and testing notes: separate phases clearly (lexing → parsing → AST → semantic checks → codegen). For a JVM target, either emit Java source and invoke the Java compiler API (simpler) or generate bytecode directly (more powerful; libraries such as ASM make this practical). Create small, focused unit tests for each phase, pretty-print the AST for visual checks, and keep a corpus of tiny programs that exercise features and error cases. Common pitfalls include left-recursive grammars for recursive-descent parsers, operator-precedence handling, and designing useful error messages and recovery.

Project-scoped advice: avoid implementing full Java — pick a subset and demonstrate it well. Break the project into milestones: language spec + interpreter; working parser + AST; semantic analyzer; working backend producing runnable artifacts; test harness and documentation. As and noted, textbooks and online tutorials help; combine one practical tutorial with one classical text for theory. A clear demo showing compile → run → correct output will communicate success more than an overambitious but fragile feature list.

Recommended Answers

All 4 Replies

A compiler transforms code into something that can be executed by something else.
Thus a Java compiler will take Java sourcecode and transform it into classfiles for execution by a Java Virtual Machine.
A crosscompiler is a special case of a compiler that compiles sourcecode into executable code for another platform, allowing you to for example compile Mac executable code under Windows.

What you're describing as finding transforms Java sourcecode into sourcecode in some other language. That's neither a compiler nor a cross compiler, it's a transpiler.
Of course if a runtime environment takes source code as its input and executes it a transpiler could be a compiler as well. A transpiler that produces Python or Ruby sourcecode would be an example of this.

Many people have written compilers in Java that produce Java bytecode for a variety of programming languages.

Hey thanks for the replies and thanks for clearing that up jwenting. ive just started to read that book dleskov hopefully its good. if anyone else has made a compiler in java could you comment on your experience.

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.