📄 pl0.h
字号:
/**************************************************************************
* PL/0 语言编译程序,由江汉石油学院计算机科学系周云才根据清华大学出版社出版,
* 吕映芝,张素琴,蒋维杜编写的教材《编译原理》中第二章(PL/0编译程序的实现)
* 以及附录A中的代码改编而成。
* 代码版权由周云才拥有,使用者必须遵循以下约定:
* 可以免费使用此文件,但必须包含此声明。
* 可以修改、传播、打印、出版这里的源代码。
* 可以在任何软件工程中使用这里的源代码。
* 周云才对于由此源代码的使用而引起的任何问题没有任何责任。
* 周云才地址:湖北省荆州市江汉石油学院计算机科学系
* 邮编:434023
* 信箱:zyc262@163.net
* 电话: 0716-8431262
**********************************************************************************************/
#include <stdio.h>
#include <set>
#include <string>
#include <iostream>
#include <iostream>
#include <vector>
#ifndef WIRTH_ZYC_
#define WIRTH_ZYC_
using namespace std;
const int norw=13;//no. of reserved words
const int txmax=100;//length of identifier table
const int al=10;//length of identifiers
const int nmax=14;// max. no. of digits in numbers
const int amax=2047;//maximum address
const int levmax=3;// maximum depth of block nesting
const int cxmax=200;// size of code array
const int lineLength=82;
typedef enum {NUL,IDENT,NUMBER,PLUS,MINUS,TIMES,SLASH,ODDSYM,EQL,NEQ,LSS,LEQ,GTR,GEQ,LPAREN,RPAREN,COMMA,SEMICOLON,PERIOD,BECOMES,BEGINSYM,ENDSYM,IFSYM,THENSYM,WHILESYM,WRITESYM,READSYM,DOSYM,CALLSYM,CONSTSYM,VARSYM,PROCSYM} symbol;
typedef char alfa[al+1];
typedef enum{CONSTANT,VARIABLE,PROCEDURE}obj0;
typedef enum {LIT,OPR,LOD,STO,CAL,INT,JMP,JPC} fct;//functions
typedef set<symbol> symset;
struct instruction{
fct f;//function code
int l;//level,cann't big than levmax
int a;//displacement address,cann't big than amax
};
/*******************************************
* lit 0,a: load constant a *
* opr 0,a: execute operation a *
* lod l,a: load variable l,a *
* sto l,a: store variable 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 *
*******************************************/
typedef struct{
alfa name;
obj0 kind;
union {
struct{int level,adr,size;}inOther;
int val;
}other;
} Table;
class PL0
{
protected:
bool listswitch,sourceEnd;
char ch;// last character read
symbol sym; //last symbol read
alfa id; // last identifier read
int num;//last number read
int cc; //character count
int ll; //line length
int kk,err;
int cx; //code allocation index
int codeNo;//code line no.
static string errStr[];//error string
char line[lineLength];//code line
vector<string> errorString;//error array
alfa a;
instruction code[cxmax+1];//destination code array
alfa word[norw+1];
symbol wsym[norw+1];
symbol ssym[100];
char mnemonic[8][6];
symset declbegsys,statbegsys,facbegsys;
Table table[txmax+1];
FILE* fin,*fout;
public:
void SaveCode();
PL0(char* source,char*destination);
~PL0(){fclose(fin),fclose(fout);}
void listcode(int cx0);
void error(int n);
void getsym();
void getch();
void gen(fct x,int y,int z);
void test(symset s1,symset s2,int n);
void block(int lev,int tx,symset fsys);
void enter(obj0 k,int &tx,int &dx,int lev);
int position(alfa id,int tx);
void constdeclaration(int&tx,int&dx,int lev);
void vardeclaration(int&tx,int&dx,int lev);
void factor(symset fsys,int tx,int lev);
void term(symset fsys,int tx,int lev);
void expression(symset fsys,int tx,int lev);
void condition(symset fsys,int tx,int lev);
void statement(symset fsys,int tx,int lev);
int base(int l,int b,int s[]);
void interpret();
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -