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 程序设计语言是一个较简单的语言,它以赋值语句为基础,构造概念有 顺序、条件和重复(循环)三种。PL/0 有子程序概念,包括过程定义(可以嵌 套)与调用且有局部变量说明。PL/0 中唯一的数据类型是整型,可以用来说明 该类型的常量和变量。当然 PL/0 也具有通常的算术运算和关系运算。
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 ) \begin{aligned} &\text{Program → Block }\\ &\text{Block → [ConstDecl] [VarDecl][ProcDecl] Stmt}\\ &\text{ConstDecl → const ConstDef {, ConstDef} ;}\\ &\text{ConstDef → ident = number}\\ &\text{VarDecl → var ident {, ident} ;}\\ &\text{ProcDecl → procedure ident ; Block ; {procedure ident ; Block ;}}\\ &\text{Stmt → ident := Exp | call ident | begin Stmt {; Stmt} end |}\\ &\text{ if Cond then Stmt | while Cond do Stmt | ε}\\ &\text{Cond → odd Exp | Exp RelOp Exp}\\ &\text{RelOp → = | <> | < | > | <= | >=}\\ &\text{Exp → [+ | − ] Term {+ Term | − Term}}\\ &\text{Term → Factor {∗ Factor | / Factor}}\\ &\text{Factor → ident | number | ( Exp )}\\ &\end{aligned} Program → Block Block → [ConstDecl] [VarDecl][ProcDecl] StmtConstDecl → const ConstDef , ConstDef ;ConstDef → ident = numberVarDecl → 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 ExpRelOp → = | <> | < | > | <= | >=Exp → [+ | − ] Term + Term | − TermTerm → Factor ∗ Factor | / FactorFactor → ident | number | ( Exp )
其中的标识符 ident 是字母开头的字母数字串,number 是无符号整数,begin、call、const、do、end、if、odd、procedure、then、var、while 是保留字。用 PL/0 语言写的一个程序如下,它有 3 个过程,分别计算两个整数相乘、相除和求最大公约数。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<g then g:=g-f;
if g<f then f:=f-g;
end
end;
begin
x:=m; y:=n; call multiply;
x:=25; y:=3; call divide;
x:=34; y:=36; call gcd;
end.