主题:关于PL/0编译程序的for扩展(用PASCAL写的)
帮我用PASCAL写一个FOR循环的编译程序啊 ????
PROGRAM PL0(INPUT,OUTPUT);
CONST
AL=10;{标识符长度}
NORW=15;{保留字个数}
TXMAX=100;{标识符表长度}
NMAX=14;{数中数字的最大个数}
AMAX=2047;{最大地址}
LEVMAX=3;{分程序嵌套的最大深度}
CXMAX=200;{目标代码数组的大小}
TYPE
SYMBOL=(NUL,IDENT,NUMBER,PLUS,MINUS,TIMES,SLASH,ODDSYM,EQL,NEQ,LSS,
LEQ,GTR,GEQ,LPAREN,RPAREN,COMMA,SEMICOLON,PERIOD,BECOMES,ELSESYM,
BEGINSYM,ENDSYM,IFSYM,THENSYM,WHILESYM,WRITESYM,READSYM,DOSYM,
CALLSYM,CONSTSYM,VARSYM,PROCSYM,PROGSYM);
ALFA=PACKED ARRAY[1..AL] OF CHAR;
BJECT=(CONSTANT,VARIABLE,PROCEDUR);
SYMSET=SET OF SYMBOL;
FCT=(LIT,OPR,LOD,STO,CAL,INT,JMP,JPC);
INSTRUCTION=PACKED RECORD
F:FCT;
L:0..LEVMAX;
A:0..AMAX;
END;
{LIT 0,A:取长量a
OPR 0,A:执行运算a
LOD 1,A:取变量(相对地址为;层差为1)
STD 1,A:存变量
CAL 1,A:调用过程(入口地址为a;层差为1)
INT 0,A:运行栈S的增加a
JPM 0,A:转移到指令地址a
JPC 0,A;条件转移到指令地址a处}
VAR
cchh:char;
CH:CHAR;{当前字符}
SYM:SYMBOL;{当前符号}
ID :ALFA;{当前标时符}
NUM:INTEGER;{当前数}
II:INTEGER;
CC:INTEGER;{字符计数}
LL:INTEGER;{行长}
LNUM:INTEGER;
KK:INTEGER;
CX:INTEGER;{目标代码下标}
ERR:INTEGER;
LINE:ARRAY[1..81] OF CHAR;
A:ALFA;
SC:TEXT;
SOUCFILE:STRING;
FNAME:ALFA;
CODE:ARRAY[0..CXMAX] OF INSTRUCTION;{目标代码区}
WORD:ARRAY[1..NORW] OF ALFA;{保留字表}
WSYM:ARRAY[1..NORW] OF SYMBOL;{保留字机内表示表}
SSYM:ARRAY[CHAR] OF SYMBOL;{运算符,界限符机内表示表}
MNEMONIC:ARRAY[FCT] OF PACKED ARRAY[1..5] OF CHAR;
DECLBEGSYS,STATBEGSYS,FACBEGSYS:SYMSET;
TABLE:ARRAY[0..TXMAX] OF
RECORD
NAME:ALFA;
CASE KIND:BJECT OF
CONSTANT:(VAL:INTEGER);
VARIABLE,PROCEDUR:(LEVEL,ADR,SIZE:INTEGER);
END;
PROCEDURE ERROR(N:INTEGER);
BEGIN
WRITE('ERROR IN LINE', LNUM,id);
WRITELN('*******',N:2);
READ(CCHH);
ERR:=ERR+1
END;{ERROR 过程结束}
PROCEDURE GETSYM;{词法分析}
VAR
I,J,K:INTEGER;
PROCEDURE GETCH;{读字符}
BEGIN
IF CC=LL
THEN BEGIN
IF EOF (SC)
THEN WRITE('PROGRAM INCOMPLETE');
LL:=0;CC:=0; WRITE(CX:4,' ');
WHILE NOT EOLN(SC) DO
BEGIN
LL:=LL+1;
READ(SC,CH);WRITE(CH);
LINE[LL]:=CH
END;
WRITELN;LNUM:=LNUM+1;LL:=LL+1;
READ(SC,LINE[LL]);READ(SC,CH);LINE[LL]:=' '
END;
CC:=CC+1;
CH:=LINE[CC]
END;{GETCH 过程结束}
。。。。。。。。
PROGRAM PL0(INPUT,OUTPUT);
CONST
AL=10;{标识符长度}
NORW=15;{保留字个数}
TXMAX=100;{标识符表长度}
NMAX=14;{数中数字的最大个数}
AMAX=2047;{最大地址}
LEVMAX=3;{分程序嵌套的最大深度}
CXMAX=200;{目标代码数组的大小}
TYPE
SYMBOL=(NUL,IDENT,NUMBER,PLUS,MINUS,TIMES,SLASH,ODDSYM,EQL,NEQ,LSS,
LEQ,GTR,GEQ,LPAREN,RPAREN,COMMA,SEMICOLON,PERIOD,BECOMES,ELSESYM,
BEGINSYM,ENDSYM,IFSYM,THENSYM,WHILESYM,WRITESYM,READSYM,DOSYM,
CALLSYM,CONSTSYM,VARSYM,PROCSYM,PROGSYM);
ALFA=PACKED ARRAY[1..AL] OF CHAR;
BJECT=(CONSTANT,VARIABLE,PROCEDUR);
SYMSET=SET OF SYMBOL;
FCT=(LIT,OPR,LOD,STO,CAL,INT,JMP,JPC);
INSTRUCTION=PACKED RECORD
F:FCT;
L:0..LEVMAX;
A:0..AMAX;
END;
{LIT 0,A:取长量a
OPR 0,A:执行运算a
LOD 1,A:取变量(相对地址为;层差为1)
STD 1,A:存变量
CAL 1,A:调用过程(入口地址为a;层差为1)
INT 0,A:运行栈S的增加a
JPM 0,A:转移到指令地址a
JPC 0,A;条件转移到指令地址a处}
VAR
cchh:char;
CH:CHAR;{当前字符}
SYM:SYMBOL;{当前符号}
ID :ALFA;{当前标时符}
NUM:INTEGER;{当前数}
II:INTEGER;
CC:INTEGER;{字符计数}
LL:INTEGER;{行长}
LNUM:INTEGER;
KK:INTEGER;
CX:INTEGER;{目标代码下标}
ERR:INTEGER;
LINE:ARRAY[1..81] OF CHAR;
A:ALFA;
SC:TEXT;
SOUCFILE:STRING;
FNAME:ALFA;
CODE:ARRAY[0..CXMAX] OF INSTRUCTION;{目标代码区}
WORD:ARRAY[1..NORW] OF ALFA;{保留字表}
WSYM:ARRAY[1..NORW] OF SYMBOL;{保留字机内表示表}
SSYM:ARRAY[CHAR] OF SYMBOL;{运算符,界限符机内表示表}
MNEMONIC:ARRAY[FCT] OF PACKED ARRAY[1..5] OF CHAR;
DECLBEGSYS,STATBEGSYS,FACBEGSYS:SYMSET;
TABLE:ARRAY[0..TXMAX] OF
RECORD
NAME:ALFA;
CASE KIND:BJECT OF
CONSTANT:(VAL:INTEGER);
VARIABLE,PROCEDUR:(LEVEL,ADR,SIZE:INTEGER);
END;
PROCEDURE ERROR(N:INTEGER);
BEGIN
WRITE('ERROR IN LINE', LNUM,id);
WRITELN('*******',N:2);
READ(CCHH);
ERR:=ERR+1
END;{ERROR 过程结束}
PROCEDURE GETSYM;{词法分析}
VAR
I,J,K:INTEGER;
PROCEDURE GETCH;{读字符}
BEGIN
IF CC=LL
THEN BEGIN
IF EOF (SC)
THEN WRITE('PROGRAM INCOMPLETE');
LL:=0;CC:=0; WRITE(CX:4,' ');
WHILE NOT EOLN(SC) DO
BEGIN
LL:=LL+1;
READ(SC,CH);WRITE(CH);
LINE[LL]:=CH
END;
WRITELN;LNUM:=LNUM+1;LL:=LL+1;
READ(SC,LINE[LL]);READ(SC,CH);LINE[LL]:=' '
END;
CC:=CC+1;
CH:=LINE[CC]
END;{GETCH 过程结束}
。。。。。。。。