Creating programming language question
I'm thinking about creating a custom (small) programming language (syntax) based on C. I don't understand how apple implemented obj开发者_高级运维ective-c with operators like [testClass runThis:true]; with that syntax.
How would i implement a custom syntax for my custom c language
You might want to look at Clang (the C, C++ and Obj-C frontend of the LLVM compiler) and the Eero Programming Language, a new language based on Objective-C that was built with LLVM. Both projects are open-source.
You'd need to implement a parser and a lexer to start with. One way to go about it is to get the C syntax and grammar from K&R's "The C programming language" and use that as input to Flex and Bison to create a standalone parser. That will parse syntactically valid C programs. After that, you can tweak it as you want and customise it.
This is however a non trivial project and you'll have to be prepared to teach yourself a lot about compilers. Good luck!
The bit inside [...]
is based on the syntax of Smalltalk.
As for designing your own language, that is quite a big subject. You need to acquaint yourself with context free grammars, lexical analysis, recursive descent parsers, LALR parsers, and we haven't even got to the problem of generating executable code yet. Still it's all good fun. Designing and implementing workable computer languages is a challenging but (IMO) rewarding exercise.
You'll have to write a compiler. If your semantics fit, you might be able to compile directly to C code; otherwise you'll have to also write some kind of runtime for the bits of your language which don't easily and directly translate to C. Given that you're talking about a small C-based language, though, that's probably not an issue.
How do you go about doing this? Oh. Man.
There are dozens of books, articles, tutorials, etc. on the subject of writing a compiler. While no individual piece of a compiler is particularly difficult (at least for naive, non-optimizing compilers), there is a whole lot of stuff to contend with in a whole lot of different ways, often ways which are subtly incompatible with each other.
At the very least you'll want a lexer (usually through an automated tool like flex or as part of a compiler suite like ANTLR), a parser (usually through automated tools like Bison and the aforementioned ANTLR) and a code generator (usually generated by hand). In reality you'll probably want to stick an abstract syntax tree between the parser and the code generator to give you (or someone else) room for implementing various optimizations and for making code generation simpler.
There are lots of books available on this topic, not to mention research papers. Have fun. What you're trying to do is challenging but rewarding when you pull it off.
Step up to C++ and you have the possibility to create a DSEL (Domain-Specific Embedded Language). I don't know what problem you're trying to solve but a well designed DSEL might be much more convenient than designing your own language and creating parsers, compilers, etc. Grab a copy of this book for some insight: C++ Template Metaprogramming
精彩评论