Live data from Hacker News

How to Write Your Own Compiler (2009)

staff.polito.it

11–20 of 43 posts

Re: How to Write Your Own Compiler (2009)

#11
post #2

From its earliest year UNIX/Linux had utilities including lex and yacc that automate much of these fuctions. I tended to use awk to create powerful new command scripts.

lex and yacc only automate one tiny part of the front-end of a compiler.

Also, if you're learning to write your own compiler, you should, IMO, implement the lexer/parser (if you haven't done so before) to understand how they work.

Re: How to Write Your Own Compiler (2009)

#12
post #3

One of the best resources I've seen on this is Capon and Jinks 'Compiler Engineering Using Pascal' (1988; ISBN 0-333-47155-5) if you can get hold of it http://www.cs.man.ac.uk/~pjj/book.html

I took "compiler techniques" at UMASS Amherst where we built a compiler for a subset of pascal. It was one of the hardest classes i took for my undergrad, but i learnt so much from it. I think building a compiler is somthing every cs major should do once, even if it's just for a subset of a language.

Re: How to Write Your Own Compiler (2009)

#13

May I suggest considering Forth as a substrate? Or will that get me voted straight into neckbeard land? I've had a lot of fun hacking Forths, writing my own languages didn't really click until I finally had a serious look. Forth skips on most of the complexity of getting from bytes to semantics; even Lisp is complex in comparison; but is still powerful enough to do interesting things; and the best foundation for DSL'…

Forth is one of those things that always fascinates me but ultimately always ends up feeling too hard to read.

Snabel looks like an interesting experiment in that regard..

Re: How to Write Your Own Compiler (2009)

#14
post #13

May I suggest considering Forth as a substrate? Or will that get me voted straight into neckbeard land? I've had a lot of fun hacking Forths, writing my own languages didn't really click until I finally had a serious look. Forth skips on most of the complexity of getting from bytes to semantics; even Lisp is complex in comparison; but is still powerful enough to do interesting things; and the best foundation for DSL'…

Forth is one of those things that always fascinates me but ultimately always ends up feeling too hard to read. Snabel looks like an interesting experiment in that regard..

Lisp and Forth, everyone gets that reaction on first contact; the real problem is that most popular languages look the same these days. Popularity has very little to do with earning it. And it's getting worse with more and more crapscript lately.

I'm not very much into dogmatics and orthodoxy, Forth wasn't the final answer to anything. Nothing ever is. Implementing the same thing over and over again doesn't make sense to me; I have plenty of experience of my own that thinks differently, things I always wanted to work differently. So that's what I'm doing, my best to improve the status quo.

Re: How to Write Your Own Compiler (2009)

#16
post #13

May I suggest considering Forth as a substrate? Or will that get me voted straight into neckbeard land? I've had a lot of fun hacking Forths, writing my own languages didn't really click until I finally had a serious look. Forth skips on most of the complexity of getting from bytes to semantics; even Lisp is complex in comparison; but is still powerful enough to do interesting things; and the best foundation for DSL'…

Forth is one of those things that always fascinates me but ultimately always ends up feeling too hard to read. Snabel looks like an interesting experiment in that regard..

I think any new language will seem hard to read if you've not made an initial effort to learn to parse it. I had this problem with C++ when I first made the leap from Pascal to C-derived languages.

I've not done anything original in Forth, only hacking some existing code on FreeBSD, but I found Forth to be remarkable quiet to pick up once I took time to familiarise myself with it's syntax.

Unfortunately that was a while ago now with so little time spent in the language at the time and none spent in it since that I've now forgotten most of what I had learned.

Re: How to Write Your Own Compiler (2009)

#17
post #6
post #3

One of the best resources I've seen on this is Capon and Jinks 'Compiler Engineering Using Pascal' (1988; ISBN 0-333-47155-5) if you can get hold of it http://www.cs.man.ac.uk/~pjj/book.html

Also Brinch Hansen on Pascal Compilers : https://www.amazon.com/Brinch-Hansen-Pascal-Compilers/dp/013...

Brinch Hansen's book is a classic. It contains all necessary code for a self-compiling Pascal compiler (If I recall correctly).

Re: How to Write Your Own Compiler (2009)

#18
I appreciate the effort to have a web page talking about writing your own compiler. Right now, I'm looking for an easy way to add define-use chains to a compiler. I know it involves breaking code into blocks and tracing through the code looking for use of the same variables. This tutorial is good because it adds a symbol table on each block level, which helps in differentiating names that are re-used in a block and don't refer to other variables of the same name. Does anyone know of code that makes clear what is involved in def-use for a variable without saying "this is an exercise for the reader" ?

Re: How to Write Your Own Compiler (2009)

#19
post #18

I appreciate the effort to have a web page talking about writing your own compiler. Right now, I'm looking for an easy way to add define-use chains to a compiler. I know it involves breaking code into blocks and tracing through the code looking for use of the same variables. This tutorial is good because it adds a symbol table on each block level, which helps in differentiating names that are re-used in a block and d…

> a symbol table on each block level, which helps in differentiating names

Yes, that's something that every compiler (for languages with scopes that allow hiding) needs. Typically you also never compare variables by name but by pointer to a symbol structure or some numeric ID.

> Does anyone know of code that makes clear what is involved in def-use for a variable without saying "this is an exercise for the reader" ?

It's not code, but chapter 2 of Nielson/Nielson/Hankin's Principles of Program Analysis discusses how to do this. See accompanying slides on the book's website: http://www.imm.dtu.dk/~hrni/PPA/ppasup2004.html

In a sense, this must be an exercise for the reader, since if you are working on your own compiler, nobody's code will be compatible with yours.

Re: How to Write Your Own Compiler (2009)

#20
post #18

I appreciate the effort to have a web page talking about writing your own compiler. Right now, I'm looking for an easy way to add define-use chains to a compiler. I know it involves breaking code into blocks and tracing through the code looking for use of the same variables. This tutorial is good because it adds a symbol table on each block level, which helps in differentiating names that are re-used in a block and d…

Go has several packages supporting program analysis in the standard library. https://golang.org/pkg/go/types/#Info populates separate maps for Defs and Uses. Check out the source for how they walk the SSA and calculate def-use chains.

A more convenient interface to go program analysis is https://godoc.org/golang.org/x/tools/go/loader. See https://blog.cloudflare.com/building-the-simplest-go-static-... for a basic tutorial on using the module.

Post reply on HN