Monday, May 24, 2010

How can I make a new programming language?

How should I go about learning to make my own programming language? What is a compiler?

How can I make a new programming language?
First, learn a few different languages yourself. I would start with basic or Visual Basic, then learn Java and Javascript.


Then learn C and C++. Then learn VB.NET, VC++.net, VC#.net, and a few scripting languages such as perl, php, shell scripts, SQL, ...





After you have a good idea of what programming languages are out there, and the features that you like and dislike about the different languages, you can then consider how you would design your own language.





You may find that you don't want to write your own language. You may just want to extend an existing one, or write classes, functions and libraries in one and use those functions like language extensions.





If you want to start from scratch, I would look up a book called "Threaded Interpreted Languages" written in the late 70s or early 80s. It describes writing very small language constructs and routines and using linked lists, arrays of linked lists, and arrays of subroutine addresses to build the language and eventually the programs.
Reply:It is not easy. You need first to learn how to use a parser engine, such as antlr (http://www.antlr.org/) or javacc. This parses the source code.





Then the parser converts the source tokens into an abstract tree representation of the syntax (AST).





You then can use the Visitor Pattern to loop over the items in this tree and do transformations to the code. You can check that the syntax is valid, for example, and set the correct types for variables.





Finally, you need to emit the binary or bytecode. For this you can use something like asm for java vm bytecode (http://asm.objectweb.org/) or Cecil for .NET's clr bytecode (http://www.mono-project.com/Cecil).





The easiest way to learn is to look at existing open source compilers such as:


http://groovy.codehaus.org/


http://boo.codehaus.org/


http://www.scala-lang.org/





and lists where compiler writers hang out like:


http://groups.google.com/group/jvm-langu...
Reply:Nothing should stop you from designing your own programming language.





Designing a language that you can parse requires a bit more skill. But writing parsers is not so difficult.





Is it an object oriented language? Early binding (C# or Java) or late binding (Smalltalk).





Code generation is a different matter. Here you have to think carefully about whether you want to generate machine (x86) instructions or you want to cross-compile into another language (C, Java). At this point, you will remove ambiguities from the language that you encounter.You must also decide on runtime libraries for things such as sin() or log(). Do you include string operations?





The compiler should also produce useful error messages so that the programmer will be able to correct his/her syntax errors. This can be surprisingly tricky to do well. Your run-time system should also be informative about what went wrong.





Because interpreters are easier to write than scanners/parsers/code generators, interpreted languages are popular (Perl, php, python). In fact, starting with an interpreter, perhaps adding functions is a good start.





Obviously there are lots of good compiler books around. The "dragon book" (Aho, Hopcroft, Ullmann) was one of the first ones that made compilers somewhat more accesible. But the books improved over the years.





Some serious computer scientists think that writing a compiler is a real challenge and that someone who is able to do that will benefit enourmously from it: learning about grammars, parse trees, understanding code generation and code optimization.Oh yes, and to answer your question: a compiler is a scanner (of the input program, recognizing language elements or "tokens"), a parser that groups language constructs, expressions, statements into units that can be dealt with, and the code generator.





Have fun.

flower shops

No comments:

Post a Comment