如何定义编程语言的语法
如何为您想要从头开始设计的新编程语言(命令式编程语言)定义语法(上下文无关)。
换句话说:当你想从头开始创建一种新的编程语言时,你该如何进行?
How to define a grammar (context-free) for a new programming language (imperative programming language) that you want to design from scratch.
In other words: How do you proceed when you want to create a new programming language from scratch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一次一步。
不认真的,从表达式和运算符开始,向上到语句,然后到函数/类等。保留一个标点符号用于什么的列表。
并行定义引用变量、数组、哈希、数字文字、字符串文字和其他内置文字的语法。同时定义数据命名模型和范围规则。
要检查您的语法是否有意义,请重点关注某个级别(文字/变量、运算符、表达式、语句、函数等),并确保散布或附加/前置的其他级别的标点符号和标记不会导致歧义。
最后将其全部写在 EBNF 中并通过 ANTLR 或类似的程序运行它。
也最好不要重新发明轮子。我通常首先选择开始和结束语句块和函数以及数学运算符的序列,这些序列通常基本上是类似 C、类似 ECMAScript、类似 Basic、基于命令列表或基于 XML 的。这很有帮助,因为这是人们习惯于使用的方式。
当然,你必须想出一个非常令人信服的理由,不放弃编写一门新语言,而坚持使用经过良好测试和广泛使用的 C、ECMAScript 或 Basic。
我经常开始定义新语言,却发现其他人已经在某种现有语言的某个地方实现了某个功能。
如果您的目标是某些特定项目的开发速度,并且希望快速启动和运行,并希望减少大多数编译语言中所需的输入量,那么您最好使用 Python、Lua 或 SpiderMonkey 等工具进行原型设计。
One step at a time.
No seriously, start with expressions and operators, work upwards to statements, then to functions/classes etc. Keep a list of what punctuation is used for what.
In parallel define syntax for referring to variables, arrays, hashes, number literals, string literals, other builtin literal. Also in parallel define your data naming model and scoping rules.
To check whether your grammar makes sense focus on a level (literal/variable, operator, expression, statement, function etc) and make sure that punctuation and tokens from other levels interspersed or appended/prepended is not gonna cause an ambiguity.
Finally write it all out in EBNF and run it through ANTLR or similar.
Also best not to reinvent the wheel. I normally start off by choosing sequences to start and end statement blocks and functions, and mathematical operators, that are usually fundamentally C-like, ECMAScript-like, Basic-like, command-list based or XML-based. This helps a lot cos this is what people are used to working with.
Of course you have to come up with a pretty compelling reason not to abandon writing a new language and just stick with C, ECMAScript, or Basic which are well tested and much used.
I've often started defining new language only to find someone else has already implemented a feature somewhere in some existing language.
If your goal is speed of development for some specific project, you might be better off prototyping in something like Python, Lua or SpiderMonkey if you're looking to get up and running quickly and want to reduce the amount of typing necessary in most compiled languages.
您需要查看 EBNF(扩展巴科斯-诺尔范式)。
(假设您想编写上下文无关语法。)
You'll want to have a look at EBNF (Extended Backus-Naur Form).
(Assuming you want to write a context free grammar, that is.)
如果您的意思是定义语法,那么最好从现有语言开始并修改其语法以匹配您所追求的内容。创建语法规范是一项相当机械的练习,需要使用您自己头脑中的一组模式。例如,if 语句是什么样的?它看起来像 C
if <- if(exp) block
if <- if(exp) block else block2
还是像 ML?
if <- if exp then block else block end
或者也许你想使用类似 Lua 的 elseif:
if <- if exp then exp end
if <- if exp then exp end
if <- if exp then exp end
- if exp then exp (elseif exp)* else exp end语法和语义将这些决策编码化。请注意,这些都不太适合在 LALR 或 LL(*) 编译器生成器中实现,并且必须进行调整才能实现,因为它们是不明确的。
Michael Scott 的《编程语言语用学》很好地介绍了编程语言的设计。可在亚马逊此处购买
If you mean defining a grammar, you would be best served by starting with an existing language and modifying its grammar to match what it is that you are after. Creating a grammar specification is a fairly mechanical exercise, using a set of patterns in your own head. For instance, what does an if statement look like? Does it look like C
if <- if(exp) block
if <- if(exp) block else block2
or like ML?
if <- if exp then block else block end
or maybe you want to use elseifs like Lua:
if <- if exp then exp end
if <- if exp then exp (elseif exp)* else exp end
The grammar and semantics codify these decisions. Note that none of these are quite suitable for implementation in a LALR or LL(*) compiler generator yet, and would have to be massaged for implementation because they are ambiguous.
Programming Language Pragmatics by Michael Scott is a good introduction to the design of programming languages. It's available on Amazon here
看看 Bison,也许这就是您正在寻找的东西?
Have a look at Bison, maybe that's what you are looking for?
在开始设计一种编程语言之前,您需要了解很多有关编程语言的知识。我推荐Shriram Krishnamurthi 的《编程语言:应用与解释》。
You'll need to know quite a lot about programming languages before you start designing one. I recommend Programming Languages: Application and Interpretation by Shriram Krishnamurthi.