【问题标题】:reading the scala spec for expressions阅读 scala 规范中的表达式
【发布时间】:2015-09-10 17:17:19
【问题描述】:

我一定错过了here in the spec 定义的符号。例如,完全破译它的前两行会非常有帮助:

Expr         ::=  (Bindings | id | `_') `=>' Expr
               |  Expr1

我很不知道id 是在哪里定义的,以及这个符号的语法(以及解释)。

谢谢!

【问题讨论】:

    标签: scala scalac


    【解决方案1】:

    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]
    

    Bindingsin the context-free syntax 的下方定义,但在Chapter 2 中定义

    Scala 中的名称标识了统称为实体的类型、值、方法和类。名称由局部定义和声明、继承、导入子句或包子句引入,统称为绑定。

    但是它是什么意思

    非正式语言:

    Expr ::= (Bindings | id | `_') `=>' Expr
             |  Expr1
    

    可以写成:

    Expr 应被视为 (::=) 或 ((...)) Bindingsid 或文字下划线字符 ('_'),后跟等号和右-角括号字符 ('=>'),后跟 ExprExpr1

    即使在更多非正式的语言中,你也可以说:

    表达式 (Expr) 是任何有效的绑定语法(魔术 _ 或普通变量引用,一次或多次,可能带有类型归属)或变量引用,或者只是魔术 _ ,后跟一个箭头 (=>),然后是我们允许在该语言中使用的任何表达式

    【讨论】:

      【解决方案2】:

      我不确定非终端 id 是如何定义的,但希望我能帮助您理解这个符号。

      符号看起来像Backus-Naur Form。这意味着Expr 是以下之一:

      1. Bindings '=>' Expr
      2. id '=>' Expr
      3. '_' '=>' Expr
      4. Expr1

      ' 引号中的标记是字符串文字,而未引用的标记是非终结符,这意味着它们可以进一步扩展。

      所以,如果我有以下 sn-p 代码:

      x => x * x
      

      那么可能会被解析为如下内容:

          Expr
         / |  \
       id '=>' Expr
       |        |
      'x'     Expr1
                |
             BinaryOp
             /  |  \
         Expr1 '*' Expr1
           |        |
          id       id
           |        |
          'x'      'x'
      

      【讨论】:

        猜你喜欢
        • 2016-02-08
        • 1970-01-01
        • 2011-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        相关资源
        最近更新 更多