In my ongoing effort to quench my undying thirst for more programming knowledge I have come up with the idea of attempting to write a (at least for now) simple programming language that compiles into bytecode. The problem is I don't know the first thing about language design. Does anyone have any advice on a methodology to build a parser and what the basic features every language should have? What reading would you recommend for language design? How high level should I be shooting for? Is it unrealistic to hope to be able to include a feature to allow one to inline bytecode in a way similar to gcc allowing inline assembler? Seeing I primarily code in C and Java which would be better for compiler writing?
-
Dupe of stackoverflow.com/questions/479013/… among many others. Also, you are asking too many questions - one at a time is a good rule.anon– anon2009-07-30 18:18:06 +00:00Commented Jul 30, 2009 at 18:18
-
1And this stackoverflow.com/questions/1669/learning-to-write-a-compiler is the definitive SO answer on the subject area.anon– anon2009-07-30 18:19:57 +00:00Commented Jul 30, 2009 at 18:19
-
ok I'm sorry I didn't see it was a duplicate should it just be closed as a duplicate or should I delete the question?faceless1_14– faceless1_142009-07-30 18:23:23 +00:00Commented Jul 30, 2009 at 18:23
-
possible duplicate of creating-your-own-languagenawfal– nawfal2014-07-21 12:48:30 +00:00Commented Jul 21, 2014 at 12:48
3 Answers
There are so many ways...
You could look into stack languages and Forth. It's not very useful when it comes to designing other languages, but it's something that can be done very quickly.
You could look into functional languages. Most of them are based on a few simple concepts, and have simple parsing. And, yet, they are very powerful.
And, then, the traditional languages. They are the hardest. You'll need to learn about lexical analysers, parsers, LALR grammars, LL grammars, EBNF and regular languages just to get past the parsing.
Targeting a bytecode is not just a good idea – doing otherwise is just insane, and mostly useless, in a learning exercise.
Do yourself a favour, and look up books and tutorials about compilers.
Either C or Java will do. Java probably has an advantage, as object orientation is a good match for this type of task. My personal recommendation is Scala. It's a good language to do this type of thing, and it will teach you interesting things about language design along the way.
4 Comments
You might want to read a book on compilers first.
For really understanding what's going on, you'll likely want to write your code in C.
Java wouldn't be a bad choice if you wanted to write an interpreted language, such as Jython. But since it sounds like you want to compile down to machine code, it might be easier in C.
Comments
I recommend reading the following books:
This will give you tools and techniques for creating parsers, lexers, and compilers for custom languages.