📄 20041016212439689.txt
字号:
测试代码:
const a=10;
var b;
begin
b:=5;
write(a*b*(2+4/2));
end.
输出结果:
0 jmp 0 1
1 int 0 4
2 lit 0 5
3 sto 0 3
4 lit 0 10
5 lod 0 3
6 opr 0 4
7 lit 0 2
8 lit 0 4
9 lit 0 2
10 opr 0 5
11 opr 0 2
12 opr 0 4
13 opr 0 14
14 opr 0 15
15 opr 0 0
start pl/0 :
200
end pl/0 !
程序源代码:
/*假想栈式计算机目标指令集
指令格式: f l a
1。lit :将常量取到运行栈顶,a域为常数值
2。lod:将变量放到栈顶。a域为变量在所说明层的相对位置,l为调用层与说明层的层差值。
3。sto:将栈顶的内容送入某变量单元中。a,l域的含义同lod指令。
4。cal:调用过程的指令。a为被调用过程的目标程序入口地址,l为层差
5。int:为被调用的过程(或主程序)在运行栈中开辟数据区。a域为开辟的单元个数。
6。jmp:无条件转移指令,a为转向地址。
7。jpc:条件转移指令,当栈顶的布尔值为非真时,转向a域的地址,否则顺序执行。
8。 opr: 关系运算和算术运算指令。具体操作由a域值给出
0 return
1 - (求反)
2 +
3 -(减)
4 *
5 /
6 true
7
8 ==
9 <>
10 <
11 >=
12 >
13 <=
14 cout
15 cout<<endl;
16 cin
*/
/*
PL0语言的ENBF表示
<程序>::=<分程序>.
<分程序>::=[<常量说明>][<变量说明>][<过程说明>]<语句>
<常量说明>::=CONST〈常量定义〉{,〈常量定义};
〈常量定义〉::=<标识符>=<无符号整数>;
<无符号整数>::=<数字>{<数字>}
<变量说明>:=VAR〈标识符〉{,〈标识符〉};
〈标识符〉::=〈字母〉{〈字母〉|〈数字〉}
<过程说明>::=〈过程首部〉〈分程序〉{;〈过程说明〉};
〈过程首部〉::=PROCCEDURE〈标识符〉;
〈语句〉::=〈赋值语句〉|〈条件语句〉|〈当循环语句〉|〈过程调用语句〉|〈读语句〉|〈写语句|〉〈复合语句〉|〈空〉
〈赋值语句〉::=〈标识符〉:=〈表达式〉
〈复合语句〉::=BEGIN〈语句〉{,〈语句〉}END
〈条件〉::=〈表达式〉〈关系运算符〉〈表达式〉|ODD〈表达式〉
〈表达式〉::=[+|-]<项>{〈加法运算符〉〈项〉};
〈项〉::=〈因子〉{〈乘法运算符〉〈因子〉}
〈因子〉::=〈标识符〉|〈无符号整数〉‘(’表达式‘)
〈加法运算符〉:=+|-
〈乘法运算符〉:=*|/
〈关系运算符〉:==|#|〈|〈=|〉|〉=
〈条件语句〉::=IF〈条件〉THEN〈语句〉
〈过程调用语句〉::=CALL〈标识符〉
〈当循环语句〉::WHILE〈条件〉DO〈语句〉
〈读语句〉:=READ‘(’〈标识符〉{,〈标识符〉}‘)’
〈写语句〉:=WRITE‘(’〈表达式〉{,〈表达式〉}‘)’
〈字母〉:=a|b...X|Y|Z
〈数字〉:=1|2|3|4|5|6|7|8|9|0
’
*/
/*-----------------adopt by c.w.t.----------------------------------*/
#include<fstream>
#include<iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include<vector>
#include<iterator>
using namespace std;
/*-------------------------define constant------------------------*/
# define norw 16
# define txmax 100
# define al 10
# define nmax 14
# define amax 2047
# define levmax 3
# define cxmax 200
# define stacksize 50000
/*-----------------------type define-----------------------------*/
typedef struct { int level, adr, size, val, foot;
string name, kind; } tbl;
typedef struct { string f;
int l,a; } instruction;
typedef struct { string sy[30]; } symset;
/*------------------------------variable declaration------------------------*/
int ll,cc,listswitch,err,num,kk,cx;
string id,sym,a,fname;
char ch;
vector<int> line(1000);
vector<int> s(10000);
string text;
int ptext;
vector<instruction> code(cxmax+1);
vector<tbl> table(txmax+1);
vector<tbl> str_table(txmax+1);
ifstream finf;
string word[]={"0","begin","call","const","do","else","end",
"if","odd","procedure","read","repeat","then",
"until","var","while","write"};
string wsym[]={"0","beginsym","callsym","constsym",
"dosym","elsesym","endsym","ifsym","oddsym",
"procsym","readsym","repeatsym","thensym","untilsym","varsym",
"whilesym","writesym"};
string ssym[]={"period","lss","lparen","plus","times","rparen",
"semicolon","minus","slash","comma","colon","neq","eql"};
symset typesys={"int","string","*"};
symset declbegsys={"constsym","varsym","procsym","*"};
symset statbegsys={"beginsym","callsym","ifsym","repeatsym","whilesym","readsym","writesym","*"};
symset facbegsys={"ident","number","lparen","*"};
symset fsysa={"period","constsym","varsym","procsym",
"beginsym","callsym","ifsym","whilesym","repeat","*"};
string what_identify(char ch){
switch(ch){
case '.':return string("period");
case '<':return string("lss");break;
case '(':return string("lparen");break;
case '+':return string("plus");break;
case '*':return string("times");break;
case ')':return string("rparen");break;
case ';':return string("semicolon");break;
case '-':return string("minus");break;
case '/':return string("slash");break;
case ',':return string("comma");break;
case ':':return string("colon");break;
case '#':return string("neq");break;
case '=':return string("eql");break;
case '{':return string("begin");break;
case '}':return string("end");break;
default:return string("");
}
}
void gen(string , int , int );
void error(int n);
void getsym();
void constdeclaration(int *dx,int *tx,int lev);
void vardeclaration(int * dx,int *tx,int lev);
void enter(string k,int *dx,int *tx,int lev);
void insert(string sym1,symset fsys,symset *fsys1);
void test(symset s1,symset s2,int n);
int in(string sm,symset fsys);
void statement(symset fsys,int tx,int lev);
void listcode(int cx0);
int position(string id,int tx);
void expression(symset fsys,int tx,int lev);
void term(symset fsys,int tx,int lev);
void condition(symset fsys,int tx,int lev);
void factor(symset fsys,int tx,int lev);
void getch();
/*------------------------------block (lev, tx, fsys)-----------------------------*/
void block(int lev,int tx,symset fsys)
{
int tx0,tx1,cx0,dx;
symset fsys1;
dx=3;
tx0=tx;
table[tx].adr=cx;
gen("jmp",0,0);
if (lev>levmax)error(32);
do{
if (sym=="constsym"){
getsym( );
do {
constdeclaration(&dx,&tx,lev);
while (sym=="comma"){
getsym( );
constdeclaration(&dx,&tx,lev);
}
if (sym=="semicolon"){
getsym();
}
else error(5);
} while (sym=="ident");
}
if (sym=="varsym"){
getsym();
do{
vardeclaration(&dx,&tx,lev);
while (sym=="comma"){
getsym();
vardeclaration(&dx,&tx,lev);
}
if (sym=="semicolon") getsym();
else error(5);
} while (sym=="ident");
}
while (sym=="procsym"){
getsym();
if (sym=="ident"){
enter("procedure",&dx,&tx,lev);
getsym( );
}
else error(4);
if (sym=="semicolon") getsym( );
else error(5);
insert("semicolon",fsys,&fsys1);
// "semicolon" and the fsys now and front the follow sym
block(lev+1,tx,fsys1);
if (sym=="semicolon")
{
getsym();
file://ident and procsym can follow "declbegsys
insert("ident",statbegsys,&fsys1);
insert("procsym",fsys1,&fsys1);
test(fsys,fsys1,6);
}
else error(5);
}
insert("ident",statbegsys,&fsys1);
test(fsys1,declbegsys,7);
} while (in(sym,declbegsys));
code[table[tx0].adr].a=cx;
table[tx0].adr=cx;
table[tx0].size=dx;
cx0=cx;
gen("int",0,dx);
insert("semicolon",fsys,&fsys1);
insert("endsym",fsys1,&fsys1);
statement(fsys1,tx,lev);
gen("opr",0,0);
file://test(fsys,fsys,8);
listcode(cx0);
}
/*-----------------------------------statement (fsys, tx, lev)---------------------*/
void statement(symset fsys,int tx,int lev)
{
symset fsys1;
int i,cx1,cx2;
if (sym=="ident")
{
i=position(id,tx);
if (i==0){
cout<<"error 11\n";
error(11);
}
else
if (table[i].kind!="variable")
{
error(12);
i=0;
}
file://i=ifarr(fsys,tx,lev,i); file://add
getsym( );
if (sym=="becomes")
getsym();
else
error(13);
expression(fsys,tx,lev);
if (i!=0)
gen("sto",lev-table[i].level,table[i].adr);
}
else
if (sym=="readsym")
{
getsym( );
if (sym!="lparen")
error(34);
else
do
{
getsym ( );
if ( sym=="lparen")
error (34);
if (sym=="ident")
i=position(id,tx);
else
i=0;
if (i==0)
error(35);
else
{
file://i=ifarr(fsys,tx,lev,i); file://add
gen("opr",0,16);
gen("sto",lev-table[i].level,table[i].adr);
}
getsym( );
} while (sym=="comma");
if (sym!="rparen")
{
error(33);
while (in(sym,fsys)==0)
getsym( );
}
else
getsym();
}
else
if (sym=="writesym")
{
getsym();
if (sym=="lparen")
{
do
{
getsym( );
insert("rparen",fsys,&fsys1);
insert("comma",fsys1,&fsys1);
expression(fsys1,tx,lev);
gen("opr",0,14);
} while (sym=="comma");
if (sym!="rparen")
error(33);
else
getsym( );
}
gen("opr",0,15);
}
else
if (sym=="callsym")
{
getsym( );
if (sym!="ident")
error(14);
else
{
i=position(id, tx);
if (i==0)
error(11);
else
if (table[i].kind=="procedure")
error(15);
else
gen("cal",lev-table[i].level,table[i].adr);
getsym( );
}
}
else
if (sym=="ifsym")
{
getsym( );
insert("thensym",fsys,&fsys1);
condition(fsys1,tx,lev);
if (sym=="thensym")
getsym( );
else
error(16);
cx1=cx;
gen("jpc",0,0);
file://insert("elsesym",fsys,&fsys1);//add
statement(fsys,tx,lev);
file://if (sym=="elsesym")!=0)//add
code[cx1].a=cx;
/*else
{
cx2=cx;//
gen("jmp",0,0);//
code[cx1].a=cx;//
getsym();//
statement(fsys,tx,lev);//
code[cx2].a=cx;//
}*/
}
else
if (sym=="beginsym")
{
getsym( );
insert("semicolon",fsys,&fsys1);
insert("endsym",fsys1,&fsys1);
statement(fsys1,tx,lev);
while (in(sym,statbegsys) || (sym=="semicolon"))
{
if (sym=="semicolon")
getsym( );
else
error(10);
statement(fsys1,tx,lev);
}
if (sym=="endsym")
getsym( );
else
error(17);
}
else
if (sym=="whilesym")
{
cx1=cx;
getsym( );
insert("dosym",fsys,&fsys1);
condition(fsys1,tx,lev);
cx2=cx;
gen("jpc",0,0);
if (sym=="dosym")
getsym( );
else
error(18);
statement(fsys,tx,lev);
gen("jmp",0,cx1); /* has been changed */
code[cx2].a=cx;
}
file://add begin
else
if (sym=="repeatsym")
{
getsym();
cx1=cx;
insert("semicolon",fsys,&fsys1);
insert("untilsym",fsys1,&fsys1);
statement(fsys1,tx,lev);
while (in(sym,statbegsys)||(sym=="semicolon"))
{
if (sym=="semicolon")
getsym();
else
error(60);
statement(fsys1,tx,lev);
}
if (sym=="untilsym")
getsym();
else
error(61);
insert("semicolon",fsys,&fsys1);
condition(fsys1,tx,lev);
gen("jpc",0,cx1);
}
file://add end
test(fsys,fsys,19);
}
/*----------------------condition (fsys, tx, lev)----------------------*/
void condition(symset fsys,int tx,int lev)
{
string relop;
symset fsys1;
if (sym=="oddsym")
{
getsym( );
expression(fsys,tx,lev);
gen("opr",0,6);
}
else
{
insert("eql",fsys,&fsys1);
insert("neq",fsys1,&fsys1);
insert("lss",fsys1,&fsys1);
insert("leq",fsys1,&fsys1);
insert("gtr",fsys1,&fsys1);
insert("geq",fsys1,&fsys1);
expression(fsys1,tx,lev);
if ((sym!="eql") && (sym!="neq")
&& (sym!="lss") && (sym!="leq")
&&(sym!="gtr") && (sym!="geq"))
error(20);
else
{
relop=sym;
getsym( );
expression(fsys,tx,lev);
if (relop=="eql") gen("opr",0,8);
if (relop=="neq") gen("opr",0,9);
if (relop=="lss") gen("opr",0,10);
if (relop=="leq") gen("opr",0,11);
if (relop=="gtr") gen("opr",0,12);
if (relop=="geq") gen("opr",0,13);
}
}
}
/*-------------------expression (fsys, tx, lev)----------------------*/
void expression(symset fsys,int tx,int lev)
{
string addop;
symset fsys1;
insert("plus",fsys,&fsys1);
insert("minus",fsys1,&fsys1);
if ((sym=="plus") || (sym=="minus"))
{
addop=sym;
getsym( );
term(fsys1,tx,lev);
if (addop=="minus")
gen("opr",0,1);
}
else term(fsys1,tx,lev);
while ((sym=="plus") || (sym=="minus"))
{
addop=sym;
getsym( );
term(fsys1,tx,lev);
if (addop=="plus")
gen("opr",0,2);
else
gen("opr",0,3);
}
}
/*-----------------term (fsys, tx, lev)-----------------------*/
void term(symset fsys,int tx,int lev)
{
string mulop;
symset fsys1;
insert("times",fsys,&fsys1);
insert("slash",fsys1,&fsys1);
factor(fsys1,tx,lev);
while ((sym=="times") || (sym=="slash"))
{
mulop=sym;
getsym( );
factor(fsys1,tx,lev);
if (mulop=="times")
gen("opr",0,4);
else gen("opr",0,5);
}
}
/*------------------factor (fsys, tx, lev)--------------------*/
// Int ifarr(fsys,tx,lev,tt);
void factor(symset fsys,int tx,int lev)
{
symset fsys1;
int i;
test(facbegsys,fsys,24);
while (in(sym,facbegsys)==1)
{
if (sym=="ident")
{
i=position(id,tx);
if (i==0) error(11);
else
{
if (table[i].kind=="constant")
gen("lit",0,table[i].val);
if (table[i].kind=="variable")
{
file://i=ifarr(fsys,tx,lev,i); file://add
gen("lod",lev-table[i].level,table[i].adr);
}
if (table[i].kind=="procedure")
error(21);//getsym( );
}
getsym();//
}
else if (sym=="number")
{
if (num>amax)
{
error(31);
num =0;
}
gen("lit",0,num);
getsym( );
}
else if (sym=="lparen")
{
getsym();
insert("rparen",fsys,&fsys1);
expression(fsys1,tx,lev);
if (sym=="rparen")
getsym();
else error(22);
}
/* { if (num>amax)
{ error (31);
num=0;
}
gen ("lit",0, num);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -