跳到主要内容

IV. 示例

说明​

以下示例(形式以及单个规则)是 Form Machine 的一些简单的代表性用法。示例逐行写出一个项,仅仅是为了便于说明。通常,单个规则可以写成单行。

字段插入​

要插入一个字段,应把输入分隔成两个项,以便在它们之间允许插入字段。例如,要为一台每行 121 个字符、带前导回车控制字符的行式打印机做行编号,可以使用如下形式。

(NUMB.<=>.1);       /*initialize line number counter to one*/
1 CC(,E,,1:F(R(99))), /*pick up control character and save
as CC*/
/*return a code of 99 upon exhaustion*/
LINE(,E,,121 : F(R(98))) /*save text as LINE*/
:CC, /*emit control character*/
(,E,NUMB,2), /*emit counter in first two columns*/
(,E,E".",1), /*emit period after line number*/
(,E,LINE,117), /*emit text, truncated in 117 byte field*/
(NUMB.<=.NUMB+1:U(1)); /*increment line counter and go to
rule one*/;;

删除​

要删除的数据应当被隔离为左边的单独项,这样它们就可以在右边被省略(不发出)。

(,B,,8),           /*isolate 8 bits to ignore*/
SAVE(,A,,10) /*extract 10 ASCII characters from
input stream*/
:(,E,SAVE,); /*emit the characters in SAVE as EBCDIC
characters whose length defaults to the
length of SAVE, i.e., 10, and advance to
the next rule*/

在上面的例子中,如果任一输入流项失败,则应用下一条顺序规则。

变长记录​

有些设备、终端和程序会生成变长记录。下面的规则拾取变长的 EBCDIC 记录并把它们翻译成 ASCII。

CHAR(,E,,#),       /*pick up all (an arbitrary number of)
EBCDIC characters in the input stream*/
(,X,X"FF",2) /*followed by a hexadecimal literal,
FF (terminal signal)*/
:(,A,CHAR,), /*emit them as ASCII*/
(,X,X"25",2); /*emit an ASCII carriage return*/

字符串长度计算​

常常需要在一个任意长的字符串前面加上长度字段。下面的规则在一个 EBCDIC 字符串前加上一个单字节的长度字段。

Q(,E,,#),          /*pick up all EBCDIC characters*/
TS(,X,X"FF",2) /*followed by a hexadecimal literal, FF*/
:(,B,L(Q)+2,8), /*emit the length of the characters
plus the length of the literal plus
the length of the count field itself,
in an 8-bit field*/
Q, /*emit the characters*/
TS; /*emit the terminal*/

转置​

重新排列字段的次序常常是需要的,如下例所示。

Q(,E,,20), R(,E,,10) , S(,E,,15), T(,E,,5) : R, T, S, Q ;

各项以不同的次序被发出。

字符打包与解包​

在诸如 HASP 之类的系统中,重复的字符序列被压缩成一个计数后跟该字符,以便更高效地存储和传输。第一个形式打包多个字符,第二个形式则将其解包。

/*form to pack EBCDIC streams*/
/*returns 99 if OK, input exhausted*/
/*returns 98 if illegal EBCDIC*/
/*look for terminal signal FF which is not a legal EBCDIC*/
/*duplication count must be 0-254*/
1 (,X,X"FF",2 : S(R(99))) ;
/*pick up the EBCDIC and initialize count*/
CHAR(,E,,1 : F(R(98))) , (CNT .<=. 1) ;
/*count consecutive EBCDICs like CHAR*/
2 (,E,CHAR,1 : F(3)) , (CNT .<=. CNT+1 : U(2)) ;
/*emit count and current character*/
3 : (,B,CNT,8), CHAR, (:U(1));
/*end of form*/;;

/*form to unpack EBCDIC streams*/
/*look for terminal*/
1 (,X,X"FF",2 : S(R(99))) ;
/*emit character the number of times indicated*/
/*by the counter contents*/
CNT(,B,,8), CHAR(,E,,1) : (CNT,E,CHAR,CNT:U(1));
/*failure of form*/
(:U(R(98))) ;;