要开发像咖啡脚本这样的编程语言,我需要知道的初始要求是什么,它基本上有自己的语法,但在编译后会更改为另一种语言。我对此进行了谷歌搜索,但找不到正确的答案。
-
使用基本语言指定您的语言形式语法 http://en.wikipedia.org/wiki/Formal_grammar在类似的事情中EBNF http://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_Form.
statement = if-statement
| return-statement
| expression
| ...
if-statement = "if" "(" expression ")" "{" statements "}"
return-statement = "return" expression ";"
...
了解一下简单的parsing http://en.wikipedia.org/wiki/Parsing by 递归下降 http://en.wikipedia.org/wiki/Recursive_descent_parser and 运算符优先级 http://effbot.org/zone/simple-top-down-parsing.htm.
编写一个解析器来创建抽象语法树 http://en.wikipedia.org/wiki/Abstract_syntax_tree来自源文件。
Write a 代码生成器 http://en.wikipedia.org/wiki/Code_generation_%28compiler%29将此 AST 转换为您的目标语言;或者
写一个口译员 http://en.wikipedia.org/wiki/Interpreter_%28computing%29仅仅评估 AST。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)