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


    PL/0 语言简介
    A. PL/0 语言是 Pascal 语言的子集
       - 数据类型只有整型
       - 标识符的有效长度是 10 ,以字母开头的字母数字串
       - 数最多 14- 过程无参,可嵌套(最多三层),可递归调用
       - 变量的作用域同 Pascal ,常量为全局的
    
    B. 语句类型:
       - 赋值语句: if…then…, whiledo, read, write, call
       - 复合语句: begin …end
       - 说明语句: const, var…, procedure…
    
    C. 13个保留字:
       - If/then, while/do, read/write,
       -  call, begin/end, const, var, procedure, odd
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    PL/0 程序设计语言是一个较简单的语言,它以赋值语句为基础,构造概念有 顺序、条件和重复(循环)三种。PL/0 有子程序概念,包括过程定义(可以嵌 套)与调用且有局部变量说明。PL/0 中唯一的数据类型是整型,可以用来说明 该类型的常量和变量。当然 PL/0 也具有通常的算术运算和关系运算。
    PL/0 文法
    PL/0 是一个小巧的高级语言。虽然它只有整数类型,但它是相当完全的可嵌套的分程序(block)的程序结构,分程序中可以有常量定义、变量声明和无参过程声明,过程体又是分程序。PL/0 有赋值语句、条件语句、循环语句、过程调用语句、复合语句和空语句。由于上面这些语言概念已为大家熟知,因此不再进行语义解释。下面用习题 3.7 所介绍的扩展方式来给出 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 )

    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 | − Term}Term → Factor {∗ Factor | / Factor}Factor → ident | number | ( Exp )" role="presentation">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 | − Term}Term → Factor {∗ Factor | / Factor}Factor → ident | number | ( Exp )
    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;
    				if g;
    			end
    	end;
    begin
    	x:=m; y:=n; call multiply;
    	x:=25; y:=3; call divide;
    	x:=34; y:=36; call gcd;
    end.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    【广州华锐互动】城市水处理VR仿真实训平台
    什么是Boot Guard?电脑启动中的信任链条解析
    详解 SpringMVC 的 HttpMessageConverter
    bestphp‘s revenge/ 安洵杯Babyphp(phpsession题目)
    产品问答:陪学数据分析公开课答疑
    【kali-漏洞利用】(3.3)后渗透之Meterpreter(上):命令大全
    附参考文献丨艾美捷Cholesterol胆固醇说明书
    【优化求解】基于matlab遗传算法求解立体仓库出入库路径优化问题【含Matlab源码 2028期】
    RegNet架构复现--CVPR2020
    激光雷达构建地图( 覆盖栅格建图)
  • 原文地址:https://blog.csdn.net/weixin_56462041/article/details/127709193