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?
Ask HN: What's a starting point for learning how to write programming languages?
71–80 of 105 posts
Re: Ask HN: What's a starting point for learning how to write programming languages?
#72Earlier 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.
Re: Ask HN: What's a starting point for learning how to write programming languages?
#73Re: Ask HN: What's a starting point for learning how to write programming languages?
#74Earlier 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.
Re: Ask HN: What's a starting point for learning how to write programming languages?
#75The 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?
#76Another 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?
#77When 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?
#78This 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…
Re: Ask HN: What's a starting point for learning how to write programming languages?
#791. 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?
#80Put 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…