900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > PL/0 语言简介 PL/0 文法

PL/0 语言简介 PL/0 文法

时间:2024-02-21 23:36:52

相关推荐

PL/0 语言简介 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 是一个小巧的高级语言。虽然它只有整数类型,但它是相当完全的可嵌套的分程序(block)的程序结构,分程序中可以有常量定义、变量声明和无参过程声明,过程体又是分程序。PL/0 有赋值语句、条件语句、循环语句、过程调用语句、复合语句和空语句。由于上面这些语言概念已为大家熟知,因此不再进行语义解释。下面用习题 3.7 所介绍的扩展方式来给出 PL/0 语言的文法。

Program→BlockBlock→[ConstDecl][VarDecl][ProcDecl]StmtConstDecl→constConstDef,ConstDef;ConstDef→ident=numberVarDecl→varident,ident;ProcDecl→procedureident;Block;procedureident;Block;Stmt→ident:=Exp|callident|beginStmt;Stmtend|ifCondthenStmt|whileConddoStmt|εCond→oddExp|ExpRelOpExpRelOp→=|<>|<|>|<=|>=Exp→[+|−]Term+Term|−TermTerm→Factor∗Factor|/FactorFactor→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→BlockBlock→[ConstDecl][VarDecl][ProcDecl]StmtConstDecl→constConstDef,ConstDef;ConstDef→ident=numberVarDecl→varident,ident;ProcDecl→procedureident;Block;procedureident;Block;Stmt→ident:=Exp|callident|beginStmt;Stmtend|ifCondthenStmt|whileConddoStmt|εCond→oddExp|ExpRelOpExpRelOp→=|<>|<|>|<=|>=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;begina:=x; b:=y; z:=0;while b>0 dobeginif odd b then z:=z+a;a:=2*a; b:=b/2;endend;----------------------------------------------procedure divide;var w;beginr:=x; q:=0; w:=y;while w<=r do w:=2*w;while w>y dobeginq:=2*q; w:=w/2;if w<=r thenbeginr:=r-w;q:=q+1;endendend;----------------------------------------------procedure gcd;var f,g;beginf:=x;g:=y;while f<>g dobeginif f<g then g:=g-f;if g<f then f:=f-g;endend;beginx:=m; y:=n; call multiply;x:=25; y:=3; call divide;x:=34; y:=36; call gcd;end.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。