| PL/0 语言简介 |
A. PL/0 语言是 Pascal 语言的子集
- 数据类型只有整型
- 标识符的有效长度是 10 ,以字母开头的字母数字串
- 数最多 14 位
- 过程无参,可嵌套(最多三层),可递归调用
- 变量的作用域同 Pascal ,常量为全局的
B. 语句类型:
- 赋值语句: if…then…, while…do…, read, write, call
- 复合语句: begin …end
- 说明语句: const…, var…, procedure…
C. 13个保留字:
- If/then, while/do, read/write,
- call, begin/end, const, var, procedure, odd
| PL/0 文法 |
Program → Block
Block → [ConstDecl] [VarDecl][ProcDecl] Stmt
ConstDecl → const ConstDef , ConstDef ;
ConstDef → ident = number
VarDecl → var ident , ident ;
ProcDecl → procedure ident ; Block ; procedure ident ; Block ;
Stmt → ident := Exp | call ident | begin Stmt ; Stmt end |
if Cond then Stmt | while Cond do Stmt | ε
Cond → odd Exp | Exp RelOp Exp
RelOp → = | <> | < | > | <= | >=
Exp → [+ | − ] Term + Term | − Term
Term → Factor ∗ Factor | / Factor
Factor → ident | number | ( Exp )
const m=7, n=85;
var x,y,z,q,r;
procedure multiply;
var a,b;
begin
a:=x; b:=y; z:=0;
while b>0 do
begin
if odd b then z:=z+a;
a:=2*a; b:=b/2;
end
end;
----------------------------------------------
procedure divide;
var w;
begin
r:=x; q:=0; w:=y;
while w<=r do w:=2*w;
while w>y do
begin
q:=2*q; w:=w/2;
if w<=r then
begin
r:=r-w;
q:=q+1;
end
end
end;
----------------------------------------------
procedure gcd;
var f,g;
begin
f:=x;
g:=y;
while f<>g do
begin
if f;
if g;
end
end;
begin
x:=m; y:=n; call multiply;
x:=25; y:=3; call divide;
x:=34; y:=36; call gcd;
end.

