如何定义编程语言的语法 [英] How to define a grammar for a programming language

查看:266
本文介绍了如何定义编程语言的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为要从头设计的新编程语言(命令式编程语言)定义语法(无上下文)。

How to define a grammar (context-free) for a new programming language (imperative programming language) that you want to design from scratch.

换句话说:

推荐答案

一次只能执行一次步骤。

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.

最后写入所有在EBNF和运行它ANTLR或类似的。

Finally write it all out in EBNF and run it through ANTLR or similar.

也最好不要重塑轮子。我通常通过选择序列来开始和结束语句块和函数,以及数学运算符,通常基本上是C样,ECMAScript样,基于类似,基于命令列表或基于XML。这有助于很多人这是人们习惯使用。

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.

我经常开始定义新的语言,只是为了找到别人已经实现了一个功能,只是坚持用C,ECMAScript或Basic

I've often started defining new language only to find someone else has already implemented a feature somewhere in some existing language.

如果你的目标是开发一些特定项目的速度,你可能会更好地在像Python,Lua或SpiderMonkey的原型,如果你'希望能够快速启动和运行,并希望减少大多数编译语言所需的打字量。

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.

这篇关于如何定义编程语言的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆