可以用在线解析EBNF
SourceCharacter ::= #x0009 | #x000A | #x000D | [#x0020-#xFFFF] // 大部分的 Unicode
Name ::= [_A-Za-z][_0-9A-Za-z]* // 标识符名称第一部分只能出现一次,后面部分零次或多次
StringCharacter ::= SourceCharacter - '"' // - '"' 代表不包含双引号 ", 即 StringCharacter 是 SourceCharacter 但不包含双引号. (String 要用双引号作为结束/闭合的标记)
Integer ::= [0-9]+
Number ::= Integer Ignored
String ::= '"' '"' Ignored | '"' StringCharacter '"' Ignored
Variable ::= "$" Name Ignored // 变量
Assignment ::= Variable Ignored '=' Ignored ( String | Number | Variable | BinaryExpression) Ignored
Print ::= "print" "(" Ignored Variable Ignored ")" Ignored
Statement ::= Print | Assignment
SourceCode ::= Statement+
Comment ::= Ignored "#" SourceCharacter // 注释
BinaryExpression::= (Variable | Number) Ignored Operator Ignored (Variable | Number)
Operator ::= "+" | "-" | "*" | "/" | ">" | "<" | "==" | ">=" | "<="
BinaryExpressions ::= (BinaryExpression Operator)+ Ignored (Variable | Number) // eg: 1: (2 + 1 +) 3 2: ((2 + 1 +) (5 + 6 -)) 3
FunctionDeclaration ::= "func" Ignored Name Ignored "(" Variable ("," Variable)* ")" BlockStatement // eg: 1: func foo ($a) {} 2: func foo ($a[,$b][,$c]) {} ("," Variable)*这部分是一个或多个
BlockStatement ::= "{" Ignored (IfStatement | CallFunction | Print | Assignment | ReturnStatement ) Ignored "}"
ReturnStatement ::= "return" (BinaryExpression | Variable)
CallFunction ::= Name "(" (Variable | Number) ("," (Variable | Number))* ")" Ignored
IfStatement ::= "if" Ignored "(" Variable Ignored Operator Ignored Variable ")" Ignored BlockStatement Ignored "else" Ignored BlockStatement Ignored
TypeScript implementation of pineapple language (https://github.com/karminski/pineapple) as a personal exercise.
pineapple lang 是一个简单的编程语言 demo. 它包含了个手写的递归下降解析器和一个简单的解释器.
该语言现在应该是图灵完备的.
pineapple 的主要目的是让编译原理初学者有一个预热, 简单了解一个编程语言是怎么构建的.
npm install
npm run test
最后把代码转成JavaScript的AST然后使用javascript的解释器canjs执行代码.
# 求斐波那契数
func Fibonacci($a) {
if ($a <= 2) {
return 1
}
$_a = $a - 1
$_b = $a - 2
$aa = Fibonacci($_a)
$bb = Fibonacci($_b)
return $aa + $bb
}
$res = Fibonacci(7)
print($res)