Form Machine
输入/输出流与 form
本节描述规定数据重构的 form 的语法与语义。Form Machine 接收一个输入流, 依照描述重构的 form 对输入流进行重新格式化, 并把重排后的数据作为输出流发出。
阅读本节时, 建议把 form 应用到数据流的过程想象成图 5 描绘的样子。输入流指针标识 form 的某一部分在任一时刻正在分析的数据在输入流中的位置; 同样, 输出流指针定位输出流中正在发出的数据。
/\/\ /\/\
^ | | FORM | | ^
| | | ----------------- | | |
| | | +- ----------------- -+ | | |
| | | | CURRENT PART OF | | | |
INPUT | |<= CURRENT < ----------------- > CURRENT => | | OUTPUT
STREAM | | POINTER | FORM BEING APPLIED | POINTER | | STREAM
| | +- ----------------- -+ | |
| | ----------------- | |
| | ----------------- | |
| | ----------------- | |
\/\/ \/\/
Figure 5. Application of Form to Data Streams
Form Machine BNF 语法
form ::= rule | rule form
rule ;;= label inputstream outputstream ;
label ::= INTEGER | <null>
inputstream ::= terms | <null>
terms ::= term | terms , term
outputstream ::= : terms | <null>
term ::= identifier | identifier descriptor |
descriptor | comparator
identifier ::= an alpha character followed by 0 to 3
alphanumerics
descriptor ::= (replicationexpression , datatype ,
valueexpression , lengthexpression control)
comparator ::= (value connective value control) |
(identifier *<=* control)
replicationexpression ::= # | arithmeticexpression | <null>
datatype ::= B | O | X | E | A
valueexpression ::= value | <null>
lengthexpression ::= arithmeticexpression | <null>
connective ::= .LE. | .LT. | .GE. | .GT. | .EQ. | .NE.
value ::= literal | arithmeticexpression
arithmeticexpression ::= primary | primary operator
arithmeticexpression
primary ::= identifier | L(identifier) | V(identifier) |
INTEGER
operator ::= + | - | * | /
literal ::= literaltype "string"
literaltype ::= B | O | X | E | A
string ::= from 0 to 256 characters
control ::= : options | <null>
options ::= S(where) | F(where) | U(where) |
S(where) , F(where) |
F(where) , S(where)
where ::= arithmeticexpression | R(arithmeticexpression)
Form Machine 语法的另一种表述
infinity
form ::= {rule}
1
1 1 1
rule ::= {INTEGER} {terms} {:terms} ;
0 0 0
infinity
terms ::= term {,term}
0
1
term ::= identifier | {identifier} descriptor
0
| comparator
1
descriptor ::= ({arithmeticexpression} , datatype ,
0
1 1 1
{value} , {lengthexpression} {:options}
0 0 0
1
comparator ::= (value connective value {:options} ) |
0
1
(identifier .<=. value {:options} )
0
connective ::= .LE. | .LT. | .GE. | .GT. | .EQ. | .NE.
lengthexpression ::= # | arithmeticexpression
datatype ::= B | O | X | E | A
value ::= literal | arithmeticexpression
infinity
arithmeticexpression ::= primary {operator primary}
0
operator ::= + | - | * | /
primary ::= identifier | L(identifier) |
V(identifier) | INTEGER
256
literal ::= literaltype "{CHARACTER} "
0
literaltype ::= B | O | X | A | E
1
options ::= S(where) {,F(where)} |
0
1
F(where) {,S(where)} | U(where)
0
where ::= arithmeticexpression |
R(arithmeticexpression)
3
identifier ::= ALPHABETIC {ALPHAMERIC}
0
form (Forms)
form 是规则的有序集合。
form ::= rule | rule form
当前规则被应用到输入流的当前位置。如果 (规则的输入流部分) 未能正确描述当前输入的内容, 则另一条规则成为当前规则, 并被应用到输入流的当前位置。下一条成为当前的规则, 或由当前规则中的当前 term 显式指定, 或默认为按顺序的下一条规则。控制流在 TERM AND RULE SEQUENCING 一节中有更完整的描述。
如果 (规则的输入流部分) 成功地正确描述了当前输入流, 则依照该规则, 可能会在输出流的当前位置发出一些数据。输入与输出流指针分别越过被描述和被发出的数据而前进, 然后下一条规则被应用到输入流新的当前位置。
当在一条规则中遇到显式返回 (R(arithmeticexpression)) 时, form 的应用终止。用户连接与服务连接被关闭, 返回码 (arithmeticexpression) 被发送给发起用户。
规则 (Rules)
规则是一次替换、比较和/或赋值操作, 其形式如下所示。
rule ::= label inputstream outputstream
label 是规则的名字, 它的存在使该规则可以在 form 的其他位置被引用, 以实现显式的规则间控制转移。label 的形式如下。
label ::= INTEGER | <null>
可选的整数 label 取值范围为 0 >= INTEGER >= 9999。各规则不必按数值递增的顺序标注。
term (Terms)
inputstream (描述要匹配的输入流) 与 outputstream (描述要在输出流中发出的数据) 由零个或多个 term 组成, 形式如下所示。
inputstream ::= terms | <null>
outputstream ::= :terms | <null>
terms ::= term | terms , term
term 有如下四种格式之一。
term ::= identifier | identifier descriptor |
descriptor | comparator
Term 格式 1
第一种 term 格式如下所示。
identifier
identifier 是对 form 中一个先前已定义的 term (term 格式 2) 的符号引用。它取得同名 term 的相同属性 (值、长度、类型)。Term 格式 1 通常用于在输出流中发出数据。
identifier 由一个字母字符后跟 0 到 3 个字母数字字符构成。
Term 格式 2
第二种 term 格式如下所示。
identifier descriptor
Term 格式 2 一般用作输入流 term, 但也可以用作输出流 term。
descriptor 的定义如下所示。
descriptor ::= (replicationexpression, datatype,
valueexpression, lengthexpression
control)
identifier 是该 term 的符号名, 与通常程序设计语言中的意义一致。它取得该 term 的类型、长度、值和重复属性, 并可在 form 的其他位置被引用。
若指定了重复表达式 (replication expression), 它会使该 term 的单元值按重复表达式的值所指示的次数生成。term 的单元值 (要重复的量) 由数据类型、值表达式和长度表达式属性确定。数据类型定义所规定数据的种类。值表达式规定一个名义值 (nominal value), 并由其他 term 属性加以修饰。长度表达式确定 term 的单位长度。(关于伪指令 "defined constant" 的类似解释, 参见 IBM SRL Form C28-6514, descriptor 即以此为模型。)
重复表达式定义如下。
replicationexpression ::= # | arithmeticexpression | <null>
arithmeticexpression ::= primary | primary operator
arithmeticexpression
operator ::= + | - | * | /
primary ::= identifier | L(identifier) | V(identifier) |
INTEGER
重复表达式是一个施加于数据类型、值和长度表达式组合之上的重复函数。它表示名义值要重复的次数。
终止符号 # 表示任意的重复因子。它必须由与输入流的匹配或不匹配显式终止。该终止可能来自同一个 term 或下一个 term。
空的重复表达式取值为 1。算术表达式自左向右求值, 无优先级。
L(identifier) 是长度算符, 生成一个 32 位二进制整数, 对应于所指名 term 的长度。V(identifier) 是值算符, 生成一个 32 位二进制整数, 对应于所指名 term 的值。(见 Restrictions and Interpretations of Term Functions 一节。) 值算符的用意是把字符串转换为其对应的数值。
数据类型定义如下。
datatype ::= B | O | X | E | A
数据类型描述该 term 所表示的数据的种类。(预期今后会按需增加其他数据类型, 例如浮点类型和用户自定义类型。)
Data Type Meaning Unit Length
B Bit string 1 bit
O Bit string 3 bits
X Bit string 4 bits
E EBCDIC character 8 bits
A Network ASCII character 8 bits
值表达式定义如下。
valueexpression ::= value | <null>
value ::= literal | arithmeticexpression
literal ::= literaltype "string"
literaltype ::= B | O | X | E | A
值表达式是 term 的名义值, 以数据类型所指示的格式表示。它按重复表达式重复。
输入流 term 的空值表达式默认取输入流中现成的数据; 但数据必须符合 datatype 属性。
输出流 term 的空值表达式按 Restrictions and Interpretations of Term Functions 一节的规定生成填充。
长度表达式定义如下。
lengthexpression ::= arithmeticexpression | <null>
长度表达式给出包含值表达式的字段的长度。
若长度表达式小于或等于零, 则该 term 成功, 但相应的流指针不前进。若 term 其余部分成功, 正长度会使相应的流指针前进。
control 在 TERM AND RULE SEQUENCING 一节中定义。
Term 格式 3
第三种 term 格式如下所示。
descriptor
它与 term 格式 2 完全相同, 只是省去了 identifier。Term 格式 3 一般用于输出流。它也用于输入流中输入数据只需越过而无需保留以便发出或日后引用的场合。
Term 格式 4
第四种 term 格式如下所示。
comparator ::= (value connective value control) |
(identifier *<=* value control)
value ::= literal | arithmeticexpression
literal ::= literaltype "string"
literaltype ::= B | O | X | E | A
string ::= from 0 to 256 characters
connective ::= .LE. | .LT. | .GE. | .GT. | .EQ. | .NE.
第四种 term 格式用于赋值和比较。
赋值算符 *<=* 把值赋给 identifier。各连接词 (connective) 具有通常的含义。要比较的值必须具有相同的类型和长度属性, 否则产生错误条件, form 失败。
term 的应用
term 的各元素按如下步骤序列应用。
- 数据类型、值表达式和长度表达式共同确定一个单元值, 记为 x。
- 重复表达式规定 x 要重复的次数。串接后的各 x 之值成为长度为 L 的 y。
- 若该 term 是输入流 term, 则长度为 L 的 y 之值从当前输入指针位置开始与输入值进行比较。
- 若输入值在长度 L 上满足 y 的约束, 则该长度为 L 的输入值成为该 term 的值。
对输出流 term, 过程相同, 只是输入的来源是值表达式中所述 term 的值, 而数据被发到输出流中。
由于符号 # 表示任意重复因子, 重复值的长度可能不定, 上述过程因此被修改为包含一个 term 的向前查看 (look-ahead)。
term 功能的限制与解释
- 因其值按 # 符号重复而长度不定的 term, 必须用某种类型相关的数据 (如字面量 literal) 分隔。(不过字面量并非硬性要求; 一段任意长度的 ASCII 字符可以由一个非 ASCII 字符终止。)
- 截断与填充规则如下:
- 字符到字符 (A <-> E) 的转换左对齐, 右侧截断或以空格填充。
- 字符到数值、数值到数值的转换右对齐, 左侧截断或以零填充。
- 数值到字符的转换右对齐, 左侧以空格填充。
- 在控制连接上传送的 form 定义中, 以下内容被忽略:
- TELNET 控制字符。
- 引号之外的空格。
- 引号之外的
/* string */被当作注释。
- 当 term 的某部分缺省时, 适用如下默认值:
- 重复表达式默认为 1。
- 输出流 term 中的 # 默认为 1。
- 输入流 term 的值表达式默认取输入流中找到的值, 但输入流必须符合数据类型和长度表达式。输出流 term 的值表达式默认为仅做填充。
- 长度表达式默认为由数据类型和值表达式确定的量的大小。
- 若某 term 成功应用, control 默认为按顺序的下一个 term; 否则默认为按顺序的下一条规则。若 where 求值结果是一个未定义的 label, 则 form 失败。
- 算术表达式自左向右求值, 无优先级。
- 适用如下限制:
- 二进制长度 <= 32 位
- 字符串 <= 256 个 8 位字符
- identifier 名字 <= 4 字符
- identifier 最大个数 <= 256
- label 整数 >= 0 且 <= 9999
- 值算符与长度算符生成 32 位二进制整数。值算符当前的用途是把 A 或 E 类型的十进制字符串转换为其对应的二进制数。例如, E'12' 的值是 0......01100; 而 E'AB' 的值会导致 form 失败。
term 与规则的排序
排序可以通过在 term 中包含 control 来显式控制。
control ::= :options | <null>
options ::= S(where) | F(where) | U(where)
S(where) , F(where) |
F(where) , S(where)
where ::= arithmeticexpression | R(arithmeticexpression)
S、F、U 分别表示成功转移、失败转移和无条件转移。where 求值为一个 rule label, 因此可以把控制从规则内部 (在 term 的末尾) 转移到另一条规则的开头。R 表示终止 form, 并把求得的表达式经控制连接 (若仍然打开) 返回给发起方。
若 term 未被显式排序, 适用如下默认值。
1) When a term fails go to the next sequential rule.
2) When a term succeeds go to the next sequential
term within the rule.
3) At the end of a rule, go to the next sequential
rule.
在下面的例子中, 请注意控制转移与输入指针移动之间的关联。
1 XYZ(,B,,8:S(2),F(3)) : XYZ ;
2 . . . . . . .
3 . . . . . . .
XYZ 的值永远不会在输出流中发出, 因为无论成功或失败, 控制都会转移到该规则之外。若该 term 成功, 输入的 8 位将被赋为 XYZ 的值, 然后规则 2 将被应用到同一输入流数据上。也就是说, 由于规则 1 的左半部分并未整体成功应用, 输入流指针没有前进。