⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pl0.h

📁 PL/0语言编译器的实现
💻 H
字号:
//PL/0编译系统源程序
//C++
//34211431 高超
//2007.5.7

#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;//PCODE
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
};

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