3 ms·
I haven't made my own programming language yet, but I am working on a from-scratch C compiler (http://recc.robertelder.org/ http://recc.robertelder.org/), so I'
by robertelder 10y ago
I haven't made my own programming language yet, but I am working on a from-scratch C compiler (http://recc.robertelder.org/ http://recc.robertelder.org/), so I'll give you a few ideas:
1) You'll probably want to start by thinking about what the programming language will do, and what the grammar (https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_F...) of the language will look like. I would recommend starting by writing an LL grammar (https://en.wikipedia.org/wiki/LL_grammar https://en.wikipedia.org/wiki/LL_grammar), so you can write a recursive descent parser for it (https://en.wikipedia.org/wiki/Recursive_descent_parser https://en.wikipedia.org/wiki/Recursive_descent_parser). You will also need to be careful to not introduce indirect, or direct left recursion into your grammar (https://en.wikipedia.org/wiki/Left_recursion https://en.wikipedia.org/wiki/Left_recursion).
2) The first step will naturally lead you to need to consider tokenization: https://en.wikipedia.org/wiki/Tokenization_(lexical_analysis) https://en.wikipedia.org/wiki/Tokenization_(lexical_analysis...
3) Some caveats of step 1 include the dangling else problem (https://en.wikipedia.org/wiki/Dangling_else https://en.wikipedia.org/wiki/Dangling_else) and other grammar ambiguities (https://en.wikipedia.org/wiki/Ambiguous_grammar https://en.wikipedia.org/wiki/Ambiguous_grammar). A recursive descent parser will likely need to do backtracking so you'll want to think about how you can also backtrack any internal state that gets build up as the parser does its thing.
4) Once you can create a full parse tree and can traverse it, you can consider code generation. For un-optimized code, this is probably the easiest part, but once you start to considering possible optimizations, you'll probably want to write a 'back-end' and you could probably spend the rest of your life creating new optimizations.
5) Of course, this all gets more complicated if you want to do it differently with an LR grammar or if you want an interpreted language. You can also think about things like just in time compilation, etc.