Example5.5

「Example5.5」の編集履歴(バックアップ)一覧に戻る

Example5.5 - (2008/04/20 (日) 02:28:19) のソース

** 5.5 Language Elements Seen So Far

Chapters 4 and 5 have covered Scala’s language elements to express expressions and types comprising of primitive data and functions. The context-free syntax of these language elements is given below in extended Backus-Naur form, where ‘|’ denotes alternatives, [...] denotes option (0 or 1 occurrence), and {...} denotes repetition (0 or more occurrences). 

第 4 章と第 5 章はプリミティブなデータと関数の式と型を表現する Scala の構文をカバーしています。これらの文脈自由文法が下記に拡張 Backus-Naur 記法で与えられています。'|' は選択を、[...] はオプション (0 あるいは 1 回の出現)、{...} は繰り返し (0 回以上の出現)。

*** Characters:

*** 文字:

Scala programs are sequences of (Unicode) characters. We distinguish the following character sets:

Scala プログラムは (Unicode) 文字列です。下記の文字セットを区別します。

- whitespace, such as ‘’, tabulator, or newline characters, 
- letters ‘a’ to ‘z’, ‘A’ to ‘Z’, 
- digits ‘0’ to ‘9’, 
- the delimiter characters 
 .  ,  ;  (  )  {  }  [  ]  \  "  ’ 
- operator characters, such as ‘#’ ‘+’, ‘:’. Essentially, these are printable characters which are in none of the character sets above. 

- 空白文字:例えば ' '、タブ、改行文字
- 文字:'a'-'z', 'A'-'Z'
- 数字:'0'-'9'
- 区切り文字:
 .  ,  ;  (  )  {  }  [  ]  \  "  ’ 
- 演算子文字:例えば '#' '+' ':'。本質的には上記のどれにも含まれない印刷可能な文字。

*** Lexemes: 

*** 語彙素:

 ident = letter {letter | digit} 
          | operator { operator } 
          | ident ’_’ ident 
 literal = “as in Java” 

Literals are as in Java. They define numbers, characters, strings, or boolean values. Examples of literals as 0, 1.0e10, ’x’, "he said "hi!"", or true.

リテラルは Java と同様です。数、文字、文字列、ブール値を定義します。リテラルの例は 0, 1.e10, 'x', "he said "hi"", true です。

Identifiers can be of two forms. They either start with a letter, which is followed by a (possibly empty) sequence of letters or symbols, or they start with an operator character, which is followed by a (possibly empty) sequence of operator characters. Both forms of identifiers may contain underscore characters ‘_’. Furthermore, an underscore character may be followed by either sort of identifier. Hence, the following are all legal identifiers: 

識別子は2つの形式があります。文字で始まり文字またはシンボルの列 (空を許す) が続くものか、演算子文字で始まり演算子文字の列 (空を許す) が続くものです。どちらの形式もアンダースコア '_' を含んで構いません。さらにアンダースコア文字列の後にはどちらの形式の識別子も続いて構いません。従って、下記は全て正しい識別子です。

 x   Room10a   +   --   foldl_:   +_vector 

It follows from this rule that subsequent operator-identifiers need to be separated by whitespace. For instance, the input x+-y is parsed as the three token sequence x, +-, y. If we want to express the sum of x with the negated value of y, we need to add at least one space, e.g. x+ -y. 

この規則から、後に続く演算子識別子は空白文字で区切られねばならないということが判ります。例えば入力 x+-y は三つのトークン列 x, +-, y へと構文解析されます。もし x と、 y の符号を逆にした値との和を表現したいならば x+ -y と最低一つの空白を加える必要があります。

The $ character is reserved for compiler-generated identifiers; it should not be used in source programs. 

文字 $ はコンパイラ生成識別子の為に予約されています。ソースプログラムで使用すべきでありません。

The following are reserved words, they may not be used as identifiers: 

以下は予約語です。識別子としては使用出来ません。

 abstract case catch class def 
 do else extends false final 
 finally for if implicit import 
 match new null object override 
 package private protected requires return 
 sealed super this throw trait 
 try true type val var 
 while with yield 
 _    :    =    =>    <-    <:    <%    >:    #    @ 

*** Types: 

*** 型

 Type              = SimpleType | FunctionType 
 FunctionType = SimpleType ’=>’ Type | ’(’ [Types] ’)’ ’=>’ Type 
 SimpleType    = Byte | Short | Char | Int | Long | Float | Double | Boolean | Unit | String 
 Types            = Type {‘,’ Type} 

Types can be: 
- number types Byte, Short, Char, Int, Long, Float and Double (these are as in Java), 
- the type Boolean with values true and false, 
- the type Unit with the only value (), 
- the type String, 
- function types such as (Int, Int) => Int or String => Int => String.

型は
- 数値型 Byte, Short, Char, Int, Long, Float, Doble (Java と同様)
- Boolean 型とその値の true と false
- Unit 型とその唯一の値 ()
- String 型
- 関数型、例えば (Int, Int) => Int や String => Int => String

*** Expressions: 

*** 式

 Expr               = InfixExpr | FunctionExpr | if ’(’ Expr ’)’ Expr else Expr 
 InfixExpr        = PrefixExpr | InfixExpr Operator InfixExpr 
 Operator        = ident 
 PrefixExpr      = [’+’ | ’-’ | ’!’ | ’~’ ] SimpleExpr 
 SimpleExpr    = ident | literal | SimpleExpr ’.’ ident | Block 
 FunctionExpr = (Bindings | Id) ’=>’ Expr 
 Bindings        = ‘(’ Binding {‘,’ Binding} ‘)’ 
 Binding          = ident [’:’ Type] 
 Block             = ’{’ {Def ’;’} Expr ’}’ 

Expressions can be: 
- identifiers such as x, isGoodEnough, *, or +-, 
- literals, such as 0, 1.0, or "abc", 
- field and method selections, such as System.out.println, 
- function applications, such as sqrt(x), 
- operator applications, such as -x or y + x, 
- conditionals, such as if (x < 0) -x else x, 
- blocks, such as { val x = abs(y) ; x * 2 }, 
- anonymous functions, such as x => x + 1 or (x: Int, y: Int) => x + y. 

式は
- 識別子、例えば x, isGoodEnugh, *, +-
- リテラル、例えば 0, 1.0, "abc"
- フィールドとメソッド選択、例えば System.out.println
- 関数適用、例えば sqrt(x)
- 演算子適用、例えば -x, y + x
- 条件式、例えば if (x < 0) -x else x
- ブロック、例えば { val x = abs(y); x * 2}
- 無名関数、例えば x => x + 1, (x: Int, y: Int) = x + y

*** Definitions: 

*** 定義

 Def            = FunDef | ValDef 
 FunDef       = ’def’ ident {’(’ [Parameters] ’)’} [’:’ Type] ’=’ Expr 
 ValDef        = ’val’ ident [’:’ Type] ’=’ Expr 
 Parameters = Parameter {’,’ Parameter} 
 Parameter  = ident ’:’ [’=>’] Type 

Definitions can be: 
- function definitions such as def square(x: Int): Int = x * x, 
- value definitions such as val y = square(2).

定義は
- 関数定義、例えば def square(x: Int): Int = x * x
- 値定義、例えば  val y = square(2)
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。