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

📄 pl.h

📁 实现pl0编译 能够实现简单的编译 满足编译原理的要求
💻 H
字号:
#define keynum 42    //the number of the keyword
#define nummax 14      //the number of a digit 
#define maxlen 10     //the max length of a keyword
#define maxline 81     //the max length of a line
#define errornum 14
#define maxlev 3
#define amax 2047
#define codenum 10
typedef enum
{
	false,
	true
} bool;

/**
the fellow should have other define items and 
should be added later.
*/
enum symbolset{         //the set of the symbol
	nul,                //mean null;
	ident,              //标志符;
	number,             //数字;
	plus,               //+;
	minus,              // -;
	multi,				// *
	divid,				// /;
	eql,                // =;
	neql,               // !=;<>
	less,				// <;
	leql,				// <=;
	grt,                // >;
	geql,               // >=;
	lparen,             // (;
	rparen,             // );
	comma,				// ,;
	semicolon,			// ;
	squot,			// '
	period,// .
	colon,               // :
	assign,              // :=;
	forsym,              // symbol of for;
	ifsym,               // symbol of if;
	thensym,             // symbol of then;
	elsesym,            // symbol of else;
	casesym,             // symbol of case;
	ofsym,				// symbol of of;
	downtosym,           // symbol of downto;
	tosym,              // symbol of to;
	dosym,               // symbol of do;
	beginsym,            // symbol of begin;
	endsym,				// symbol of end;
	readsym,				// symbol of read
	writesym,           // symbol of write
	quote,				// left quote
	varsym,				//var
	realsym,			//the real
	constsym,			//the const
	charsym,			//the char
	functionsym,			//the function
	proceduresym,		//the procedure
	integersym,
	stringsym
};
enum typeset{            // the set of the types;
	constant,				// constant;
	var,				// variable;
	procedure,			// procedure;
	function,			// function;
	integer,            //  int ;
	plchar,              // char,/
	real,	           // float;
	string,
	parameter

};
enum vmcode{			//the code of the virtual machine
	lit,				//
	opr,				//operator
	lod,				//load
	sto,				//store
	cal,				//call?
	inte,				//assign the mem
	jmp,				//jump
	jpc,				//jump when =
	red,				//read
	wrt					//write
};
struct instruction
{
	enum vmcode vc;		//the virtual code
	int l;				//the level
	int a;				//constant
	double d;           //the double value
	char c;  //the plchar
	int flag;//identify the type of the var
};
struct tablestruct		// 符号表结构定义
{
	char name[maxlen];//the name
	enum typeset kind;//the type set 
	int val_int;//the value of the number ,for the constant
	double val_real;//real
	int level;//the level of the type of kind
	int adr;//the address;
	int size;//the volume of the space need to beed assigned
	//下面几个定义与常量扩充有关
	char string[50];		//string
	char c;				//char 
	enum typeset type;//the type
	int paranum;  //the num of the function 
};
struct datastack
{
	int int_val;
	int real_val;
	char string[50];
};
typedef struct para
{//部分成员与常量有关;
	int i;
	double d;
	char c;
	char s[50];
}p;
//虚拟机栈类型
typedef struct stacktable
{
	int i;//for the integer
	char c;//for the char;
	char s[50];//for the string;
	double d;//for the real;
	struct datastack para[10];
} s;
//end of the it

struct tablestruct table[1024];//the table
//将符号表定义为一个结构的数组,
struct para pa;//vip
char errorset[errornum][80];
int cc;                 // the current position of the char getted;
int ll;
int linenum;
int num;          ///the value of the number read
int cx;				//the max code lines of the vm
int nerror;           //the number of the errors in all
double m;				//for the real
double fnum;      //the value of the real value the ch read   
char ch;                 //the length of the line read;
char id[maxlen+1];		//the symbol buffer
char a[maxlen+1];		//a momentary buffer		
//char *sym;                //the symbol;
char keyword[keynum][maxlen];
char line[maxline];
char charcode[codenum][5];//only five chars only to save space (max length is fout +'\0')
FILE *fin;
FILE *fout;
FILE *ftab;
FILE *fcode;
char char_buffer;//store the char read;
char char_buffer;//store the char read;
char string_buffer[20];//store the string read
enum symbolset keysym[keynum];
enum symbolset sym;
enum symbolset symset[256];
enum symbolset ssym[256];
struct instruction code[500];
//virtual machine code instruction
//some marco
#define getchdo if(-1==getch()) return -1
#define getsymdo if(-1==getsym()) return -1
//宏定义
// 以简化do_deal_item
#define do_deal_expression(a,b,c)  if(-1==deal_expression(a,b,c)) return -1
#define do_deal_term(a,b,c) if(-1==deal_term(a,b,c)) return -1
#define do_deal_factor(a,b,c) if(-1==deal_factor(a,b,c)) return -1
#define do_deal_condition(a,b,c) if(-1==deal_condition(a,b,c)) return -1
#define do_deal_var(a,b,c) if(-1==deal_var(a,b,c)) return -1
#define do_deal_constant(a,b,c) if(-1==deal_constant(a,b,c)) return -1
#define do_deal_statement(a,b,c) if(-1==deal_statement(a,b,c)) return -1
#define do_test(a,b,c) if(-1==test(a,b,c)) return -1
#define do_genecode(a,b,c,d) if(-1==genecode(a,b,c,d)) return -1



int genecode(enum vmcode x,int y,struct para p,enum typeset t);
void filltable(enum typeset k,int *ptx,int lev,int *pdx,enum typeset t );
int deal_expression(bool *fsys,int *ptx,int lev);
int deal_term(bool *fsys,int *ptx,int lev);
int deal_factor(bool *fsys,int *ptx,int lev);
int deal_condition(bool *fsys,int *ptx,int lev);
int deal_var(int *ptx,int lev,int *pdx);
int deal_constant(int *ptx,int lev,int *pdx);
int deal_statement(bool *fsys,int *ptx,int lev);
int test(bool *seta,bool *setb,int n);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -