没有合适的资源?快使用搜索试试~ 我知道了~
pl0(pascalversion)编译器.pdf
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 88 浏览量
2021-10-02
12:11:12
上传
评论
收藏 22KB PDF 举报
温馨提示
试读
13页
pl0(pascalversion)编译器.pdf
资源推荐
资源详情
资源评论
program pl0(input,output);
{pl/0 compiler with code generation}
label 99;
const norw = 11; {no. of reserved words} { 保留字个数 }
txmax = 100; {length of identifier table} { 标识符变长度 }
nmax = 14; {max. no. of digits in numbers}{ }
al = 10; {length of identifiers} { 标识符的长度。。。。为 10}
amax = 2047; {maximum address} { 最大地址 }
levmax = 3; {maximum depth of block nesting} {}
cxmax = 200; {size of code array} {代码数组长度 }
type symbol =
(nul,ident,number,plus,minus,times,slash,oddsym,
{ 等号,标识符 ,数字, + , - ,* , / ,wsym[ 7] := oddsym }
eql,neq,lss,leq,gtr,geq,lparen,rparen,comma,semicolon,
{=,#,<, [ , >, ], ( , ) , ‘ , ’ , ; }
period,becomes,beginsym,endsym,ifsym,thensym,
{ ‘ . ’, 赋值 ,开始, 结束 , if , then } { 不确定 }
whilesym,dosym,callsym,constsym,varsym,procsym);
{while , do , call , const, var , procedure} { 不确定 }
alfa = packed array [1..al] of char;{字符数组 ,存放关键字 }
object1 = (constant,varible,proc);{判断标识符是常量、变量还是过程? }
symset = set of symbol;
fct = (lit,opr,lod,sto,cal,int,jmp,jpc); {functions 目标代码的功能码,具体如
下: }
instruction = packed record { 指令记录 目标代码结构 }
f: fct; {function code} {}
l: 0..levmax; {level 层数 }
a: 0..amax {displacement address}
end;
{ lit 0,a : load constant a
opr 0,a : execute operation a
lod l,a : load varible l,a
sto l,a : store varible l,a
cal l,a : call procedure a at level l
int 0,a : increment t-register by a
jmp 0,a : jump to a
jpc 0,a : jump conditional to a }
var ch: char; {last character read 当前字符 }
sym: symbol; {last symbol read 当前读入的符号 }
id: alfa; {last identifier read 当前标识符 }
num: integer; {last number read} { 当前数字 }
cc: integer; {character count} { 当前读入字符数目 }
ll: integer; {line length} { 当前行数 }
kk, err: integer;
cx: integer; {code allocation index} { ?????? }
line: array [1..81] of char; { 当前读入的一行 }
a: alfa;
code: array [0..cxmax] of instruction; {存放生成的目标代码 }
word: array [1..norw] of alfa; { 存放的关键字数组 }
wsym: array [1..norw] of symbol; {标识符字符集 }
ssym: array [char] of symbol; {符号语义字符集识别符号 类似 token.seman}
mnemonic: array [fct] of
packed array [1..5] of char; { 功能码字符数组 }
declbegsys, statbegsys, facbegsys: symset;
table: array [0..txmax] of { 符号表 ??? }
record name: alfa;
case kind: object1 of
constant: (val: integer);
varible, proc: (level, adr: integer)
end;
fin:text; {存放源文件。。。}
sfile:string;
{----------------------------------------------------------------------------------------------------}
procedure error(n: integer); { 给出错误提示 }
begin writeln(' ****',' ': cc-1, '^',n: 2); err := err+1 { 给出错误位置 数目+1}
end {error};
{----------------------------------------------------------------------------------------------------}
procedure getsym;
var i,j,k: integer;
procedure getch;
begin if cc = ll then { 到达行尾 }
begin if eof(fin) then { 文件结束? }
begin write(' program incomplete'); {goto 99}
{ 文件空,结束。 }
close(fin);
exit;
end;
{ 文件没有结束时: }
ll := 0; cc := 0; write(cx: 5,' '); { 行长度和字符个数清零,输出行数: }
while not eoln(fin) do { 有回车(换行)时结束循环 }
begin
ll := ll+1; read(fin,ch); write(ch); line[ll]:=ch
{ 此一行长度加 1,读一个字符,回显,置于数组中 }
end;
writeln; { 读入回车时,换行。 。。。。。。。。。。。。。}
readln(fin); {。。。。。。。。}
ll := ll + 1; line[ll] := ' '; {}
end;
cc := cc+1; ch := line[cc] { 读入一个字符 }
end {getch};
begin {getsym} { getsys.....。。。。。}
while ch = ' ' do getch; { 空格过滤 }
if ch in ['a'..'z'] then
begin {identifier or reserved word} k := 0; { 标识符或者保留字 }
repeat if k < al then {读入一个完整的标识符或者保留字 }
begin k := k+1; a[k] := ch
end;
getch;
until not(ch in ['a'..'z','0'..'9']);
if k >= kk then kk := k else { 如果 关键字的个数不足十,补空格 }
repeat a[kk] := ' '; kk := kk-1
until kk = k;
id := a; i := 1; j := norw; {norw=11-}
repeat k := (i+j) div 2; { 查找是否为保留字。。。。。。。。}
if id <= word[k] then j := k-1;
if id >= word[k] then i := k+1
until i > j;
if i-1 > j then sym := wsym[k] else sym := ident { 是标识符,将内部表示
赋给 sys…… .否则,将 ”ident”赋给 sys}
end else
if ch in ['0'..'9'] then { 如果读入的是数字 }
begin {number} k := 0; num := 0; sym := number;
repeat num := 10*num + (ord(ch)-ord('0')); {拼数。。。。。}
k := k+1; getch
until not(ch in ['0'..'9']);
if k > nmax then error(30) { 数值超出范围,错误 }
end else
if ch = ':' then
begin getch; { 判断是否为赋值语句。 。。。。。}
if ch = '=' then
begin sym := becomes; getch { 赋值语句。。。}
剩余12页未读,继续阅读
资源评论
资料大全
- 粉丝: 14
- 资源: 26万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功