【发布时间】:2015-09-10 17:17:19
【问题描述】:
我一定错过了here in the spec 定义的符号。例如,完全破译它的前两行会非常有帮助:
Expr ::= (Bindings | id | `_') `=>' Expr
| Expr1
我很不知道id 是在哪里定义的,以及这个符号的语法(以及解释)。
谢谢!
【问题讨论】:
我一定错过了here in the spec 定义的符号。例如,完全破译它的前两行会非常有帮助:
Expr ::= (Bindings | id | `_') `=>' Expr
| Expr1
我很不知道id 是在哪里定义的,以及这个符号的语法(以及解释)。
谢谢!
【问题讨论】:
id 来自the lexical grammar。简而言之就是:
upper ::= ‘A’ | … | ‘Z’ | ‘$’ | ‘_’ // and Unicode category Lu
lower ::= ‘a’ | … | ‘z’ // and Unicode category Ll
letter ::= upper | lower // and Unicode categories Lo, Lt, Nl
digit ::= ‘0’ | … | ‘9’
opchar ::= // printableChar not matched by (whiteSpace | upper | lower |
// letter | digit | paren | delim | opchar | Unicode_Sm | Unicode_So)
op ::= opchar {opchar}
varid ::= lower idrest
plainid ::= upper idrest
| varid
| op
id ::= plainid
| ‘`’ stringLiteral ‘`’
idrest ::= {letter | digit} [‘_’ op]
Bindings 在in the context-free syntax 的下方定义,但在Chapter 2 中定义
Scala 中的名称标识了统称为实体的类型、值、方法和类。名称由局部定义和声明、继承、导入子句或包子句引入,统称为绑定。
非正式语言:
Expr ::= (Bindings | id | `_') `=>' Expr
| Expr1
可以写成:
Expr应被视为 (::=) 或 ((...))Bindings、id或文字下划线字符 ('_'),后跟等号和右-角括号字符 ('=>'),后跟Expr或Expr1。
即使在更多非正式的语言中,你也可以说:
表达式 (
Expr) 是任何有效的绑定语法(魔术_或普通变量引用,一次或多次,可能带有类型归属)或变量引用,或者只是魔术_,后跟一个箭头 (=>),然后是我们允许在该语言中使用的任何表达式
【讨论】:
我不确定非终端 id 是如何定义的,但希望我能帮助您理解这个符号。
符号看起来像Backus-Naur Form。这意味着Expr 是以下之一:
Bindings '=>' Exprid '=>' Expr'_' '=>' ExprExpr1' 引号中的标记是字符串文字,而未引用的标记是非终结符,这意味着它们可以进一步扩展。
所以,如果我有以下 sn-p 代码:
x => x * x
那么可能会被解析为如下内容:
Expr
/ | \
id '=>' Expr
| |
'x' Expr1
|
BinaryOp
/ | \
Expr1 '*' Expr1
| |
id id
| |
'x' 'x'
【讨论】: