Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Creating a Compiler in Java

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Creating a Compiler in Java - Updated

    Hey all,

    well im thinking of creating a Compiler for my final year project and i just wanted to know if you have created a compiler before can tell me about your experiences and maybe recommened some reading material please.

    All help is apperciated thanks for reading

    Update - Apparently i need to have an academic question. im really stuck and i cant think of an academic question or a reason to create a compiler has anyone got any ideas? all help is apperciated could you please let me know thanks
    Last edited by Superstar288; September 16th, 2010 at 03:25 PM.


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Creating a Compiler in Java

    What do you want to compile?

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Compiler in Java

    sorry i wanted it to compile Java aswell i should have wrote that in my 1st post lol.

  4. #4
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Creating a Compiler in Java

    So you want to write a compiler in Java that compiles Java? Why not a game, IRC client, or something along those lines.

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Compiler in Java

    Well i recently have done an IRC client and i havent done any game dev modules. it does sound a bit silly when i say i want to make a Compiler in java which is also written in java lol but i would like to know how the back end works etc and i think it should be an intresting topic.

  6. #6
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Creating a Compiler in Java

    Well good luck. I have no clue where to start.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating a Compiler in Java

    You could start at the source (pun intended): The Compiler Group

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Creating a Compiler in Java

    There's always the good old "Dragon Book".

    It doesn't focus specifically on Java compilers, but it should be a great reference for creating compilers regardless of the language.

    You will also probably want to familiarize yourself with Java ByteCode. This is what most Java compilers compile Java programs down to, then the JVM either interprets or performs a JIT to run the Java program (modern JVM's do the latter because it has significantly better performance).

    Good luck, writing a compiler's pretty tough (especially if you want it to be any good).

  9. #9
    Junior Member
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Compiler in Java

    Hi all thanks for the replies i will look into the dragon book ive heard its lot of theory though lol well i think if i do choose to write a compiler it will only be a simple one and i have around 7 months to complete it.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating a Compiler in Java

    An idea: Create your own simple language to parse vs using a more complicated one.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Superstar288 (September 7th, 2010)

  12. #11
    Junior Member
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Compiler in Java

    Thanks for that Norm quite a nice idea i have no idea where i would start at trying to make my own language lol

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Creating a Compiler in Java

    Then take a subset of an existing one.

  14. #13
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Creating a Compiler in Java

    Quote Originally Posted by helloworld922 View Post
    There's always the good old "Dragon Book".

    It doesn't focus specifically on Java compilers, but it should be a great reference for creating compilers regardless of the language.

    You will also probably want to familiarize yourself with Java ByteCode. This is what most Java compilers compile Java programs down to, then the JVM either interprets or performs a JIT to run the Java program (modern JVM's do the latter because it has significantly better performance).

    Good luck, writing a compiler's pretty tough (especially if you want it to be any good).
    Im doing a compiler class right now at uni, and the dragon book is core material. Pretty tough reading, but you really cant do a compiler without it (or a similar book).

    I can safely say, that writing a compiler for the full Java 1.5 spec is not possible to do solo, unless you are already very experienced in compiler production, or you have a year++ to spend on it. As Norm said, do a subset of the java impl or simply your own simple language.

    Take a look at compiler framework for java, sableCC. Once you get around the quirky syntax, it will save you a lot of time making a parser to abstract syntax trees.

  15. #14
    Junior Member
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Compiler in Java

    thanks for the comments everyone i will take your advise and use a java subset or just make my own simple language. i will let you know what i decide next wednesday after i have spoken to my supervisor.

  16. #15
    Junior Member
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Compiler in Java

    Update - Apparently i need to have an academic question. im really stuck and i cant think of an academic question or a reason to create a compiler has anyone got any ideas? all help is apperciated could you please let me know thanks

  17. #16
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Creating a Compiler in Java

    There are a lot of "specialized languages" that take either build on to an existing language, or define a subset.

    Examples would be OpenCL (variant of C99 with a strong vector/parallel processing core), Arduino C (variant of C, used to program the Arduino micro controller), MatLab (kind of like some scripting languages, designed for mathematical computation), etc.

    edit:

    I'm actually working on my own interpreter/compiler that can be used for math/scientific computations. It's based off Java, and I'm designing it so that components can be designed/used from Java, or using the interpreter I'm writing.
    Last edited by helloworld922; September 16th, 2010 at 08:13 PM.

  18. #17
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Creating a Compiler in Java

    How would I add an eternal scripting language to my game engine? Ive searched every where in google, and i cant find anything good.

  19. #18
    Junior Member
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Compiler in Java

    Hey yoda thanks yea i know ive looked and thought as much as i can however im just stuck lol.

  20. #19
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Creating a Compiler in Java

    At the risk of stating the obvious, not so obvious, or obscure...a compiler is a tool - solving a question is more suited for an algorithm. Both are intertwined but quite different. Perhaps come at it from a different angle - think about a question to address, and not how to address a question..if that makes any sense
    Last edited by copeg; September 16th, 2010 at 10:06 PM.

  21. #20
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Compiler in Java

    Hi, I'm attempting to do a similar project and I have few tips if you're still interested:

    As others have said, it is essentially pointless to create a compiler for Java (or a subset of Java). You need to do something new or different. That might be: extending an existing language to add new/missing features, or creating a completely new language (possibly to solve a certain problem).

    Also It is probably best to make your compiler generate byte code for the JVM. The alternative options are to generate real machine code, or to create an interpreter to execute your language. I'd advise against the last two options as they would likely be too much work for a student project.

    Lastly, you shouldn't need to write the lexical analyser or parser yourself. There are many different Java 'compiler-compilers' out there, The main ones are: SableCC (already mentioned), JavaCC, ANTLR, CUP, and Beaver. Which one you use will depend on the grammar of the language you are parsing, and maybe the design of your compiler.

    I'd definitely recommend the book: "Modern Compiler Implementation in Java" by Andrew Appel, which tells you exactly how to go out writing a compiler in Java.

  22. #21
    Junior Member
    Join Date
    Feb 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a Compiler in Java

    Could u upload your project (compiler) please
    my project is develop compiler in last project
    to connect to me
    https://www.facebook.com/engolfat.ameen

Similar Threads

  1. Creating a Triagular Wave in Java
    By locorecto in forum AWT / Java Swing
    Replies: 3
    Last Post: March 8th, 2010, 01:51 AM
  2. Creating program Blackjack - Dr.Java
    By TheUntameable in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 20th, 2010, 12:54 PM
  3. Why the compiler can not find the symbol?
    By AlicNewbie in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2010, 08:16 PM
  4. setting the compiler for eclipse mac os x
    By etidd in forum Java IDEs
    Replies: 2
    Last Post: January 29th, 2010, 10:18 AM
  5. sql compiler
    By tsuki in forum JDBC & Databases
    Replies: 6
    Last Post: October 16th, 2009, 10:35 PM