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!

Prabith Balagopalan

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

回复
Shine Xavier

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.

回复
Anand Iyer

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!

回复
Sarath S.

Senior Software Engineer

9 年

Reached Step 7 :)

回复

要查看或添加评论,请登录

Praseed Pai K.T.的更多文章

  • How Russel arrived at Barber Paradox?

    How Russel arrived at Barber Paradox?

    (This was published in the blog, "Analysis Leads to Paralysis" in the year 2013) I was explaining Barber paradox to a…

    5 条评论
  • Encumbrance in Object Oriented Design

    Encumbrance in Object Oriented Design

    (This was Posted in my Blog, "Analysis leads to Paralysis" in the year 2010) Encumbrance is a legal term which has been…

  • I think, Claude Shannon was dead right.!

    I think, Claude Shannon was dead right.!

    (This is a slightly modified version of an article which I wrote in my blog, "Analysis Leads to Paralysis", in the…

    1 条评论
  • Check whether a number is Fibonacci number or not?

    Check whether a number is Fibonacci number or not?

    Sunday, May 25, 2014 (This was a Blog Entry Published in the Year 2014 in "Analysis leads to Paralysis") I just happen…

  • Why Indian Philosophy is "superior" to modern Western Philosophy?

    Why Indian Philosophy is "superior" to modern Western Philosophy?

    The Celebrated French Philosopher, Rene Descartes, is allegedly the father of Modern Western Philosophy by saying, the…

  • Joseph Goebbels and Software Design!

    Joseph Goebbels and Software Design!

    (The following text was published as a Blog entry in "Analysis Leads To Paralysis", a Blog which I used to write…

    1 条评论
  • Machine Learning Programs are far simpler than Computer Graphics Programming

    Machine Learning Programs are far simpler than Computer Graphics Programming

    Currently, I am preparing an Analytics course, which includes the following content Analysis/Synthesis Thinking Model…

    7 条评论
  • An Instance of a Logical Fallacy

    An Instance of a Logical Fallacy

    I collaborate with different individuals on multiple disciplines. Often, I propose my collaborators to venture into…

    1 条评论
  • Do you posses the temperament of a "Software Architect"?

    Do you posses the temperament of a "Software Architect"?

    Being a person who has got exposure to "mundane" Business Application development to Computer Aided Design (CAD)…

    6 条评论
  • Prime Obsession of the World?

    Prime Obsession of the World?

    Over a period of time , I have observed a curious phenomena, which I have also been a victim. It is search for an…

    11 条评论

社区洞察

其他会员也浏览了