?? 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 + -