Live data from Hacker News

Ask HN: What's a starting point for learning how to write programming languages?

news.ycombinator.com

61–70 of 105 posts

Re: Ask HN: What's a starting point for learning how to write programming languages?

#61
post #59

Put that flex & bison book away. Don't think about compilers. Design your own litte interpreted language. Write a lexer by hand (not that hard). Write a recursive descent parser by hand. It's a bit tricky to get infix operator parsing stuff right, but an important learning experience. Once you got the basic parsing algorithm right you can probably add lot's of fun stuff to your language just with general programming…

There is a really simple algorithm to convert infix to prefix or postfix. The pre/postfix is then interpreted/compiled easily. This was one of the first things my comp-sci teacher in HS made us do after teaching linked list/stacks/fifos.

Of course if you pick a language like forth (mentioned below) the whole problem is sort of moot.

Re: Ask HN: What's a starting point for learning how to write programming languages?

#62

There is much good advice given by others here regarding the implementation of languages. However, if you really want to get into language design, there is one area that you should/need to understand (as far as I am concerned) and that is how to specify the meaning of your language and its constructs. If this is not done, you end up with too many gotchas and special cases in how you will do things. I have found too m…

> that is how to specify the meaning of your language and its constructs You pose the question but don't seem to answer it - this area is called semantics , and there are several approaches and notations you can use. Types and Programming Languages by Pierce is the most common starting point.

I am not posing a question. I am suggesting he look into the subject. The word "semantics" doesn't hold much meaning for many people, they understand the word "meaning" better. I could have dived straight in and suggested that he look at "denotational semantics", but I considered that just a little too much to start him off with.

By the way, how much are you doing on Katadhin these day?

Re: Ask HN: What's a starting point for learning how to write programming languages?

#63
post #54

The book "Lisp In Small Pieces" takes you threw the journey of creating a few compilers of increasing difficulty, from a simple language's interpreter using a complex language to a complex language's compiler using a simple language. By using Lisp-y language you can ignore the tedious and uninteresting task of parsing for the real meat of compiling and (some) optimizing.

Love lisp in small pieces! Beautiful book. Also enjoyed Essentials of Programming Languages by Friedman and Wand -- very SICPish into to programming language implementation in that it weaves together this narrative with uniting underlying themes rather than bombarding the reader with seemingly disparate concepts and leaving it an exercise to unite them as many "classic" texts in this discipline do

Re: Ask HN: What's a starting point for learning how to write programming languages?

#64

Although its still in development, http://craftinginterpreters.com/ (previously on HN https://news.ycombinator.com/item?id=13406081 ) seems promising, and the existing content is good.

Crafting interpreters is a great place to start! Takes you from basically nothing to implementing a whole language surprisingly quickly.

Re: Ask HN: What's a starting point for learning how to write programming languages?

#65
> a "simple" language like SQL, Markdown

If you're happy to consider Markdown a language (which it is, just not a programming language), you could also consider a (subset of) XML parser, or JSON parser. It's a nice place to start, because it's very small scope, a subset of PL (parsing), and the completed thing is actually usable and useful.

Plus, it gets you a step on the path to a programming language - arguably, the fundamental step.

I recommend a recursive descent parser, and this dragon compiler book (https://en.m.wikipedia.org/wiki/Principles_of_Compiler_Desig... Your local uni library has it.). An early chapter gives a complete calculator (arithmetic parser), in very simple and clear C code.

NB the most recent dragon book uses horrific over-complex architecture-astronaut, design-patterns OO java code. OOP has its place, but not like this. Not like this.

Re: Ask HN: What's a starting point for learning how to write programming languages?

#66

Beautiful Racket[0] is a good starting point that covers a lot of nice small examples that incrementally build up your understanding of the different components involved in making languages. And it uses Racket[1], which incorporates the idea of crafting many small "domain-specific" languages to solve different problems into its entire philosophy[2]. [0] https://beautifulracket.com/ [1] https://racket-lang.org/ [2] ht…

I'm just finishing up the last tutorial, and I highly recommend this as well. I made one traditional C compiler back in college with flex and bison, which I consider an invaluable experience, but I've always wanted to tap in to the power of Scheme for developing new languages. I had some false starts on my own and with mediocre tutorials, but in the end Beautiful Racket was exactly the kind of pedagogy I needed.

Re: Ask HN: What's a starting point for learning how to write programming languages?

#68
This is going to sound "out of left field" perhaps, but use Prolog.

I've been exploring exactly this, compiling with Prolog, this past month or so and I'm simply blown away. It's so simple, elegant, and easy. It will be faster and easier for you to learn the basics of Prolog and then learn how to write compilers using it, than to learn to write compilers in another language (even a language you already know.) Prolog is an extremely simple language despite its incredible power. It's very different from imperative/functional languages so it might take a minute to wrap your head around it, but then you're operating on a higher plane.

Read "Logic Programming and Compiler Writing " by David Warren http://sovietov.com/tmp/warren1980.pdf But beware: it's dated. Or, if you can get a copy of "The Art of Prolog" you can read Chapter 24, which covers the same concepts but has more modern code. Specifically, you're going to want to use Definite Clause Grammars (DCG):

https://en.wikipedia.org/wiki/Definite_clause_grammar http://www.pathwayslms.com/swipltuts/dcg/index.html https://www.metalevel.at/prolog/dcg http://www.amzi.com/manuals/amzi/pro/ref_dcg.htm

Study "Toy compiler in Prolog" http://faculty.cooper.edu/smyth/cs225/ch7/prolog.htm And maybe this: https://github.com/fusiongyro/dailyprogrammer/tree/master/20...

I can vouch for this. I wrote a parser for Oberon in two days. I wrote an assembler for the RISC CPU used in Project Oberon in another two days. Both of them are no longer than two pages and the code is essentially just grammar rules. I haven't (yet) written the bit in-between that translates the AST from the parser into assembly code for the chip, but only because I have other fish to fry. But I can see how to do it and it would be very easy. The compiler is described in the Project Oberon book if you want to attempt that language.

I'm implementing a compiler for Joy on top of the assembler and it's been like a dream. Because the code is so simple, and the editor I'm using has an extension that uses the Prolog interpreter to lint as I go, it's actually hard to introduce bugs. I write the asm code then abstract patterns into the compiler to refactor and (lightly) abstract the raw asm into what are effectively AST nodes. But there is no language above this. I think I have to write a blog post called "Compiler for No Language" or something. Eventually, I am going to abstract my way to "atomic" nodes that represent Joy functions and then I'll have a Joy compiler. But in the meantime, I'm floating around in a semantic sea between parsing and assembly, and it's warm and tropical and you would not believe the fish I've seen.

I have to conclude that anyone who is writing any sort of compiler and NOT using Prolog is working way too hard.

I've been programming in Python for twenty years, happy as a clam, and now you couldn't pay me enough to NOT use Prolog.

"These are the words of VAL. They are true. Hear them."

Re: Ask HN: What's a starting point for learning how to write programming languages?

#69
post #37

There's more than a few ways to do it. Ohm [0] is a good place to start. It's a parser generator. If you can understand the math example [1], you can probably realize how things get built upwards. Then you can focus on what you want your language to do, how you'll interpret it, what problems it would express, &c. [0] https://ohmlang.github.io/ [1] https://github.com/harc/ohm/blob/master/examples/math/index.... Also c…

Ohm’s web playground is an awesome way to learn how to build up a language from scratch. I’m in the process of porting Postscript to it and it’s amazingly easy.

FWIW Prolog DCGs (Definite Clause Grammar) are very similar to Ohm/OMeta.

Re: Ask HN: What's a starting point for learning how to write programming languages?

#70
post #60

Write a parser for a complicated file format. Write a simple interpreter for a C-like language by parsing it into a syntax tree, constructing a stack of symbol tables, and evaluating the nodes. Once that works, write a version that emits code for a stack-based virtual machine (a simple stack machine written concisely might eat just a few screenfuls of code). When that works, symbolically evaluate the stack bytecode i…

What's the purpose of the file format bit?
Post reply on HN