跳到主要内容

示例

下面的示例 (既有完整的 form, 也有单条规则) 是 Form Machine 的一些简单而有代表性的用法。示例采用每行一个 term 的书写格式, 仅为便于讲解; 通常, 单条规则可以写成一行。

字段插入​

要插入一个字段, 应把输入拆分为两个 term, 以便让被插入的字段位于二者之间。例如, 要为一台行宽 121 字符、行首带回车控制字符的打印机做行号编号, 可使用下面的 form。

(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*/;;

删除​

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

(,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*/

在上例中, 若输入流的任一 term 失败, 则应用按顺序的下一条规则。

变长记录​

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

CHAR(#,E,,1),      /*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,,1),         /*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 ;

各 term 以不同的顺序发出。

字符打包与解包​

在 HASP 这类系统中, 为了更高效地存储和传输, 重复的字符序列被压缩成 "计数 + 字符" 的形式。第一个 form 打包多个字符, 第二个 form 解包它们。

/*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 an EBCDIC char/*
CHAR(,E,,1) ;
/*get identical EBCDIC chars/*
LEN(#,E,CHAR,1)
/*emit the count and the char/*
: (,B,L(LEN)+1,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 count, in a field the length indicated*/
/*by the counter contents*/
CNT(,B,,8), CHAR(,E,,1) : (CNT,E,CHAR,1:U(1));
/*failure of form*/
(:U(R(98))) ;;

[ 本 RFC 已转为机器可读形式 ] [ 由 Simone Demmel 于 03/98 录入在线 RFC 档案 ]