📄 pl0.c
字号:
/*
* PL/0 complier program for win32 platform (implemented in C)
*
* The program has been test on Visual C++ 6.0, Visual C++.NET and
* Visual C++.NET 2003, on Win98, WinNT, Win2000, WinXP and Win2003
*
* 使用方法:
* 运行后输入PL/0源程序文件?
* 回答是否输出虚拟机代码
* 回答是否输出名字表
* fa.tmp输出虚拟机代码
* fa1.tmp输出源文件及其各行对应的首地址
* fa2.tmp输出结?
* fas.tmp输出名字表
*/
#include <stdio.h>
#include "pl0.h"
#include "string.h"
/* 解释执行时使用的栈 */
#define stacksize 500
int main()
{
bool nxtlev[symnum];
printf("Input pl/0 file:");
scanf("%s", fname); /* 输入文件名 */
fin = fopen(fname, "r");
if (fin)
{
printf("List object code?(Y/N)"); /* 是否输出虚拟机代码 */
scanf("%s", fname);
listswitch = (fname[0]=='y' || fname[0]=='Y');
printf("List symbol table?(Y/N)"); /* 是否输出名字表 */
scanf("%s", fname);
tableswitch = (fname[0]=='y' || fname[0]=='Y');
fa1 = fopen("fa1.tmp", "w");
fprintf(fa1,"Input pl/0 file? ");
fprintf(fa1,"%s\n",fname);
init(); /* 初始化 */
err = 0;
cc = cx = ll = 0;
ch = ' ';
if(-1 != getsym())
{
fa = fopen("fa.tmp", "w");
fas = fopen("fas.tmp", "w");
addset(nxtlev, declbegsys, statbegsys, symnum);
nxtlev[period] = true;
if(-1 == block(0, 0, nxtlev)) /* 调用编译程序 */
{
fclose(fa);
fclose(fa1);
fclose(fas);
fclose(fin);
printf("\n");
return 0;
}
fclose(fa);
fclose(fa1);
fclose(fas);
if (sym != period)
{
error(9);
}
if (err == 0)
{
fa2 = fopen("fa2.tmp", "w");
interpret(); /* 调用解释执行程序 */
fclose(fa2);
}
else
{
printf("Errors in pl/0 program");
}
}
fclose(fin);
}
else
{
printf("Can't open file!\n");
}
printf("\n");
return 0;
}
/*
* 初始化
*/
void init()
{
int i;
/* 设置单字符符号 */
for (i=0; i<=255; i++)
{
ssym[i] = nul;
}
ssym['+'] = plus;
ssym['-'] = minus;
ssym['*'] = times;
ssym['/'] = slash;
ssym['('] = lparen;
ssym[')'] = rparen;
ssym['='] = eql;
ssym[','] = comma;
ssym['.'] = period;
ssym['#'] = neq;
ssym[';'] = semicolon;
/* 设置保留字名字,按照字母顺序,便于折半查找 */
strcpy(&(word[0][0]), "begin");
strcpy(&(word[1][0]), "call");
strcpy(&(word[2][0]), "const");
strcpy(&(word[3][0]), "do");
strcpy(&(word[4][0]), "end");
strcpy(&(word[5][0]), "if");
strcpy(&(word[6][0]), "odd");
strcpy(&(word[7][0]), "procedure");
strcpy(&(word[8][0]), "read");
strcpy(&(word[9][0]), "then");
strcpy(&(word[10][0]), "var");
strcpy(&(word[11][0]), "while");
strcpy(&(word[12][0]), "write");
/* 设置保留字符号 */
wsym[0] = beginsym;
wsym[1] = callsym;
wsym[2] = constsym;
wsym[3] = dosym;
wsym[4] = endsym;
wsym[5] = ifsym;
wsym[6] = oddsym;
wsym[7] = procsym;
wsym[8] = readsym;
wsym[9] = thensym;
wsym[10] = varsym;
wsym[11] = whilesym;
wsym[12] = writesym;
/* 设置指令名称 */
strcpy(&(mnemonic[lit][0]), "lit");
strcpy(&(mnemonic[opr][0]), "opr");
strcpy(&(mnemonic[lod][0]), "lod");
strcpy(&(mnemonic[sto][0]), "sto");
strcpy(&(mnemonic[cal][0]), "cal");
strcpy(&(mnemonic[inte][0]), "int");
strcpy(&(mnemonic[jmp][0]), "jmp");
strcpy(&(mnemonic[jpc][0]), "jpc");
/* 设置符号集 */
for (i=0; i<symnum; i++)
{
declbegsys[i] = false;
statbegsys[i] = false;
facbegsys[i] = false;
}
/* 设置声明开始符号集 */
declbegsys[constsym] = true;
declbegsys[varsym] = true;
declbegsys[procsym] = true;
/* 设置语句开始符号集 */
statbegsys[beginsym] = true;
statbegsys[callsym] = true;
statbegsys[ifsym] = true;
statbegsys[whilesym] = true;
/* 设置因子开始符号集 */
facbegsys[ident] = true;
facbegsys[number] = true;
facbegsys[lparen] = true;
}
/*
* 每次读一行,存入line缓冲区,line被getsym取空后再读一行
*
* 被函数getsym调用。
*/
int getch()
{
if (cc == ll)
{
if (feof(fin))
{
printf("program incomplete");
return -1;
}
ll=0;
cc=0;
printf("%d ", cx);
fprintf(fa1,"%d ", cx);
ch = ' ';
while (ch != 10) //10为换行符
{
if (EOF == fscanf(fin,"%c", &ch))
{
line[ll] = 0;
break;
}
printf("%c", ch);
fprintf(fa1, "%c", ch);
line[ll] = ch;
ll++;
}
printf("\n");
fprintf(fa1, "\n");
}
ch = line[cc];
cc++;
return 0; //0为读取成功
}
/*
* 词法分析,获取一个符号
*/
int getsym()
{
int i,j,k;
/* the original version lacks "\r", thanks to foolevery */
while (ch==' ' || ch==10 || ch==13 || ch==9) /* 忽略空格、换行、回车和TAB */
{
getchdo;
}
if (ch>='a' && ch<='z')
{ /* 名字或保留字以a..z开头 */
k = 0;
do {
if(k<al)
{
a[k] = ch;
k++;
}
getchdo;
} while (ch>='a' && ch<='z' || ch>='0' && ch<='9');
a[k] = 0;
strcpy(id, a);
i = 0;
j = norw-1;
do { /* 搜索当前符号是否为保留字 */
k = (i+j)/2;
if (strcmp(id,word[k]) == 0)
{
sym = wsym[k];
break;
}
if (strcmp(id,word[k]) < 0)
j = k - 1;
else
i = k + 1;
} while (i <= j);
if ( i > j)
{
sym = ident; /* 搜索失败则,是名字或数字 */
}
}
else
{
if (ch>='0' && ch<='9')
{ /* 检测是否为数字:以0..9开头 */
k = 0;
num = 0;
sym = number;
do {
num = 10*num + ch - '0';
k++;
getchdo;
} while (ch>='0' && ch<='9'); /* 获取数字的值 */
k--;
if (k > nmax)
{
error(30);
}
}
else
{
if (ch == ':') /* 检测赋值符号 */
{
getchdo;
if (ch == '=')
{
sym = becomes;
getchdo;
}
else
{
sym = nul; /* 不能识别的符号 */
}
}
else
{
if (ch == '<') /* 检测小于或小于等于符号 */
{
getchdo;
if (ch == '=')
{
sym = leq;
getchdo;
}
else
{
sym = lss;
}
}
else
{
if (ch=='>') /* 检测大于或大于等于符号 */
{
getchdo;
if (ch == '=')
{
sym = geq;
getchdo;
}
else
{
sym = gtr;
}
}
else
{
sym = ssym[ch]; /* 当符号不满足上述条件时,全部按照单字符符号处理 */
if (sym != period)
{
getchdo;
}
}
}
}
}
}
return 0;
}
/*
* 查找名字的位置.
* 找到则返回在名字表中的位置,否则返回0.
*
* idt: 要查找的名字
* tx: 当前名字表尾指针
*/
int position(char* idt, int tx)
{
int i;
strcpy(table[0].name, idt);
i = tx;
while (strcmp(table[i].name, idt) != 0)
{
i--;
}
return i;
}
/*
* 在名字表中加入一项
*
* k: 名字种类constant, variable, procedur
* ptx: 名字表尾指针的指针,为了可以改变名字表尾指针的值
* lev: 名字所在的层次,,以后所有的lev都是这样
* pdx: dx为当前应分配的变量的相对地址,分配后要增加1
*/
void enter(enum object k, int* ptx, int lev, int* pdx)
{
(*ptx)++;
strcpy(table[(*ptx)].name, id); /* 全局变量id中已存有当前名字的名字 */
table[(*ptx)].kind = k;
switch (k)
{
case constant: /* 常量名字 */
if (num > amax)
{
error(31); /* 数越界 */
num = 0;
}
table[(*ptx)].val = num;
break;
case variable: /* 变量名字 */
table[(*ptx)].level = lev;
table[(*ptx)].adr = (*pdx);
(*pdx)++;
break;
case procedur: /* 过程名字 */
table[(*ptx)].level = lev;
break;
}
}
/*
* 用数组实现集合的集合运算
*/
int inset(int e, bool* s)
{
return s[e];
}
int addset(bool* sr, bool* s1, bool* s2, int n)
{
int i;
for (i=0; i<n; i++)
{
sr[i] = s1[i]||s2[i];
}
return 0;
}
int subset(bool* sr, bool* s1, bool* s2, int n)
{
int i;
for (i=0; i<n; i++)
{
sr[i] = s1[i]&&(!s2[i]);
}
return 0;
}
int mulset(bool* sr, bool* s1, bool* s2, int n)
{
int i;
for (i=0; i<n; i++)
{
sr[i] = s1[i]&&s2[i];
}
return 0;
}
/*
* 测试当前符号是否合法
*
* 在某一部分(如一条语句,一个表达式)将要结束时时我们希望下一个符号属于某集合
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -