Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ann: Practical Compiler Construction

164 views
Skip to first unread message

Nils M Holm

unread,
Apr 28, 2012, 2:09:24 AM4/28/12
to

I thought you might be interested in my latest textbook,

PRACTICAL COMPILER CONSTRUCTION
A No-nonsense Tour through a C Compiler

which is now available at Lulu.com:

http://www.lulu.com/content/12610903 (Paperback book)
http://www.lulu.com/content/12685672 (PDF download)

See also:

http://www.t3x.org/reload/ (Contents, Source code, etc)

The book illuminates the practical side of compiler
construction and takes the reader on a tour through
the full source code of a compiler for a C subset.

It is a book for seasoned programmers with little
or no background in compiler-writing.

Theory will be covered when necessary, but the focus
is always on actual code. The compiler discussed in
the book is a clean and simple implementation that
does not "bend the rules" like many other small C
compilers. Its sources pass gcc -Wall -pedantic with
little or no warnings (depending on the gcc version).

The compiler and runtime environment is designed to run
on FreeBSD on a 386 processor, but a post-book version
of SubC has already been ported to FreeBSD/x86-64,
NetBSD/x86-64 as well as Linux/386. Porting it to other
Unix-like systems should be easy.

The complete code from the book is in the public domain
and can be downloaded at

http://www.t3x.org/subc/ (book version)
http://www.t3x.org/subc/current/ (post-book version)

Enjoy!

--
Nils M Holm < n m h @ t 3 x . o r g > www.t3x.org

Rugxulo

unread,
May 3, 2012, 2:03:29 PM5/3/12
to
Hi,

On Apr 28, 1:09 am, Nils M Holm <news2...@t3x.org> wrote:
>
> I thought you might be interested in my latest textbook,

I've barely noticed you before, and you seem like you know what you're
doing! Way more than me, at least. ;-)

> PRACTICAL COMPILER CONSTRUCTION
> A No-nonsense Tour through a C Compiler
>
> which is now available at Lulu.com:
>
> http://www.lulu.com/content/12610903    (Paperback book)http://www.lulu.com/content/12685672    (PDF download)

Sounds almost interesting, though I'm not very skilled at C. In fact,
I'd consider ANSI C to be "too hard" to implement, personally, but
kudos for getting this far (quite well done!).

I almost, for some odd reason, expected this to be written in Scheme
or something else. Please ignore this ramble, but I'm a little sad
it's not written in Pascal (or Oberon?) or something else, heheh.
Though admittedly it's better in a lot of ways to write it in itself.

> See also:
>
> http://www.t3x.org/reload/  (Contents, Source code, etc)
>
> The book illuminates the practical side of compiler
> construction and takes the reader on a tour through
> the full source code of a compiler for a C subset.

Indeed, quite complete (to my eyes), though not full, but still very
good and impressive.

> It is a book for seasoned programmers with little
> or no background in compiler-writing.

Seasoned, ugh, heheh.

> Theory will be covered when necessary, but the focus
> is always on actual code. The compiler discussed in
> the book is a clean and simple implementation that
> does not "bend the rules" like many other small C
> compilers. Its sources pass gcc -Wall -pedantic with
> little or no warnings (depending on the gcc version).

"make lint" doesn't work out of the box, but that's an easy fix. It
does whine once or twice about some stuff, but nothing major. But
yeah, "-ansi -pedantic -Wall" (what, no "-Wextra"?).

> The compiler and runtime environment is designed to run
> on FreeBSD on a 386 processor, but a post-book version
> of SubC has already been ported to FreeBSD/x86-64,
> NetBSD/x86-64 as well as Linux/386. Porting it to other
> Unix-like systems should be easy.

Needs GAS and LD, blech. Heh, I'd almost rather have a DJGPP targeted
version, but I'm too lazy. ;-)

Honestly, while I understand that C will always need a linker, in this
case I think it'd almost be better to just use FASM, which can output
ELF reloc (for later use) or executable directly. It runs on all those
OSes and assembles itself (and as such is much easier to bootstrap
than BinUtils). Though I guess it's not that important overall.

> The complete code from the book is in the public domain
> and can be downloaded at
>
> http://www.t3x.org/subc/          (book version)http://www.t3x.org/subc/current/  (post-book version)

It's pretty good, I played with it a very tiny bit. Lacks most CPP
stuff (#if) and doesn't understand "long" or "L" suffix (though all
ints are 32-bit anyways) or the "l" in printf("%ld"), or "typedef" or
"unsigned", but otherwise seems very useful. Seems to come with a
subset of a libc too, and it can compile itself (statically!).

You managed to do this in relatively (very) few lines of code. Of
course, self-hosting makes this easier. Congrats! ;-)

Nils M Holm

unread,
May 4, 2012, 1:36:14 AM5/4/12
to
Rugxulo <rug...@gmail.com> wrote:
> On Apr 28, 1:09 am, Nils M Holm <news2...@t3x.org> wrote:
> > PRACTICAL COMPILER CONSTRUCTION
> > A No-nonsense Tour through a C Compiler
> >
> > which is now available at Lulu.com:
> >
> > http://www.lulu.com/content/12610903    (Paperback book)
> > http://www.lulu.com/content/12685672    (PDF download)
>
> Sounds almost interesting, though I'm not very skilled at C. In fact,
> I'd consider ANSI C to be "too hard" to implement, personally, but
> kudos for getting this far (quite well done!).

Thank you! I agree that C is not easy to implement. This is why I
chose a subset of the language for the textbook. A line-by-line
tour through a full C compiler probably would have filled a volume
of 2000 pages. ;-)

> I almost, for some odd reason, expected this to be written in Scheme
> or something else.

Shameless plug: if you are looking for something written in Scheme,
have a look at Scheme 9 from Empty Space, by the same author. :-)

> Please ignore this ramble, but I'm a little sad
> it's not written in Pascal (or Oberon?) or something else, heheh.
> Though admittedly it's better in a lot of ways to write it in itself.

I have already gotten requests for books about Javascript and Logo.
Well, I guess, you have to set some priorities.

> "make lint" doesn't work out of the box, but that's an easy fix. It
> does whine once or twice about some stuff, but nothing major. But
> yeah, "-ansi -pedantic -Wall" (what, no "-Wextra"?).

I have deliberately ignored the warnings about mixing && and ||
without parentheses, because I find the additional parens ugly.
And then every version of GCC seems to add new warnings, and I am
using a rather old release. I did not know about -Wextra.

> Honestly, while I understand that C will always need a linker, in this
> case I think it'd almost be better to just use FASM, which can output
> ELF reloc (for later use) or executable directly. It runs on all those
> OSes and assembles itself (and as such is much easier to bootstrap
> than BinUtils). Though I guess it's not that important overall.

GAS and LD have the big advantage of already being there on most systems.
You do not have to install any additional clutter. On *BSD, SubC also
uses the system call wrapper of the system's libc. The Linux version
is completely stand-alone, because the GNU libc is... well, a bit
special. To put it in a euphemistic way. The latest ports can be found
here: http://www.t3x.org/subc/current/

> It's pretty good, I played with it a very tiny bit. Lacks most CPP
> stuff (#if) and doesn't understand "long" or "L" suffix (though all
> ints are 32-bit anyways) or the "l" in printf("%ld"), or "typedef" or
> "unsigned", but otherwise seems very useful. Seems to come with a
> subset of a libc too, and it can compile itself (statically!).

A lot of things are missing. There are only ints, chars, void pointers
in the book version. The current version adds structs and unions. A
complete list of omissions is in the book and in the preview (View
Introduction) on the book home page: http://www.t3x.org/reload/

The libc implementation of SubC is quite complete, but not very
efficient. It is small, though, so the static executables tend to be
small, too. The compiler itself, for example, has a static size of
about 190KB.

> You managed to do this in relatively (very) few lines of code. Of
> course, self-hosting makes this easier. Congrats! ;-)

Thanks!
0 new messages