Writing a Compiler in C - compiler-theory

I am very interested in compilers and how they work so I want to know if there are good sources or books to write a compiler from scratch. As I am a novice C programmer it would be perfect if the guide(s) is for C.
Thank you.

"Compiler Construction for Digital Computers" 1st Edition by David Gries
is my favorite, and it has good reviews

There's also the book Compiler Design in C by Allen Holub, which is available electronically. It's also a bit newer than Gries' book (1990 as opposed to 1971).

Related

What is a good intermediate introduction to Scheme?

I am trying to find a resource where I can practice my programming skills in scheme.
I have a class coming up that uses scheme exclusively. While I have done moderate amounts of programming in C++ and Java, and grasp the basic ideas about good/bad programming practices and different programming paradigms (as much as a second-year college student could be expected to), Scheme looks like hieroglyphics to me!
Any help would be appreciated.
If you haven't looked at Structure and Interpretation of Computer Programs yet, that's the obvious (and free!) place to start.
I really like The Little Schemer. The format is very different, but it all builds up from basics.
If you're looking for an intermediate introduction, SICP will not be a good choice. You could try HtDP, but that not too much of an intro to Scheme as it is an intro to programming in general.
The Scheme Programming Language, 4th Edition (Covers the language as described in R6RS.).
If you are using a Scheme that conforms only with R5RS, use the 3rd edition of the book.
Structure and Interpretation of Computer Programs is highly recommended.
The Guide to Racket http://docs.racket-lang.org/guide/index.html is a good place to find the information you need. It has a very good navigation system and many simple examples. You can use it with SICP.

Books on parallel programming in OpenMP and MPI

I've been programming with C and FORTRAN for the past 6 years, but I have only done serial programming until now. I would now like to switch to parallel program design, but I haven't really found a book that would help me make this jump. I have looked at Chandra's book on Parallel programming in OpenMP and Michael J. Quinn's book on programming in C with Open Mp and MPI, but I was wondering if there was a better alternative to these two. Thanks in advance!
Prashanth
I can recommend Using OpenMP by Chapman, Jost and Van de Pas. It's a really good introduction to OpenMP and includes plenty of details on using the language and getting best performance.
This tutorial might also be a useful introduction if you have never experienced parallel programming, it's a good entry to the general concepts and ideas.

Minimalist Programming Tools

What tools go well with or help minimalist programming? Examples would be libraries with tight, clean interface and very small size in it's genre.
Techniques, functions or concepts that result in smaller and/or more efficient apps would be great. If you know of any other relevant tools this would help as well.
This may not be quite what you're looking for, but I enjoyed reading A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux, which starts out with basic techniques for reducing bloat, before going into far more detail than I thought possible in order to shave every last byte from an executable!
if not assembler, then almost any Forth.
See colorFORTH - minimal and strange ... best of both worlds :)

PLT-Scheme learning reference

After having got through the two Schemer books, I'm about to embark on HtDP but also discovered the http://docs.plt-scheme.org/guide material.
The previously mentioned books are more particular to Scheme, it seems, and the latter being more geared towards PLT specific extensions (modules, require, bracket syntax, etc...). The online manual is excellent but I was hoping there might be a book form that I could purchase?
If not, I'm certainly grateful for the in-depth online manual - was just curious!
No, I don't believe so. The only other PLT-specific book that's in print right know (AFAIK) is the excellent "Semantics Engineering with PLT Redex", but I don't think that's what you're looking for. You might also be interested in Krishnamurthi's Programming Languages: Application and Interpretation. Both of these are targeted at programming languages folks.
HTH
How to Design Worlds
How to Design Programs
Programming Languages: Application and Interpretation
There is a pdf version of the guide which might be easier for you to use than the html document:
http://download.plt-scheme.org/doc/4.0.2/pdf/guide.pdf
I assume this is legit, feel free to edit my post if it isn't.

(When) Should I learn compilers?

According to this http://steve-yegge.blogspot.com/2007/06/rich-programmer-food.html article, I defnitely should.
Quote Gentle, yet insistent executive
summary: If you don't know how
compilers work, then you don't know
how computers work. If you're not 100%
sure whether you know how compilers
work, then you don't know how they
work.
I thought that it was a very interesting article, and the field of application is very useful (do yourself a favour and read it)
But then again, I have seen successful senior sw engineers that didn’t know compilers very well, or internal machine architecture for that matter,
but did know a thing or two of each item in the following list :
A programming paradigm (OO, functional,…)
A programming language API (C#, Java..) and at least 2 very different some say! (Java / Haskell)
A programming framework (Java, .NET)
An IDE to make you more productive (Eclipse, VisualStudio, Emacs,….)
Programming best practices (see fxcop rules for example)
Programming Principles (DRY, High Cohesion, Low Coupling, ….)
Programming methodologies (TDD, MDE)
Design patterns (Structural, Behavioural,….)
Architectural Basics (Tiers, Layers, Process Models (Waterfall, Agile,…)
A Testing Tool (Unit Testing, Model Testing, …)
A GUI technique (WPF, Swing)
A documenting tool (Javadoc, Sandcastle..)
A modelling languague (and tool maybe) (UML, VisualParadigm, Rational)
(undoubtedly forgetting very important stuff here)
Not all of these tools are necessary to be a good programmer (like a GUI when you just don’t need it)
but most of them are. Where do compilers come in, and are they really that important, since, as I mentioned,
lots of programmers seems to be doing fine without knowing them and especially, becoming a good programmer is seen the multitude of knowledge domains almost a lifetime achievement :-) , so even if compilers are extremely important, isn't there always stuff still more important?
Or should i order 'The Unleashed Compilers Unlimited Bible (in 24H..))) today?
For those who have read the article, and want to start studying right away :
Learning Resources on Parsers, Interpreters, and Compilers
If you just want to be a run-of-the-mill coder, and write stuff... you don't need to take compilers.
If you want to learn computer science and appreciate and really become a computer scientist, you MUST take compilers.
Compilers is a microcosm of computer science! It contains every single problem, including (but not limited to) AI (greedy algorithms & heuristic search), algorithms, theory (formal languages, automata), systems, architecture, etc.
You get to see a lot of computer science come together in an amazing way. Not only will you understand more about why programming languages work the way that they do, but you will become a better coder for having that understanding. You will learn to understand the low level, which helps at the high level.
As programmers, we very often like to talk about things being a "black box"... but things are a lot smoother when you understand a little bit about what's in the box. Even if you don't build a whole compiler, you will surely learn a lot. You will get to see the formalisms behind parsing (and realize it's not just a bunch of special cases hacked together), and a bunch of NP complete problems. You will see why the theory of computer science is so important to understand for practical things. (After all, compilers are extremely practical... and we wouldn't have the compilers we have today without formalisms).
I really hope you consider learning about them... it will help you get to the next level as a computer scientist :-).
You should learn about compilers, for the simple reason that implementing a compiler makes you a better programmer. The compiler will surely suck, but you will have learned a lot during the way. It is a great way of improving (or practising) your programming skill.
You do not need to understand compilers to be a good programmer, but it can help. One of the things I realized when learning about them, is that compiling is simply a translation.
If you have ever translated from one language to another, you have just done compiling.
So when should you learn about compilers?
When you want to, or need it to solve a problem.
Compiler theory is useful, but not essential.
Although there are some techniques which come in handy, like lexical analysis and parsing.
Another one is error handling. Compilers need a lot of these. User input can contain anything, even the unexpected. And you need to deal with all of these.
If you're going to be working at a high-enough level where you're worrying over UML and self-describing code, you could easily go your entire career without wanting or needing intimate details of how the compiler works.
But, if you're an in-the-trenches coder and have no aspirations to manage your friends, it's likely that one day, you'll realize you're waging war with your compiler. It could be a random bug that comes along or a hallway conversation about while-verses-for loops. You'll realize the assembly (or IL, likely, in the coming years) is just a bit to the left of what you were needing and another universe will unfold.
So, I suppose my answer is, just be aware of the compiler for now, that it's doing quite a lot, but don't worry over it too much.
The compilers courses usually focus on how the high-level code is analyzed and translated into machine code. That's very interesting, but not crucial. It's more important to understand what is this machine code that is generated by the compiler so that you understand how a computer works and what is the cost of each language construct.
So I'd rather say that you should know an assembly language (I mean a limited subset of assembly language for one architecture) to understand how a computer works and the latter is definitely required for a competent programmer so that he understands what segmenation fault is, when to optimize and when not and other similar low-level things.
If you intend to write extremely time-critical real-time code, you will benefit from understanding how the compiler optimises your code. However, you will actually benefit more from understanding the underlying architecture of your hardware.
From my experience, if you understand how the hardware works, and how the compiler interprets your code, you will be able to write code that does exactly what you intend it to do. I have been caught on several occasions, writing code that got optimised away by the compiler and made the hardware do something that I did not intend.
All in all, understanding the entire software-hardware stack is not essential to write good algorithms and code, but it will most certainly help!
From a practical perspective, general compiler theory is less of concern than a assembler, linker and loader to a specific platform. For example, I just consider the GCC compiler as a translator from my high-level C language to the low-level assembly language on a x86 platform. And more often than not, I manually refine ;) the code generated by the compiler.
From a scientific perspective, I would strongly suggest you learning the compiler theory, it will help you understand the great idea that computer is built upon. And even more, you will have a different eye upon the world.
Just my opinion, but I believe compilers is not given enough attention in CS courses, not in mine, and not in any others afaik. I think any CS major should do 2 things after a sabbatical or finishing their major: Re-learn if necessary finite automata and maybe a formal methods language. Apply it.
Write a simple compiler with this knowledge. Alex Aiken has a very useful online tutorial on writing a compiler for the COOL (Classroom Object Oriented Language) which is a subset of Scala as of 2013 ver. At least at time of writing.

Resources