Do you want to learn about Compiler Construction. that too very Fast?
When I started my computer programming career, back in the year circa 1993, I chanced upon a book written by Allen Holub. A book, titled, "Compiler Design in C". I flipped through the pages and decided that, I won't ever learn to write a compiler in my life. I settled for learning things like Data structures, Algorithms and Programming techniques.
But, cracking this book was a "goal" I have had in mind. Then, someone told about "Dragon book" (1986 edition) and my conviction that Compiler Construction is a difficult topic strengthened very much. Couple of years later, In 1995, I chanced upon a nifty book titled "Database Management using C" written by one Mr. Vipul Lal, published by BPB publications, New Delhi. I purchased the book for learning to write a "B-Tree" Indexing system and my attention soon got fixated on "Macro Processer" (Expression Evaluator) within the book.I ported the C language based code to Clipper Programming language and did add an additional exponentiation operator to the Clipper Code. Terms like Recursive Descent, Expression, term , Factor, Lexical Analysis, Code Generation, Evaluation stack etc. made real sense. A short in the arm was procurement of another book titled, "A Practical Compiler Construction using C" published by a sixteen year old boy, Kirat Singh. He showed how to write a Pascal Compiler using Recursive Descent, another implementation using Lex/Yacc, Subroutine, Virtual machine etc.These two unusual sources helped me to crack Compiler construction on a 16 bit Operating system like MSDOS. Afte repeatedly flipping through the book, I created a toy language with Variable declaration, Assignment Statement, Conditional Statement, Iteration etc. I went one step to generate x86 code on the fly.
What started out as a intellectual curiosity,turned to some kind of profit by
virtue of having designed DSLs and written interpreters/Compilers for the same.
It is a classic case of the adage, "For a person with a Hammer, Everything is a nail"
I happen to participate in projects with compiler nature like
Intereter for a DSL to write Spice (Digital Simulation) Code Model
Dynamic Compiler for a Business rule engine
Interpreter for a Spreadsheet Rules evaluation language
Transcompiler for generating Javascript compatible with Google's V8 engine
The Curiosity spilled into Functional Programming, Logic Programming and other programming paradigms. The experience in language processors helped me to understand the contents of books like "Esentials of Programming Languages" and "Structure and Interpreteration of Computer Programs".
Learning Compiler Construction using a standard textbook like Dragon Book is very difficult.Some books available are focused on Backend optimization.How can someone learn essentials of compiler building in a short period of time,became a focus area for me, sometime in the year 2009.The idea is to teach Lexical Analysis, Parsing, Tree generation,Static Type Analysis, Depth first tree walk for Interpretation, Code generation under Two Thousand Lines of Code. At that point in my time, C# was the primary programming language and I decided to use C#. Microsoft's Reflection.Emit package was suitable for writing a Compiler backend. In Java, the option was to depend upon Byte Code Engineering Library.
The Compiler supports
Three data types ( STRING,NUMERIC and BOOLEAN)
Control Structures (IF/WHILE)
Function Calls (including Recursion)
Static Typing
Interpretation
Code Generation (Microsoft IL)
Assembly Generation (.EXE)
Since the Source code is written in following Seven Steps
Abstract Syntax Tree (AST) and It's Composition for Expressions
Recursive Descent Parsing for Expressions and mapping to AST
Adding Simple Statements (Print/Printline)
Adding Types, Variable Declaration,Assignments
Generation of IL Code
Adding Conditional Constructs (IF/WHILE)
Adding Function Abstraction and Invocations
The Source code was released as an MIT licensed Open Source Code base through CodePlex.com, in early 2010. The Code Base contains an electronic book,titled, "The Art of Compiler Construction using C#". The Compiler has been since ported to Java, C++ (with an LLVM backend),Python,VB.net,Javascript,Ruby etc. Currently,a Backend for generating Java Byte code is being written.
The Compiler Source code can be retreived from https://slangfordotnet.codeplex.com and the e-book can be downloaded from https://github.com/praseedpai/CompilerConstructionBook
After reading and porting the compiler to a language of your choice, every compiler book out there will make sense to you.
Enjoy!
Architect at Cognizant | AWS | Azure | CSM | Agile | .Net Technologies | Cloud Architecture
9 年Thanks Praseed. I attended one of your session earlier on this topic and it was very informative
Systems Architect | AI & Web3.0 Innovator | Team Builder | Author
9 年Truly an effective and easy reference for writing a compiler, having embarked upon writing one myself. Concepts well compiled, though some require detailing. I guess the author leaves that to the reader to explore on his/her own.
Principal Consultant-Trainer
9 年Praseed, you're as geeky as when I met you first, ten years ago! Ever glad to have been in your company! About this article, even my repeated trials to motivate myself into understanding this haven't yielded much benefit...beyond me, is what I think of such things that I find hard!
Senior Software Engineer
9 年Reached Step 7 :)