<p><em>Preferred languages</em>: C/C++, Java, and Ruby.</p>

<p>I am looking for some helpful books/tutorials on how to write your own compiler simply for educational purposes. I am most familiar with C/C++, Java, and Ruby, so I prefer resources that involve one of those three, but any good resource is acceptable.</p>

[{'answer_id': 1672, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 11, 'selected': True, 'text': '<h2>Big List of Resources:</h2>\n<ul>\n<li><a href="http://www.cs.indiana.edu/%7Edyb/pubs/nano-jfp.pdf" rel="noreferrer">A Nanopass Framework for Compiler Education</a> ¶</li>\n<li><a href="http://www.amazon.com/gp/offer-listing/1558603204?tag=thecompilerconne&amp;camp=14573&amp;creative=327641&amp;linkCode=am1&amp;creativeASIN=1558603204&amp;adid=1ZQWN5FGBA1JM6MNQ2ZV&amp;" rel="noreferrer">Advanced Compiler Design and Implementation</a> $</li>\n<li><a href="http://scheme2006.cs.uchicago.edu/11-ghuloum.pdf" rel="noreferrer">An Incremental Approach to Compiler Construction</a> ¶</li>\n<li><a href="http://javadude.com/articles/antlr3xtut" rel="noreferrer">ANTLR 3.x Video Tutorial</a></li>\n<li><a href="http://www.diku.dk/%7Etorbenm/Basics/" rel="noreferrer">Basics of Compiler Design</a></li>\n<li><a href="http://web.archive.org/web/20160731061607/http://www.onlamp.com/pub/a/onlamp/2004/04/15/parrot_compiler_construction.html" rel="noreferrer">Building a Parrot Compiler</a></li>\n<li><a href="http://www.cs.man.ac.uk/%7Epjj/farrell/compmain.html" rel="noreferrer">Compiler Basics</a></li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/0201403536" rel="noreferrer" rel="nofollow noreferrer">Compiler Construction</a> $</li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/0442275366" rel="noreferrer" rel="nofollow noreferrer">Compiler Design and Construction</a> $</li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/0805321667" rel="noreferrer" rel="nofollow noreferrer">Crafting a Compiler with C</a> $</li>\n<li><a href="http://craftinginterpreters.com/" rel="noreferrer">Crafting Interpreters</a></li>\n<li>[Compiler Design in C] <a href="http://www.holub.com/software/compiler.design.in.c.html" rel="noreferrer">12</a> ¶</li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/0321486811" rel="noreferrer" rel="nofollow noreferrer">Compilers: Principles, Techniques, and Tools</a> $ — aka <a href="http://en.wikipedia.org/wiki/Compilers:_Principles%2C_Techniques%2C_and_Tools" rel="noreferrer">&quot;The Dragon Book&quot;</a>; widely considered &quot;the book&quot; for compiler writing.</li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/012088478X" rel="noreferrer" rel="nofollow noreferrer">Engineering a Compiler</a> $</li>\n<li><a href="http://www.cs.indiana.edu/eopl/" rel="noreferrer">Essentials of Programming Languages</a></li>\n<li><a href="http://flipcode.com/archives/articles.shtml" rel="noreferrer">Flipcode Article Archive</a> (look for &quot;Implementing A Scripting Engine by Jan Niestadt&quot;)</li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/1931841578" rel="noreferrer" rel="nofollow noreferrer">Game Scripting Mastery</a> $</li>\n<li><a href="http://www.codeproject.com/KB/recipes/B32Machine1/VMCS.pdf" rel="noreferrer">How to build a virtual machine from scratch in C#</a> ¶</li>\n<li><a href="http://research.microsoft.com/%7Esimonpj/papers/pj-lester-book/" rel="noreferrer">Implementing Functional Languages</a></li>\n<li><a href="http://www1.digitalgrammars.com/ipl-book/" rel="noreferrer">Implementing Programming Languages (with BNFC)</a></li>\n<li><a href="http://www.codeproject.com/KB/recipes/programminglanguagetoools.aspx" rel="noreferrer">Implementing Programming Languages using C# 4.0</a></li>\n<li><a href="http://en.wikipedia.org/wiki/Interpreter_pattern" rel="noreferrer">Interpreter pattern</a> (described in <a href="https://rads.stackoverflow.com/amzn/click/com/0201633612" rel="noreferrer" rel="nofollow noreferrer">Design Patterns</a> $) specifies a way to evaluate sentences in a language</li>\n<li><a href="http://pragprog.com/titles/tpdsl/language-implementation-patterns" rel="noreferrer">Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages</a> $</li>\n<li><a href="http://compilers.iecc.com/crenshaw/" rel="noreferrer">Let\'s Build a Compiler</a> by Jack Crenshaw — The <a href="http://www.stack.nl/%7Emarcov/compiler.pdf" rel="noreferrer">PDF</a> ¶ version (examples are in Pascal, but the information is generally applicable)</li>\n<li><a href="http://books.google.com/books?id=Id9cYsIdjIwC&amp;lpg=PP1&amp;ots=IxFkFWJ-8V&amp;dq=%22linkers%20and%20loaders%22&amp;pg=PA215#v=onepage&amp;q=%22linkers%20and%20loaders%22&amp;f=false" rel="noreferrer">Linkers and Loaders</a> $ (Google Books)</li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/0521562473" rel="noreferrer" rel="nofollow noreferrer">Lisp in Small Pieces (LiSP)</a> $</li>\n<li><a href="http://llvm.org/docs/tutorial/" rel="noreferrer">LLVM Tutorial</a></li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/0521607647" rel="noreferrer" rel="nofollow noreferrer">Modern Compiler Implementation in ML</a> $ — There is a <a href="https://rads.stackoverflow.com/amzn/click/com/052182060X" rel="noreferrer" rel="nofollow noreferrer">Java</a> $ and <a href="https://rads.stackoverflow.com/amzn/click/com/0521607655" rel="noreferrer" rel="nofollow noreferrer">C</a> $ version as well - widely considered a very good book</li>\n<li><a href="http://www.amazon.com/gp/offer-listing/013630740X?tag=thecompilerconne&amp;camp=14573&amp;creative=327641&amp;linkCode=am1&amp;creativeASIN=013630740X&amp;adid=0JPMVBRNCAN6PDKGYSXX&amp;" rel="noreferrer">Object-Oriented Compiler Construction</a> $</li>\n<li><a href="http://www.dickgrune.com/Books/PTAPG_1st_Edition/" rel="noreferrer">Parsing Techniques - A Practical Guide</a></li>\n<li><a href="http://www-old.oberon.ethz.ch/WirthPubl/ProjectOberon.pdf" rel="noreferrer">Project Oberon</a> ¶ - Look at chapter 13</li>\n<li><a href="https://rads.stackoverflow.com/amzn/click/com/0137302673" rel="noreferrer" rel="nofollow noreferrer">Programming a Personal Computer</a> $</li>\n<li><a href="http://www.cs.brown.edu/%7Esk/Publications/Books/ProgLangs/" rel="noreferrer">Programing Languages: Application and Interpretation</a></li>\n<li><a href="http://repository.readscheme.org/ftp/papers/ai-lab-pubs/AITR-474.pdf" rel="noreferrer">Rabbit: A Compiler for Scheme</a>¶</li>\n<li><a href="http://web.archive.org/web/20141221110345/http://cm.bell-labs.com/who/ken/trust.html" rel="noreferrer">Reflections on Trusting Trust</a> — A quick guide</li>\n<li><a href="http://msdn.microsoft.com/en-us/magazine/cc136756.aspx" rel="noreferrer">Roll Your Own Compiler for the .NET framework</a> — A quick tutorial from MSDN</li>\n<li><a href="http://mitpress.mit.edu/sicp/" rel="noreferrer">Structure and Interpretation of Computer Programs</a></li>\n<li><a href="http://www.cis.upenn.edu/%7Ebcpierce/tapl/" rel="noreferrer">Types and Programming Languages</a></li>\n<li><a href="http://prog21.dadgum.com/30.html" rel="noreferrer">Want to Write a Compiler?</a> - a quick guide</li>\n<li><a href="http://www.hokstad.com/writing-a-compiler-in-ruby-bottom-up-step-1.html" rel="noreferrer">Writing a Compiler in Ruby Bottom Up</a></li>\n<li><a href="https://bernsteinbear.com/blog/compiling-a-lisp-0/" rel="noreferrer">Compiling a Lisp</a> — compile directly to x86-64</li>\n</ul>\n<p>Legend:</p>\n<ul>\n<li>¶ Link to a PDF file</li>\n<li>$ Link to a printed book</li>\n</ul>\n'}, {'answer_id': 1678, 'author': 'saniul', 'author_id': 52, 'author_profile': 'https://Stackoverflow.com/users/52', 'pm_score': 5, 'selected': False, 'text': '<p><a href="http://compilers.iecc.com/crenshaw/" rel="noreferrer">"Let\'s Build a Compiler"</a> is awesome, but it\'s a bit outdated. (I\'m not saying it makes it even a little bit less valid.)</p>\n\n<p>Or check out <a href="https://www.quora.com/As-a-self-taught-programmer-how-can-I-learn-about-compilers/answer/Akhil-Kooliyatt?srid=TAGM" rel="noreferrer">SLANG</a>. This is similar to "Let\'s Build a Compiler" but is a much better resource especially for beginners. This comes with a pdf tutorial which takes a 7 step approach at teaching you a compiler. Adding the quora link as it have the links to all the various ports of SLANG, in C++, Java and JS, also  interpreters in python and java, originally written using C# and the .NET platform.</p>\n'}, {'answer_id': 1686, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 6, 'selected': False, 'text': '<p>I concur with the Dragon Book reference; IMO, it is the definitive guide to compiler construction. Get ready for some hardcore theory, though.</p>\n\n<p>If you want a book that is lighter on theory, <a href="https://rads.stackoverflow.com/amzn/click/com/1931841578" rel="noreferrer" rel="nofollow noreferrer">Game Scripting Mastery</a> might be a better book for you. If you are a total newbie at compiler theory, it provides a gentler introduction. It doesn\'t cover more practical parsing methods (opting for non-predictive recursive descent without discussing LL or LR parsing), and as I recall, it doesn\'t even discuss any sort of optimization theory. Plus, instead of compiling to machine code, it compiles to a bytecode that is supposed to run on a VM that you also write.</p>\n\n<p>It\'s still a decent read, particularly if you can pick it up for cheap on Amazon. If you only want an easy introduction into compilers, Game Scripting Mastery is not a bad way to go. If you want to go hardcore up front, then you should settle for nothing less than the Dragon Book.</p>\n'}, {'answer_id': 1693, 'author': 'Peter Burns', 'author_id': 101, 'author_profile': 'https://Stackoverflow.com/users/101', 'pm_score': 5, 'selected': False, 'text': '<p>If you\'re looking to use powerful, higher level tools rather than building <em>everything</em> yourself, going through the projects and readings for <a href="http://www.antlr.org/wiki/display/CS652/CS652+Home" rel="noreferrer">this course</a> is a pretty good option.  It\'s a languages course by the author of the Java parser engine ANTLR.  You can get the book for the course as a PDF from <a href="http://www.pragprog.com/titles/tpantlr/the-definitive-antlr-reference" rel="noreferrer">the Pragmatic Programmers</a>.</p>\n\n<p>The course goes over the standard compiler compiler stuff that you\'d see elsewhere: parsing, types and type checking, polymorphism, symbol tables, and code generation.  Pretty much the only thing that isn\'t covered is optimizations.  The final project is  a program that <a href="http://www.antlr.org/wiki/display/CS652/C+subset+compiler" rel="noreferrer">compiles a subset of C</a>.  Because you use tools like ANTLR and LLVM, it\'s feasible to write the entire compiler in a single day (I have an existence proof of this, though I do mean ~24 hours).  It\'s heavy on practical engineering using modern tools, a bit lighter on theory.</p>\n\n<p>LLVM, by the way, is simply fantastic.  Many situations where you might normally compile down to assembly, you\'d be much better off compiling to <a href="http://llvm.org/docs/LangRef.html" rel="noreferrer">LLVM\'s Intermediate Representation</a> instead.  It\'s higher level, cross platform, and LLVM is quite good at generating optimized assembly from it.</p>\n'}, {'answer_id': 2523, 'author': 'Chris Bunch', 'author_id': 422, 'author_profile': 'https://Stackoverflow.com/users/422', 'pm_score': 4, 'selected': False, 'text': '<p><a href="https://rads.stackoverflow.com/amzn/click/com/0321486811" rel="noreferrer" rel="nofollow noreferrer">The Dragon Book</a> is definitely the "building compilers" book, but if your language isn\'t quite as complicated as the current generation of languages, you may want to look at the Interpreter pattern from <a href="https://rads.stackoverflow.com/amzn/click/com/0201633612" rel="noreferrer" rel="nofollow noreferrer">Design Patterns</a>.</p>\n\n<p>The example in the book designs a regular expression-like language and is well thought through, but as they say in the book, it\'s good for thinking through the process but is effective really only on small languages. However, it is much faster to write an Interpreter for a small language with this pattern than having to learn about all the different types of parsers, yacc and lex, et cetera...</p>\n'}, {'answer_id': 7085, 'author': 'Dominic Cooney', 'author_id': 878, 'author_profile': 'https://Stackoverflow.com/users/878', 'pm_score': 6, 'selected': False, 'text': '<p>I think <a href="https://rads.stackoverflow.com/amzn/click/com/0521607647" rel="noreferrer" rel="nofollow noreferrer" title="Modern Compiler Implementation in ML">Modern Compiler Implementation in ML</a> is the best introductory compiler writing text. There\'s a <a href="https://rads.stackoverflow.com/amzn/click/com/052182060X" rel="noreferrer" rel="nofollow noreferrer" title="Modern Compiler Implementation in Java">Java version</a> and a <a href="https://rads.stackoverflow.com/amzn/click/com/0521607655" rel="noreferrer" rel="nofollow noreferrer" title="Modern Compiler Implementation in C">C version</a> too, either of which might be more accessible given your languages background. The book packs a lot of useful basic material (scanning and parsing, semantic analysis, activation records, instruction selection, RISC and x86 native code generation) and various "advanced" topics (compiling OO and functional languages, polymorphism, garbage collection, optimization and single static assignment form) into relatively little space (~500 pages).</p>\n\n<p>I prefer Modern Compiler Implementation to the Dragon book because Modern Compiler implementation surveys less of the field--instead it has really solid coverage of all the topics you would need to write a serious, decent compiler. After you work through this book you\'ll be ready to tackle research papers directly for more depth if you need it.</p>\n\n<p>I must confess I have a serious soft spot for Niklaus Wirth\'s <a href="https://rads.stackoverflow.com/amzn/click/com/0201403536" rel="noreferrer" rel="nofollow noreferrer" title="Compiler Construction">Compiler Construction.</a> It is <a href="http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf" rel="noreferrer" title="Compiler Construction (PDF)">available online</a> as a PDF. I find Wirth\'s programming aesthetic simply beautiful, however some people find his style too minimal (for example Wirth favors recursive descent parsers, but most CS courses focus on parser generator tools; Wirth\'s language designs are fairly conservative.) Compiler Construction is a very succinct distillation of Wirth\'s basic ideas, so whether you like his style or not or not, I highly recommend reading this book.</p>\n'}, {'answer_id': 8690, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Python comes bundled with a python compiler written in Python. You can see the source code, and it includes all phases, from parsing, abstract syntax tree, emitting code, etc.\nHack it.</p>\n'}, {'answer_id': 15178, 'author': 'Ben Combee', 'author_id': 1323, 'author_profile': 'https://Stackoverflow.com/users/1323', 'pm_score': 4, 'selected': False, 'text': '<p>One book not yet suggested but very important is <a href="http://books.google.com/books?id=h34d_jr2iikC&amp;dq=%22linkers+and+loaders%22&amp;pg=PP1&amp;ots=IxFkFWJ-8V&amp;sig=GSlclmkezTRL6YYguGJmZsnkM3c&amp;hl=en&amp;sa=X&amp;oi=book_result&amp;resnum=1&amp;ct=result" rel="noreferrer">"Linkers and Loaders"</a> by John Levine.  If you\'re not using an external assembler, you\'ll need a way to output a object file that can be linked into your final program.  Even if you\'re using an external assembler, you\'ll probably need to understand relocations and how the whole program loading process works to make a working tool.  This book collects a lot of the random lore around this process for various systems, including Win32 and Linux.</p>\n'}, {'answer_id': 17690, 'author': 'Peter Stuifzand', 'author_id': 1633, 'author_profile': 'https://Stackoverflow.com/users/1633', 'pm_score': 3, 'selected': False, 'text': '<p>An easy way to create a compiler is to use bison and flex (or similar), build a tree (AST) and generate code in C. With generating C code being the most important step. By generating C code, your language will automatically work on all platforms that have a C compiler.</p>\n\n<p>Generating C code is as easy as generating HTML (just use print, or equivalent), which in turn is much easier than writing a C parser or HTML parser.</p>\n'}, {'answer_id': 17694, 'author': 'wvdschel', 'author_id': 2018, 'author_profile': 'https://Stackoverflow.com/users/2018', 'pm_score': 4, 'selected': False, 'text': '<p>If you\'re willing to use LLVM, check this out: <a href="http://llvm.org/docs/tutorial/" rel="noreferrer">http://llvm.org/docs/tutorial/</a>. It teaches you how to write a compiler from scratch using LLVM\'s framework, and doesn\'t assume you have any knowledge about the subject.</p>\n\n<p>The tutorial suggest you write your own parser and lexer etc, but I advise you to look into bison and flex once you get the idea. They make life so much easier.</p>\n'}, {'answer_id': 17749, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>As an starting point, it will be good to create a recursive descent parser (RDP) (let\'s say you want to create your own flavour of BASIC and build a BASIC interpreter) to understand how to write a compiler.\nI found the best information in Herbert Schild\'s C Power Users, chapter 7. This chapter refers to another book of H. Schildt "C The complete Reference" where he explains how to create a calculator (a simple expression parser). I found both books on eBay very cheap.\nYou can check the code for the book if you go to www.osborne.com or check in <a href="http://www.HerbSchildt.com" rel="nofollow noreferrer">www.HerbSchildt.com</a>\nI found the same code but for C# in his latest book</p>\n'}, {'answer_id': 17768, 'author': 'bootload', 'author_id': 2092, 'author_profile': 'https://Stackoverflow.com/users/2092', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>"... Let\'s Build a Compiler ..."</p>\n</blockquote>\n\n<p>I\'d second <a href="http://compilers.iecc.com/crenshaw/" rel="nofollow noreferrer">http://compilers.iecc.com/crenshaw/</a> by <a href="https://stackoverflow.com/questions/1669/learning-to-write-a-compiler#1678">@sasb</a>. Forget buying more books for the moment.</p>\n\n<p>Why?  Tools &amp; language. </p>\n\n<p>The language required is Pascal and if I remember correctly is based on Turbo-Pascal. It just so happens if you go to <a href="http://www.freepascal.org/" rel="nofollow noreferrer">http://www.freepascal.org/</a> and download the Pascal compiler all the examples work straight from the page ~ <a href="http://www.freepascal.org/download.var" rel="nofollow noreferrer">http://www.freepascal.org/download.var</a> The beaut thing about Free Pascal is you can use it almost whatever processor or OS you can care for. </p>\n\n<p>Once you have mastered the lessons then try the more advanced <em>"<a href="http://en.wikipedia.org/wiki/Dragon_book" rel="nofollow noreferrer">Dragon Book</a>"</em> ~ <a href="http://en.wikipedia.org/wiki/Dragon_book" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Dragon_book</a></p>\n'}, {'answer_id': 22718, 'author': 'dmckee --- ex-moderator kitten', 'author_id': 2509, 'author_profile': 'https://Stackoverflow.com/users/2509', 'pm_score': 3, 'selected': False, 'text': '<p>I liked the <A HREF="http://compilers.iecc.com/crenshaw/" rel="nofollow noreferrer">Crenshaw tutorial</A> too, because it makes it absolutely clear that a compiler is just another program that reads some input and writes some out put. </p>\n\n<p>Read it. </p>\n\n<p>Work it if you want, but then look at another reference on how bigger and more complete compilers are really written.</p>\n\n<p>And read <A href="http://cm.bell-labs.com/who/ken/trust.html" rel="nofollow noreferrer">On Trusting Trust</A>, to get a clue about the unobvious things that can be done in this domain.</p>\n'}, {'answer_id': 74176, 'author': 'mfx', 'author_id': 8015, 'author_profile': 'https://Stackoverflow.com/users/8015', 'pm_score': 4, 'selected': False, 'text': '<p>The LCC compiler (<a href="http://en.wikipedia.org/wiki/Local_C_compiler" rel="nofollow noreferrer">wikipedia</a>) (<a href="http://www.cs.princeton.edu/software/lcc/" rel="nofollow noreferrer">project homepage</a>) (<a href="https://github.com/drh/lcc" rel="nofollow noreferrer">github.com/drh/lcc</a>) of Fraser and Hanson is described in their book "A Retargetable C Compiler: Design and Implementation". It is quite readable and explains the whole compiler, down to code generation. </p>\n'}, {'answer_id': 98010, 'author': 'tovare', 'author_id': 12677, 'author_profile': 'https://Stackoverflow.com/users/12677', 'pm_score': 3, 'selected': False, 'text': '<p>There\'s a lot of good answers here, so i thought I\'d just add one more to the list:</p>\n\n<p>I got a book called Project Oberon more than a decade ago, which has some very well written text on the compiler. The book really stands out in the sense that the source and explanations is very hands on and readable. The complete text (the 2005 edition) has been made available in pdf, so you can download right now. The compiler is discussed in chapter 12:</p>\n\n<p><a href="http://www.ethoberon.ethz.ch/WirthPubl/ProjectOberon.pdf" rel="nofollow noreferrer">http://www.ethoberon.ethz.ch/WirthPubl/ProjectOberon.pdf</a></p>\n\n<p>Niklaus Wirth, Jürg Gutknecht</p>\n\n<p>(The treatment is not as extensive as his book on compilers)</p>\n\n<p>I\'ve read several books on compilers, and i can second the dragon book, time spent on this book is very worthwhile.</p>\n'}, {'answer_id': 156867, 'author': 'Mark Reid', 'author_id': 24057, 'author_profile': 'https://Stackoverflow.com/users/24057', 'pm_score': 3, 'selected': False, 'text': '<p>If you are interested in writing a compiler for a functional language (rather than a procedural one) Simon Peyton-Jones and David Lester\'s "<a href="http://research.microsoft.com/~simonpj/papers/pj-lester-book/" rel="noreferrer">Implementing functional languages: a tutorial</a>" is an excellent guide. </p>\n\n<p>The conceptual basics of how functional evaluation works is guided by examples in a simple but powerful functional language called "Core". Additionally, each part of the Core language compiler is explained with code examples in Miranda (a pure functional language very similar to Haskell). </p>\n\n<p>Several different types of compilers are described but even if you only follow the so-called template compiler for Core you will have an excellent understanding of what makes functional programming tick. </p>\n'}, {'answer_id': 159530, 'author': 'user9529', 'author_id': 9529, 'author_profile': 'https://Stackoverflow.com/users/9529', 'pm_score': 2, 'selected': False, 'text': '<p>The Dragon Book is too complicated. So ignore it as a starting point. It is good and makes you think a lot once you already have a starting point, but for starters, perhaps you should simply try to write an math/logical expression evaluator using RD, LL or LR parsing techniques with everything (lexing/parsing) written by hand in perhaps C/Java. This is interesting in itself and gives you an idea of the problems involved in a compiler. Then you can jump in to your own DSL using some scripting language (since processing text is usually easier in these) and like someone said, generate code in either the scripting language itself or C. You should probably use flex/bison/antlr etc to do the lexing/parsing if you are going to do it in c/java.</p>\n'}, {'answer_id': 172181, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>You should check out Darius Bacon\'s "<a href="https://github.com/darius/ichbins" rel="nofollow noreferrer" title="I can hardly believe it&#39;s not Scheme">ichbins</a>", which is a compiler for a small Lisp dialect, targeting C, in just over 6 pages of code.  The advantage it has over most toy compilers is that the language is complete enough that the compiler is written in it.  (The tarball also includes an interpreter to bootstrap the thing.)</p>\n\n<p>There\'s more stuff about what I found useful in learning to write a compiler on my <a href="http://www.canonical.org/~kragen/sw/urscheme/" rel="nofollow noreferrer" title="a small self-hosted Scheme to x86 asm compiler">Ur-Scheme</a> web page.</p>\n'}, {'answer_id': 401786, 'author': 'dbones', 'author_id': 47642, 'author_profile': 'https://Stackoverflow.com/users/47642', 'pm_score': 3, 'selected': False, 'text': '<p>I am looking into the same concept, and found this promising article by Joel Pobar,</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/magazine/cc136756.aspx" rel="nofollow noreferrer">Create a Language Compiler for the .NET Framework - not sure where this has gone</a></p>\n\n<p><a href="http://msdn.microsoft.com/en-us/magazine/cc136756.aspx" rel="nofollow noreferrer">Create a Language Compiler for the .NET Framework - pdf copy of the original doc</a></p>\n\n<p>he discusses a high level concept of a compiler and proceeds to invent his own langauge for the .Net framework. Although its aimed at the .Net Framework, many of the concepts should be able to be reproduced. The Article covers:</p>\n\n<ol>\n<li>Langauge definition </li>\n<li>Scanner </li>\n<li>Parser (the bit im mainly interested in)</li>\n<li>Targeting the .Net Framework The</li>\n<li>Code Generator</li>\n</ol>\n\n<p>there are other topics, but you get the just.</p>\n\n<p>Its aimed to people starting out, written in C# (not quite Java)</p>\n\n<p>HTH</p>\n\n<p>bones</p>\n'}, {'answer_id': 514362, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>If you are like me, who has no formal computer science education, and is interested in\nbuilding/want to know how a compiler works:</p>\n\n<p>I am recommend "Programming Language Processors in Java: Compilers and Interpreters",\nan amazing book for a self-taught computer programmer.</p>\n\n<p>From my point of view, understanding those basic language theory, automate machine, and set theory is not a big problem. The problem is how to turn those things into code. The above book tells you how to write a parser, analysis context, and generate code. If you can not understand this book, then I have to say, give up building a compiler. The book is best programming book I have ever read.</p>\n\n<p>There is an other book, also good, <em>Compiler Design in C</em>. There is a lot of code, and it tells you everything about how to build a compiler and lexer tools.</p>\n\n<p>Building a compiler is a fun programming practice and can teach you heaps of programming skills.</p>\n\n<p>Do not buy the <a href="http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools#Second_edition" rel="nofollow noreferrer">Dragon book</a>. It was a waste of money and time and is not for a practitioner.</p>\n'}, {'answer_id': 789357, 'author': 'eKek0', 'author_id': 32173, 'author_profile': 'https://Stackoverflow.com/users/32173', 'pm_score': 3, 'selected': False, 'text': '<p>Sorry, it is in Spanish, but this is the bibliography of a course called "Compiladores e Intérpretes" (Compilers and Interpreters) in Argentina. </p>\n\n<p>The course was from formal language theory to compiler construction, and these are the topics you need to build, at least, a simple compiler:</p>\n\n<blockquote>\n  <ul>\n  <li><p>Compilers Design in C. <br />    Allen I. Holub <br /><br>\n  Prentice-Hall. 1990.</p></li>\n  <li><p>Compiladores. Teoría y Construcción. <br />    Sanchís\n  Llorca, F.J. ,  Galán Pascual, C.     Editorial Paraninfo. 1988.</p></li>\n  <li><p>Compiler Construction. <br />    Niklaus Wirth <br /><br>\n  Addison-Wesley. 1996.</p></li>\n  <li><p>Lenguajes, Gramáticas y Autómatas. Un enfoque práctico. <br />    Pedro\n  Isasi Viñuela, Paloma Martínez\n  Fernández, Daniel Borrajo Millán.     Addison-Wesley Iberoamericana\n  (España). 1997.</p></li>\n  <li><p>The art of compiler design. Theory and practice. <br />    Thomas\n  Pittman, James Peters. <br /><br>\n  Prentice-Hall. 1992.</p></li>\n  <li><p>Object-Oriented Compiler Construction. <br />    Jim Holmes.\n  <br />    Prentice Hall, Englewood\n  Cliffs, N.J. 1995</p></li>\n  <li><p>Compiladores. Conceptos Fundamentales. <br />    B. Teufel, S.\n  Schmidt, T. Teufel. <br /><br>\n  Addison-Wesley Iberoamericana. 1995.</p></li>\n  <li><p>Introduction to Automata Theory, Languages, and Computation. <br /><br>\n  John E. Hopcroft. Jeffref D. Ullman.\n  <br />    Addison-Wesley. 1979.</p></li>\n  <li><p>Introduction to formal languages. <br />    György E. Révész. <br /><br>\n  Mc Graw Hill. 1983.</p></li>\n  <li><p>Parsing Techniques. A Practical Guide. <br />    Dick Grune, Ceriel\n  Jacobs. <br />    Impreso por los\n  autores. 1995<br>\n  <a href="http://www.cs.vu.nl/~dick/PTAPG.html" rel="nofollow noreferrer">http://www.cs.vu.nl/~dick/PTAPG.html</a></p></li>\n  <li><p>Yacc: Yet Another Compiler-Compiler. <br />    Stephen\n  C. Johnson <br />    Computing Science\n  Technical Report Nº 32, 1975. Bell\n  Laboratories. Murray Hill, New<br>\n  Jersey.</p></li>\n  <li><p>Lex: A Lexical Analyzer Generator. <br />    M. E. Lesk,  E. Schmidt.     Computing Science Technical\n  Report Nº 39, 1975. Bell Laboratories.\n  Murray Hill, New     Jersey.</p></li>\n  <li><p>lex &amp; yacc. <br />    John R. Levine, Tony Mason, Doug Brown. <br />\n  O’Reilly &amp; Associates. 1995.</p></li>\n  <li><p>Elements of the theory of computation. <br />    Harry R. Lewis,\n  Christos H. Papadimitriou.\n  Segunda Edición. Prentice Hall. 1998. </p></li>\n  <li><p>Un Algoritmo Eficiente para la Construcción del Grafo de Dependencia de Control.<br/>\n  Salvador V. Cavadini.<br>\n  Trabajo Final de Grado para obtener el Título de Ingeniero en Computación.<br>\n  Facultad de Matemática Aplicada.\n  U.C.S.E. 2001.</p></li>\n  </ul>\n</blockquote>\n'}, {'answer_id': 1156383, 'author': 'Sam Harwell', 'author_id': 138304, 'author_profile': 'https://Stackoverflow.com/users/138304', 'pm_score': 3, 'selected': False, 'text': '<ol>\n<li>This is a vast subject. Do not underestimate this point. And do not underestimate my point to not underestimate it.</li>\n<li>I hear the <a href="http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools" rel="nofollow noreferrer">Dragon Book</a> is a (the?) place to start, along with searching. :) Get better at searching, eventually it will be your life.</li>\n<li>Building your own programming language is absolutely a good exercise! But know that it will never be used for any practical purpose in the end. Exceptions to this are few and <em>very</em> far between.</li>\n</ol>\n'}, {'answer_id': 1156391, 'author': 'Zachary Murray', 'author_id': 134547, 'author_profile': 'https://Stackoverflow.com/users/134547', 'pm_score': 4, 'selected': False, 'text': '<p>You might want to look into Lex/Yacc (or Flex/Bison, whatever you want to call them). Flex is a lexical analyzer, which will parse and identify the semantic components ("tokens") of your language, and Bison will be used to define what happens when each token is parsed. This could be, but is definitely not limited to, printing out C code, for a compiler that would compile to C, or dynamically running the instructions.</p>\n\n<p><a href="http://tldp.org/HOWTO/Lex-YACC-HOWTO-5.html" rel="noreferrer">This FAQ</a> should help you, and <a href="http://www.mactech.com/articles/mactech/Vol.16/16.07/UsingFlexandBison/" rel="noreferrer">this tutorial</a> looks quite useful.</p>\n'}, {'answer_id': 1156434, 'author': 'mrduclaw', 'author_id': 139463, 'author_profile': 'https://Stackoverflow.com/users/139463', 'pm_score': 6, 'selected': False, 'text': '<p>This is a pretty vague question, I think; just because of the depth of the topic involved.  A compiler can be decomposed into two separate parts, however; a top-half and a bottom-one.  The top-half generally takes the source language and converts it into an intermediate representation, and the bottom half takes care of the platform specific code generation.</p>\n\n<p>Nonetheless, one idea for an easy way to approach this topic (the one we used in my compilers class, at least) is to build the compiler in the two pieces described above.  Specifically, you\'ll get a good idea of the entire process by just building the top-half.</p>\n\n<p>Just doing the top half lets you get the experience of writing the lexical analyzer and the parser and go to generating some "code" (that intermediate representation I mentioned).  So it will take your source program and convert it to another representation and do some optimization (if you want), which is the heart of a compiler.  The bottom half will then take that intermediate representation and generate the bytes needed to run the program on a specific architecture.  For example, the the bottom half will take your intermediate representation and generate a PE executable.  </p>\n\n<p>Some books on this topic that I found particularly helpful was <a href="https://rads.stackoverflow.com/amzn/click/com/0321486811" rel="noreferrer" rel="nofollow noreferrer">Compilers Principles and Techniques</a> (or the Dragon Book, due to the cute dragon on the cover).  It\'s got some great theory and definitely covers Context-Free Grammars in a really accessible manner.  Also, for building the lexical analyzer and parser, you\'ll probably use the *nix tools lex and yacc.  And uninterestingly enough, the book called "<a href="https://rads.stackoverflow.com/amzn/click/com/1565920007" rel="noreferrer" rel="nofollow noreferrer">lex and yacc</a>" picked up where the Dragon Book left off for this part.</p>\n'}, {'answer_id': 1156689, 'author': 'greyfade', 'author_id': 33345, 'author_profile': 'https://Stackoverflow.com/users/33345', 'pm_score': 2, 'selected': False, 'text': "<p>I'm surprised it hasn't been mentioned, but Donald Knuth's <em>The Art of Computer Programming</em> was originally penned as a sort of tutorial on compiler writing.</p>\n\n<p>Of course, Dr. Knuth's propensity for going in-depth on topics has led to the compiler-writing tutorial being expanded to an estimated 9 volumes, only three of which have actually been published. It's a rather complete exposition on programming topics, and covers everything you would ever need to know about writing a compiler, in minute detail.</p>\n"}, {'answer_id': 1156750, 'author': 'Mike Dunlavey', 'author_id': 23771, 'author_profile': 'https://Stackoverflow.com/users/23771', 'pm_score': -1, 'selected': False, 'text': "<p>Whenever I want to try out a new language idea, I just write a simple parser, and have it generate some language that's easy to get good compilers for, like C.</p>\n\n<p>How do you think C++ was done?</p>\n"}, {'answer_id': 1158408, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>Generally speaking, there\'s no five minutes tutorial for compilers, because it\'s a complicated topic and writing a compiler can take months. You will have to do your own search.</p>\n\n<p>Python and Ruby are usually interpreted. Perhaps you want to start with an interpreter as well. It\'s generally easier.</p>\n\n<p>The first step is to write a formal language description, the grammar of your programming language. Then you have to transform the source code that you want to compile or interpret according to the grammar into an abstract syntax tree, an internal form of the source code that the computer understands and can operate on. This step is usually called parsing and the software that parses the source code is called a parser. Often the parser is generated by a parser generator which transform a formal grammar into source oder machine code. For a good, non-mathematical explanation of parsing I recommend Parsing Techniques - A Practical Guide. Wikipedia has a comparison of parser generators from which you can choose that one that is suitable for you. Depending on the parser generator you chose, you will find tutorials on the Internet and for really popular parser generators (like GNU bison) there are also books.</p>\n\n<p>Writing a parser for your language can be really hard, but this depends on your grammar. So I suggest to keep your grammar simple (unlike C++); a good example for this is LISP.</p>\n\n<p>In the second step the abstract syntax tree is transformed from a tree structure into a linear intermediate representation. As a good example for this Lua\'s bytecode is often cited. But the intermediate representation really depends on your language.</p>\n\n<p>If you are building an interpreter, you will simply have to interpret the intermediate representation. You could also just-in-time-compile it. I recommend LLVM and libjit for just-in-time-compilation. To make the language usable you will also have to include some input and output functions and perhaps a small standard library.</p>\n\n<p>If you are going to compile the language, it will be more complicated. You will have to write backends for different computer architectures and generate machine code from the intermediate representation in those backends. I recommend LLVM for this task.</p>\n\n<p>There are a few books on this topic, but I can recommend none of them for general use. Most of them are too academic or too practical. There\'s no "Teach yourself compiler writing in 21 days" and thus, you will have to buy several books to get a good understanding of this entire topic. If you search the Internet, you will come across some some online books and lecture notes. Maybe there\'s a university library nearby you where you can borrow books on compilers.</p>\n\n<p>I also recommend a good background knowledge in theoretical computer science and graph theory, if you are going to make your project serious. A degree in computer science will also be helpful.</p>\n'}, {'answer_id': 1241910, 'author': 'Pandafox', 'author_id': 150636, 'author_profile': 'https://Stackoverflow.com/users/150636', 'pm_score': 3, 'selected': False, 'text': '<p>I remember asking this question about seven years ago when I was rather new to programming.</p>\n\n<p>I was very careful when I asked and surprisingly I didn\'t get as much criticism as you are getting here. They did however point me in the direction of the "<a href="http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools" rel="nofollow noreferrer">Dragon Book</a>" which is in my opinion, a really great book that explains everything you need to know to write a compiler (you will of course have to master a language or two. The more languages you know, the merrier.).</p>\n\n<p>And yes, many people say reading that book is crazy and you won\'t learn anything from it, but I disagree completely with that.</p>\n\n<p>Many people also say that writing compilers is stupid and pointless. Well, there are a number of reasons why compiler development are useful:</p>\n\n<ul>\n<li>Because it\'s fun.</li>\n<li>It\'s educational, when learning how to write compilers you will learn a lot about computer science and other techniques that are useful when writing other applications.</li>\n<li>If nobody wrote compilers the existing languages wouldn\'t get any better.</li>\n</ul>\n\n<p>I didn\'t write my own compiler right away, but after asking I knew where to start. And now, after learning many different languages and reading the Dragon Book, writing isn\'t that much of a problem. (I\'m also studying computer engineering atm, but most of what I know about programming is self taught.)</p>\n\n<p>In conclusion, The Dragon Book is a great "tutorial". But spend some time mastering a language or two before attempting to write a compiler. Don\'t expect to be a compiler guru within the next decade or so though.</p>\n\n<p>The book is also good if you want to learn how to write parsers/interpreters.</p>\n'}, {'answer_id': 1344367, 'author': 'Ira Baxter', 'author_id': 120163, 'author_profile': 'https://Stackoverflow.com/users/120163', 'pm_score': 3, 'selected': False, 'text': '<p>Not a book, but a technical paper and an enormously fun learning experience if you want to know more about compilers (and metacompilers)... This website walks you through building a completely self-contained compiler system that can compile itself and other languages:</p>\n\n<p><em><a href="http://www.bayfronttechnologies.com/mc_tutorial.html" rel="nofollow noreferrer">Tutorial: Metacompilers Part 1</a></em></p>\n\n<p>This is all based on an amazing little 10-page technical paper:</p>\n\n<p><i>Val Schorre</i> <b>META II: A Syntax-Oriented Compiler Writing Language</b></p>\n\n<p>from honest-to-god 1964. I learned how to build compilers from this back in 1970. There\'s a mind-blowing moment when you finally grok how the compiler can regenerate itself....</p>\n\n<p>I know the website author from my college days, but I have nothing to do with the website.</p>\n'}, {'answer_id': 2853691, 'author': 'Taylor Leese', 'author_id': 105744, 'author_profile': 'https://Stackoverflow.com/users/105744', 'pm_score': 4, 'selected': False, 'text': '<p>Take a look at the book below. The author is the creator of <a href="http://www.antlr.org/" rel="noreferrer">ANTLR</a>.</p>\n\n<p><a href="http://pragprog.com/titles/tpdsl/language-implementation-patterns" rel="noreferrer">Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages</a>.</p>\n\n<p><img src="https://i.stack.imgur.com/ukOkz.jpg" alt="alt text"></p>\n'}, {'answer_id': 3125367, 'author': 'joe snyder', 'author_id': 357174, 'author_profile': 'https://Stackoverflow.com/users/357174', 'pm_score': 3, 'selected': False, 'text': '<p>From the <a href="http://www.faqs.org/faqs/compilers/faq/" rel="noreferrer">comp.compilers FAQ</a>:</p>\n\n<p>"Programming a Personal Computer" by Per Brinch Hansen\nPrentice-Hall 1982 ISBN 0-13-730283-5</p>\n\n<p>This unfortunately-titled book\nexplains the design and creation of a single-user programming environment\nfor micros, using a Pascal-like language called Edison.  The author presents\nall source code and explanations for the step-by-step implementation of an\nEdison compiler and simple supporting operating system, all written in\nEdison itself (except for a small supporting kernel written in a symbolic\nassembler for PDP 11/23; the complete source can also be ordered for the IBM\nPC).</p>\n\n<p>The most interesting things about this book are:  1) its ability to\ndemonstrate how to create a complete, self-contained, self-maintaining,\nuseful compiler and operating system, and 2) the interesting discussion of\nlanguage design and specification problems and trade-offs in Chapter 2.</p>\n\n<p>"Brinch Hansen on Pascal Compilers" by Per Brinch Hansen\nPrentice-Hall 1985 ISBN 0-13-083098-4</p>\n\n<p>Another light-on-theory\nheavy-on-pragmatics here\'s-how-to-code-it book.  The author presents the\ndesign, implementation, and complete source code for a compiler and p-code\ninterpreter for Pascal- (Pascal "minus"), a Pascal subset with boolean and\ninteger types (but no characters, reals, subranged or enumerated types),\nconstant and variable definitions and array and record types (but no packed,\nvariant, set, pointer, nameless, renamed, or file types), expressions,\nassignment statements, nested procedure definitions with value and variable\nparameters, if statements, while statements, and begin-end blocks (but no\nfunction definitions, procedural parameters, goto statements and labels,\ncase statements, repeat statements, for statements, and with statements).</p>\n\n<p>The compiler and interpreter are written in Pascal* (Pascal "star"), a\nPascal subset extended with some Edison-style features for creating\nsoftware development systems.  A Pascal* compiler for the IBM PC is sold by\nthe author, but it\'s easy to port the book\'s Pascal- compiler to any\nconvenient Pascal platform.</p>\n\n<p>This book makes the design and implementation of a compiler look easy.  I\nparticularly like the way the author is concerned with quality,\nreliability, and testing.  The compiler and interpreter can easily be used\nas the basis for a more involved language or compiler project, especially\nif you\'re pressed to quickly get something up and running. </p>\n'}, {'answer_id': 3125860, 'author': 'Jay', 'author_id': 114979, 'author_profile': 'https://Stackoverflow.com/users/114979', 'pm_score': 2, 'selected': False, 'text': "<p>Missing from the list: Garbage Collection: Algorithms for Automatic Dynamic Memory Management, by Jones and Lins.</p>\n\n<p>(Assuming you're writing the compiler <em>and</em> runtime system, and that you're implementing a garbage collected language.</p>\n"}, {'answer_id': 3443032, 'author': 'Lothar', 'author_id': 155082, 'author_profile': 'https://Stackoverflow.com/users/155082', 'pm_score': 4, 'selected': False, 'text': '<p>I found the Dragon book much too hard to read with too much focus on language theory that is not really required to write a compiler in practice. </p>\n\n<p>I would add the <a href="http://en.wikipedia.org/wiki/Oberon_%28programming_language%29" rel="noreferrer">Oberon</a> book which contains the full source of an amazingly fast and simple Oberon compiler <a href="http://www.amazon.de/Project-Oberon-Design-Operating-Compiler/dp/0201544288/ref=sr_1_3?ie=UTF8&amp;s=books-intl-de&amp;qid=1281378762&amp;sr=8-3" rel="noreferrer">Project Oberon</a>.</p>\n\n<p><img src="https://i.stack.imgur.com/N3GFA.jpg" alt="Alt text"></p>\n'}, {'answer_id': 3592870, 'author': 'jochenleidner', 'author_id': 1527677, 'author_profile': 'https://Stackoverflow.com/users/1527677', 'pm_score': 5, 'selected': False, 'text': '<p>If you have little time, I recommend <a href="http://www.ethoberon.ethz.ch/WirthPubl/CBEAll.pdf" rel="noreferrer">Niklaus Wirth\'s "Compiler Construction" (Addison-Wesley. 1996)</a>, a tiny little booklet that you can read in a day, but it explains the basics (including how to implement lexers, recursive descent parsers, and your own stack-based virtual machines). After that, if you want a deep dive, there\'s no way around the Dragon book as other commenters suggest.</p>\n'}, {'answer_id': 5518042, 'author': 'Bubbles', 'author_id': 631423, 'author_profile': 'https://Stackoverflow.com/users/631423', 'pm_score': 2, 'selected': False, 'text': '<p>The quickest approach is through two books:</p>\n\n<p>1990 version of <a href="https://rads.stackoverflow.com/amzn/click/com/007709221X" rel="nofollow noreferrer" rel="nofollow noreferrer">An Introduction to Compiling Techniques, a First Course using ANSI C, LeX, and YaCC</a> by JP Bennett - a perfect balance of example code, parsing theory and design- it contains a complete compiler  written in C, lex and yacc for a simple grammar</p>\n\n<p><a href="https://rads.stackoverflow.com/amzn/click/com/0201000229" rel="nofollow noreferrer" rel="nofollow noreferrer">Dragon Book</a> (older version) - mostly a detailed reference for the features not covered in the former book</p>\n'}, {'answer_id': 6695987, 'author': 'timaschew', 'author_id': 736518, 'author_profile': 'https://Stackoverflow.com/users/736518', 'pm_score': 3, 'selected': False, 'text': '<p>You can use <a href="http://jakarta.apache.org/bcel/" rel="noreferrer">BCEL</a> by the Apache Software Foundation. With this tool you can generate assembler-like code, but it\'s Java with the BCEL API. You can learn how you can generate intermediate language code (in this case byte code).</p>\n\n<h3>Simple example</h3>\n\n<ol>\n<li><p>Create a Java class with this function:</p>\n\n<pre><code>public String maxAsString(int a, int b) {\n    if (a &gt; b) {\n        return Integer.valueOf(a).toString();\n    } else if (a &lt; b) {\n        return Integer.valueOf(b).toString();\n    } else {\n        return "equals";\n    }\n}\n</code></pre></li>\n</ol>\n\n<p>Now run BCELifier with this class</p>\n\n<pre><code>BCELifier bcelifier = new BCELifier("MyClass", System.out);\nbcelifier.start();\n</code></pre>\n\n<p>You can see the result on the console for the whole class (how to build byte code MyClass.java). The code for the function is this:</p>\n\n<pre><code>private void createMethod_1() {\n  InstructionList il = new InstructionList();\n  MethodGen method = new MethodGen(ACC_PUBLIC, Type.STRING, new Type[] { Type.INT, Type.INT }, new String[] { "arg0", "arg1" }, "maxAsString", "MyClass", il, _cp);\n\n  il.append(InstructionFactory.createLoad(Type.INT, 1)); // Load first parameter to address 1\n  il.append(InstructionFactory.createLoad(Type.INT, 2)); // Load second parameter to adress 2\n    BranchInstruction if_icmple_2 = InstructionFactory.createBranchInstruction(Constants.IF_ICMPLE, null); // Do if condition (compare a &gt; b)\n  il.append(if_icmple_2);\n  il.append(InstructionFactory.createLoad(Type.INT, 1)); // Load value from address 1 into the stack\n  il.append(_factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Constants.INVOKESTATIC));\n  il.append(_factory.createInvoke("java.lang.Integer", "toString", Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL));\n  il.append(InstructionFactory.createReturn(Type.OBJECT));\n  InstructionHandle ih_13 = il.append(InstructionFactory.createLoad(Type.INT, 1));\n  il.append(InstructionFactory.createLoad(Type.INT, 2));\n    BranchInstruction if_icmpge_15 = InstructionFactory.createBranchInstruction(Constants.IF_ICMPGE, null); // Do if condition (compare a &lt; b)\n  il.append(if_icmpge_15);\n  il.append(InstructionFactory.createLoad(Type.INT, 2));\n  il.append(_factory.createInvoke("java.lang.Integer", "valueOf", new ObjectType("java.lang.Integer"), new Type[] { Type.INT }, Constants.INVOKESTATIC));\n  il.append(_factory.createInvoke("java.lang.Integer", "toString", Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL));\n  il.append(InstructionFactory.createReturn(Type.OBJECT));\n  InstructionHandle ih_26 = il.append(new PUSH(_cp, "equals")); // Return "equals" string\n  il.append(InstructionFactory.createReturn(Type.OBJECT));\n  if_icmple_2.setTarget(ih_13);\n  if_icmpge_15.setTarget(ih_26);\n  method.setMaxStack();\n  method.setMaxLocals();\n  _cg.addMethod(method.getMethod());\n  il.dispose();\n}\n</code></pre>\n'}, {'answer_id': 22357244, 'author': 'magneto12321', 'author_id': 3396829, 'author_profile': 'https://Stackoverflow.com/users/3396829', 'pm_score': 2, 'selected': False, 'text': '<p>Not included in the list so far is this book:<p></p>\n\n<p><a href="http://www.diku.dk/~torbenm/Basics/" rel="noreferrer">Basics of Compiler Design (Torben Mogensen)</a>\n(from the dept. of Computer Science, University of Copenhagen)<p></p>\n\n<p>I\'m also interested in learning about compilers and plan to enter that industry in the next couple of years. This book is the ideal theory book to begin learning compilers as far as I can see. It\'s FREE to copy and reproduce, cleanly and carefully written and gives it to you in plain English without any code but still presents the mechanics by way of instructions and diagrams etc. Worth a look imo. </p>\n'}, {'answer_id': 22811770, 'author': 'uliwitness', 'author_id': 242278, 'author_profile': 'https://Stackoverflow.com/users/242278', 'pm_score': 2, 'selected': False, 'text': '<p>If you\'re not just looking for books, but also interested in web sites that have articles on the topic, I\'ve blogged about various aspects of creating a programming language. Most of the posts can be found in my blog\'s <a href="http://web.archive.org/web/20161024215644/http://orangejuiceliberationfront.com/category/language-design/" rel="nofollow noreferrer">"Language Design" category</a>.</p>\n\n<p>In particular, I cover generating Intel machine code manually, automatically generating machine- or bytecode, creating a bytecode interpreter, writing an object-oriented runtime, creating a simple loader, and writing a simple mark/sweep garbage collector. All of this in a very practical and pragmatic way instead of boring you with lots of theory.</p>\n\n<p>Would appreciate feedback on these.</p>\n'}]
['https://Stackoverflow.com/questions/1669', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/340/']