创建编程语言问题
我正在考虑创建一种基于 C 的自定义(小型)编程语言(语法)。 我不明白苹果是如何使用 [testClass runThis:true]; 等运算符实现 Objective-C 的用那个语法。
我如何为我的自定义 C 语言实现自定义语法
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在考虑创建一种基于 C 的自定义(小型)编程语言(语法)。 我不明白苹果是如何使用 [testClass runThis:true]; 等运算符实现 Objective-C 的用那个语法。
我如何为我的自定义 C 语言实现自定义语法
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
您可能需要查看 Clang(LLVM 编译器的 C、C++ 和 Obj-C 前端)和 Eero 编程语言,一种基于 Objective-C 并使用 LLVM 构建的新语言。这两个项目都是开源的。
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.
首先,您需要实现一个解析器和一个词法分析器。一种方法是从 K&R 的“C 编程语言”中获取 C 语法和文法,并将其用作 Flex 和 Bison 的输入来创建独立的解析器。这将解析语法上有效的 C 程序。之后,您可以根据需要对其进行调整和自定义。
然而,这是一个不平凡的项目,您必须准备好自学很多有关编译器的知识。祝你好运!
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!
[...]
中的位基于 Smalltalk。至于设计自己的语言,那是一个很大的课题。您需要熟悉上下文无关语法,词法分析,递归下降解析器,LALR解析器,我们甚至还没有解决生成的问题可执行代码还没有。不过这一切都很有趣。设计和实现可行的计算机语言是一项具有挑战性但(IMO)有益的工作。
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.
你必须编写一个编译器。如果您的语义合适,您也许可以直接编译为 C 代码;否则你还必须为你的语言的某些部分编写某种运行时,这些部分不容易直接转换为 C。不过,考虑到你正在谈论一种基于 C 的小型语言,这可能不是问题。
你打算怎样做呢?哦。男人。
关于 编写编译器。虽然编译器的任何一个部分都不是特别困难(至少对于幼稚的、非优化的编译器来说),但有很多东西需要以很多不同的方式来应对,通常是彼此微妙不兼容的方式。
至少你需要一个词法分析器(通常通过自动化工具,如 flex 或作为编译器套件的一部分,如 ANTLR),一个 解析器(通常通过自动化工具,例如 Bison 和前面提到的 ANTLR) 和 代码生成器(通常手动生成)。实际上,您可能希望在之间添加一个抽象语法树解析器和代码生成器为您(或其他人)提供实现各种 的空间优化并使代码生成更简单。
有关于这个主题有很多书籍,更不用说研究论文了。玩得开心。你想做的事情很有挑战性,但当你成功时就会得到回报。
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.
升级到 C++,您就有可能创建 DSEL(特定领域嵌入式语言)。我不知道您要解决什么问题,但设计良好的 DSEL 可能比设计您自己的语言和创建解析器、编译器等方便得多。获取本书的副本以获得一些见解:C++ 模板元编程
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