Live data from Hacker News

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

news.ycombinator.com

71–80 of 105 posts

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

#71
post #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?

Declarative thingies are easier to parse? You can skip a lot of these steps, I guess --- or just stop at a working interpreter, which will get you as far as MRI Ruby got before RoR took off.

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

#72
post #71
post #70

Earlier quoted context omitted.

What's the purpose of the file format bit?

Declarative thingies are easier to parse? You can skip a lot of these steps, I guess --- or just stop at a working interpreter, which will get you as far as MRI Ruby got before RoR took off.

Oh, I see. I thought it was something like 'writing an ad-hoc parser for something convoluted will make you understand and appreciate systematic approaches to parsing better' but I guess that's just me being a terrible person.

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

#73
For my capstone class for my CS BS, we had to write a compiler that compiled to machine code that the virtual machines we wrote could run. I can't remember the name of the book, but we called it "The Dragon Book." Looking in the other comments, it looks like someone already linked it.

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

#74
post #72
post #71

Earlier quoted context omitted.

Declarative thingies are easier to parse? You can skip a lot of these steps, I guess --- or just stop at a working interpreter, which will get you as far as MRI Ruby got before RoR took off.

Oh, I see. I thought it was something like 'writing an ad-hoc parser for something convoluted will make you understand and appreciate systematic approaches to parsing better' but I guess that's just me being a terrible person.

I pretty much just use yaccs.

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

#75
Write a DSL. This is really easy in languages like Ruby that don't need parentheses around function parameters. You can write a Ruby DSL in a few hours and expand on syntax, features as you like.

The next step is to actually parse code with an abstract syntax tree. The easy path here is actually javascript. There are many transpilers for JS so things like bluebird/harmony can work.

Next step is to do thing "the right way"(tm) with yak/bison and build a compilable language. For that, I suggest you read a few books on languages and understand concepts such as backus-naur form before continuing.

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

#76
You're right that writing a complete language like Java or Python is not a small task, but that's because these languages have redundant ways of doing the same thing. If you simplify them be eliminating redundancy while retaining power, you'll have a much simpler implementation task. Here's a language I designed for this: https://kartick-log.blogspot.com/2016/04/why-arent-there-sim... If you want to learn language implementation rather than design, you can implement this language as is.

Another option is to build a VM, that accepts as input byte in a text (not binary) form. Less efficient, but easier to implement, and you can write your bytecode by hand instead of also building a compiler to do so. Like object-oriented assembly language, if you will. If you're interested, I'll scan and share my dissertation report with you.

You can also implement a domain-specific language. Did you, in the past, feel that a task you were doing would be simpler if there was a custom language for it? If so, maybe you can try building that DSL and then using that to make your main task easier.

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

#77
I spent 3 years writing como-lang https://github.com/analang/ana I didn’t have a compilers course at my college. I think the first thing you should understand is how a language works. The first part is the syntax and grammar. It would also be very useful to know how assembly works, as than you’re probably keen on stack machines, which are very simple, yet powerful machines capable of representing expressions in a linear way. Feel free to email me if you have any questions! I’d love to help you get on the right track.

When I was learning, my only resources were books, google, and PHP-src and CPython.

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

#78

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…

In fact, it would actually be easier to implement Prolog then implement a compiler in it, than to implement a compiler directly in e.g. C, Java, Lisp, etc.

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

#79
In phases, this is roughly how I learned:

1. Cute hacks like using the eval function in Python and rewriting the string before passing it in. This is basically metaprogramming by macro-processing and a good stepping stone to thinking about what a programming language really accomplishes.

2. Writing various Forth-like and Lisp-like languages. This let me explore a working system without spending too much time on parsing - I could try interpreting the AST directly or compiling it to an intermediate language.

3. Gradually learned that a lot of the features and technologies in general purpose programming languages aren't things I want to care about(how to implement arithmetic, perform variable assignment, perform typechecking etc.). Focused on data languages like JSON and small supplemental languages for e.g. scheduling concurrency.

4. Found a useful model for prototyping semantics before adding a syntax, by envisioning the whole language in the form of a state machine that builds expressions one API call at a time, then compiles/executes the result with a "commit" call or similar. This is a practical technique for many domain specific problems, and makes the necessary syntax emerge naturally.

A lot of what goes into making a useful production language isn't really on the theoretical side, but on finish-and-polish stuff: fast builds, helpful docs and error messages, well-rounded libraries, debugging tools, ease of deployment and so on. Learning this through the process of writing small PLs helped change how I viewed language selection to be more like picking any other application, vs a point of religion: choosing something with approximately the right features and tooling so that the groundwork is easy, and then magnifying that leverage by adding a bit of custom syntax, static checking, code generation, what-have-you on top. Turning the tech into an airtight abstraction generalized across a whole language is a ton of work, but getting 80% of it in for the specific app takes a fraction of the time in comparison.

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

#80
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…

The first couple chapters of crafting interpreters (free) cover lexing and parsing and precedence well and it's very readable and practical rather than theoretical.
Post reply on HN