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

📄 pascal.cpp

📁 《编译原理实践教程》中的程序作了一些扩展
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Pascal.cpp : 定义控制台应用程序的入口点。
//

#include	<stdio.h>
#include   	<string.h>
#include	<stdlib.h>

#define ISLETTER(c)	((c)>='a'&&(c)<='z')//判断是否是字母
#define ISNUMBER(c)	((c)>='0'&&(c)<='9')	//判断是否是数字
#define ISCHAR(c)	(((c>='a')&&(c<='z'))||((c>='0')&&(c<='9')))	//判断是否是字符

#define	ACC		-2
/****************************************/
#define sy_if		0
#define sy_then		1
#define sy_else		2
#define sy_while	3
#define sy_begin	4
#define sy_do		5
#define sy_end		6
#define a			7
#define semicolon	8
#define e			9
#define sharp		10
#define S			11
#define L			12

#define tempsy      15
#define EA			18 //E and
#define EO			19 //E or

#define plus		34
#define subtract	35
#define times		36
#define	divide		37
#define becomes		38

#define op_and		39
#define op_or		40
#define op_not		41
#define rop			42

#define lparent		48
#define rparent		49
#define ident       56
#define intconst    57

/******************************************/
char	ch='\0';/*当前字符*/
int     count=0;
static	char	spelling[10]={""};/*存放识别的字*/
static	char	line[81]={""};/*一行字符缓冲区*/
char	*pline;/*字符缓冲区指针*/

static	char	varnametable[100][10];
int     varnametableptr=0;

struct	ntab
{
	int tc;
	int fc;
}ntab2[200];
int label=0;
/*存放临时变量的表的定义 */
struct	KEYWORD{
	char	spell[10];
	int		type;
};
/*存放文件的结构*/
KEYWORD keyword[10]={{"if",sy_if},
{"do",sy_do},
{"else",sy_else},
{"while",sy_while},
{"then",sy_then},
{"begin",sy_begin},
{"end",sy_end},
{"and",op_and},
{"or",op_or},
{"not",op_not}};
struct  aa{
	int type;
	int pos;
}buf[1000],/*词法分析结果缓冲区*/
g_nCurChar,/*当前字符*/
g_nExpCurChar,/*当前表达式中的字符*/
E,/*非终结符*/
sstack[100],/*符号栈*/
ibuf[100],
stack[1000];
struct  aa oth;
struct  fourexp{
	char op[10];
	struct aa arg1;
	struct aa arg2;
	int	result;
}fexp[200];
/*四元式的结构*/

int		ssp=0;/*指向sstack[100]*/
struct	aa	*pbuf=buf;/*指向词法分析缓冲区*/
int     lnum=0;/*源程序长度*/


FILE    *cfile;
/*********************************************************/
int newt=0;
/*临时变量*/
int nxq=100;
/*nxq指向下一个形成的四元式的地址*/
int	lr;
int lr1;
int sp=0;
/* 状态栈定义*/
int stack1[100];
int sp1=0;
/*状态栈1的定义*/
int num=0;
struct ll{
	int nxq1;
	int tc1;
	int fc1;
}labelmark[10];
int labeltemp[10];
int pointmark=-1,pointtemp=-1;
int sign=0;
/*sign=1,表达式为赋值语句;sign=2,表达式为布尔表达式。*/
/***********************************************/
static int	action[19][13]=
/*0*/		{{2,-1,-1,3,4,-1,-1,5,-1,-1,10,1,-1},
/*1*/		{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,ACC,-1,-1},
/*2*/		{-1,-1,-1,-1,-1,-1,-1,-1,-1,6,-1,-1,-1},
/*3*/		{-1,-1,-1,-1,-1,-1,-1,-1,-1,7,-1,-1,-1},
/*4*/		{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,8},
/*5*/		{-1,-1,104,-1,-1,-1,104,-1,104,-1,104,-1,-1},
/*6*/		{-1,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*7*/		{-1,-1,-1,-1,-1,11,-1,-1,-1,-1,-1,-1,-1},
/*8*/		{-1,-1,-1,-1,-1,-1,12,-1,-1,-1,-1,-1,-1},
/*9*/		{-1,-1,-1,-1,-1,-1,105,-1,13,-1,-1,-1,-1},
/*10*/		{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,14,-1},
/*11*/		{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,15,-1},
/*12*/		{-1,-1,103,-1,-1,-1,103,-1,103,-1,103,-1,-1},
/*13*/		{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,9,16},
/*14*/		{-1,-1,17,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*15*/		{-1,-1,102,-1,-1,-1,102,-1,102,-1,102,-1,-1},
/*16*/		{-1,-1,-1,-1,-1,-1,106,-1,-1,-1,-1,-1,-1},
/*17*/		{2,-1,-1,3,4,-1,-1,5,-1,-1,-1,18,-1},
/*18*/		{-1,-1,101,-1,-1,-1,101,-1,101,-1,101,-1,-1}};

static int action1[14][9]=
/*0*/		{{3,-1,-1,-1,-1,2,-1,-1,1},
/*1*/		{-1,4,11,5,10,-1,-1,ACC,-1},
/*2*/		{3,-1,-1,-1,-1,2,-1,-1,6},
/*3*/		{104,104,104,104,104,104,104,104,-1},
/*4*/		{3,-1,-1,-1,-1,2,-1,-1,7},
/*5*/		{3,-1,-1,-1,-1,2,-1,-1,8},
/*6*/		{-1,4,11,5,10,-1,9,-1,-1},
/*7*/		{101,101,101,5,10,101,101,101,-1},
/*8*/		{102,102,102,102,102,102,102,102,-1},
/*9*/		{103,103,103,103,103,103,103,103,-1},
/*10*/		{3,-1,-1,-1,-1,2,-1,-1,12},
/*11*/		{3,-1,-1,-1,-1,2,-1,-1,13},
/*12*/		{106,106,106,106,106,106,106,106,-1},
/*13*/		{105,105,105,5,10,105,105,105,-1}};

static int action2[16][11]=
/*0*/		{{1,-1,4,-1,5,-1,-1,-1,13,7,8},
/*1*/		{-1,2,-1,101,-1,101,101,101,-1,-1,-1},
/*2*/		{3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
/*3*/		{-1,-1,-1,102,-1,102,102,102,-1,-1,-1},
/*4*/		{1,-1,4,-1,5,-1,-1,-1,11,7,8},
/*5*/		{1,-1,4,-1,5,-1,-1,-1,6,7,8},
/*6*/		{-1,-1,-1,104,-1,9,10,104,-1,-1,-1},
/*7*/		{1,-1,4,-1,5,-1,-1,-1,14,7,8},
/*8*/		{1,-1,4,-1,5,-1,-1,-1,15,7,8},
/*9*/		{105,-1,105,-1,105,-1,-1,105,-1,-1,-1},
/*10*/		{107,-1,107,-1,107,-1,-1,107,-1,-1,-1},
/*11*/		{-1,-1,-1,12,-1,9,10,-1,-1,-1,-1},
/*12*/		{-1,103,-1,103,-1,103,103,103,-1,-1,-1},
/*13*/		{-1,-1,-1,-1,-1,9,10,ACC,-1,-1,-1},
/*14*/		{-1,-1,-1,106,-1,9,10,106,-1,-1,-1},
/*15*/		{-1,-1,-1,108,-1,9,10,108,-1,-1,-1}};
/****************从文件读一行到缓冲区******************************************/
void readline()
{
	char ch1;
	pline=line;
	ch1=getc(cfile);
	while (ch1!='\n')
	{
		*pline=ch1;
		pline++;
		ch1=getc(cfile);
	}
	*pline='\0';
	pline=line;
}
/*********************************从缓冲区读一个字符************************/
void readchar()
{
	if (ch=='\0') 
	{
		readline();
		lnum++;
	}
	ch=*pline;
	pline++;
}
/******************标志符和关键字的识别***************************************/
int find(char spell[])	//检查变量表中是否已存在该变量名
{
	bool bmatch = false;
	int i=0;
	while((bmatch ==false)&&(i<varnametableptr))
	{
		if (!strcmp(spell,varnametable[i])) 
		{
			bmatch=true;
		}
		i++;
	}
	if (bmatch==true) 
	{
		return i-1;
	}
	else 
	{
		return -1;
	}
}

void identifier()
{
	memset(spelling,0,10);
	
	bool bmatch = false;

	int k=0;
	do
	{
		spelling[k]=ch;
		k++;
		readchar();
	}while(ISCHAR(ch));//////////////////////////
	pline--;
	spelling[k]='\0';
	int i = 0;

	/*关键字匹配*/
	while((bmatch==false)&&(i<10))
	{
		if (strcmp(spelling,keyword[i].spell)==0) 
		{
			bmatch=true;
		}
		i++;
	}
	
	if(bmatch==true)  
	{
		buf[count].type=keyword[i-1].type;
	}
	else
	{
		buf[count].type=ident;
		int j=find(spelling);
		if (j==-1)
		{
			buf[count].pos=varnametableptr;
			strcpy(varnametable[varnametableptr],spelling);
			varnametableptr++;
		}
		else buf[count].pos=j;
	}
	count++;
}
/*****************数字的识别***********************************/
void number()
{
	int ivalue=0;
	int digit;
	do
	{
		digit=ch-'0';
		ivalue=ivalue*10+digit;
		readchar();
	}while(ISNUMBER(ch));///////////////////////////////
	buf[count].type=intconst;
	buf[count].pos=ivalue;
	count++;
	pline--;
}

/**************扫描主函数*************************************/
void scan()
{
	while(ch!='~')
	{
		switch (ch)
		{
		case ' ':
			break;
		case 'a':
		case 'b':
		case 'c':
		case 'd':
		case 'e':
		case 'f':
		case 'g':
		case 'h':
		case 'i':
		case 'j':
		case 'k':
		case 'l':
		case 'm':
		case 'n':
		case 'o':
		case 'p':
		case 'q':
		case 'r':
		case 's':
		case 't':
		case 'u':
		case 'v':
		case 'w':
		case 'x':
		case 'y':
		case 'z':
			identifier();
			break;
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			number();
			break;
		case '<':
			readchar();
			if(ch=='=')
			{
				buf[count].pos=0;
			}
			else
			{
				if(ch=='>') 
				{
					buf[count].pos=4;
				}
				else
				{
					buf[count].pos=1;
					pline--;
				}
			}
			buf[count].type=rop;
			count++;
			break;
		case '>':
			readchar();
			if(ch=='=')
			{
				buf[count].pos=2;
			}
			else
			{
				buf[count].pos=3;
				pline--;
			}
			buf[count].type=rop;
			count++;
			break;
		case '(':
			buf[count].type=lparent;
			count++;
			break;
		case ')':
			buf[count].type=rparent;
			count++;
			break;
		case '#':
			buf[count].type=sharp;
			count++;
			break;
		case '+':
			buf[count].type=plus;
			count++;
			break;
		case '-':
			buf[count].type=subtract;
			count++;
			break;
		case '*':
			buf[count].type=times;
			count++;
			break;
		case '/':
			buf[count].type=divide;
			count++;
			break;
		case ':':
			readchar();
			if (ch=='=')
				buf[count].type=becomes;
			count++;
			break;
		case '=':
			buf[count].type=rop;
			buf[count].pos=5;
			count++;
			break;
		case ';':
			buf[count].type=semicolon;
			count++;
			break;
		}
		readchar();
	}
	buf[count].type=-1;
}
/******************************************************************/
void readnu()
{
	if (pbuf->type>=0)
	{
		g_nCurChar.type=pbuf->type;
		g_nCurChar.pos=pbuf->pos;
		pbuf++;
	}
}
/******************中间变量的生成**************************/
int newtemp()
{
	newt++;
	return newt;
}
/**************************生成四元式****************/
int gen(char op1[],struct aa arg11,struct aa arg22,int result1)
{
	strcpy(fexp[nxq].op,op1);
	fexp[nxq].arg1.type=arg11.type;
	fexp[nxq].arg1.pos=arg11.pos;
	fexp[nxq].arg2.type=arg22.type;
	fexp[nxq].arg2.pos=arg22.pos;
	fexp[nxq].result=result1;
	nxq++;
	return nxq-1;
}
/**********布尔表达式的匹配****************/
int merg(int p1,int p2)
{
	int p;
	if(p2==0) return p1;
	else
	{
		p=p2;
		while(fexp[p].result!=0) p=fexp[p].result;
		fexp[p].result=p1;
		return p2;
	}
}
void backpatch(int p,int t)
{
	int tempq;
	int q;
	q=p;
	while(q!=0)
	{
		tempq=fexp[q].result;
		fexp[q].result=t;
		q=tempq;
	}
}
/*******************************************/
int change1(int chan)
{
	switch (chan)
	{
	case ident:
	case intconst:
		return 0;
	case plus:
		return 1;
	case subtract:
		return 2;
	case times:
		return 3;
	case divide:
		return 4;
	case lparent:
		return 5;
	case rparent:
		return 6;
	case sharp:
		return 7;
	case tempsy:
		return 8;
	}
}
int change2(int chan)
{
	switch (chan)
	{
	case ident:
	case intconst:
		return 0;
	case rop:
		return 1;
	case lparent:
		return 2;
	case rparent:
		return 3;
	case op_not:
		return 4;
	case op_and:
		return 5;
	case op_or:
		return 6;
	case sharp:
		return 7;
	case tempsy:
		return 8;
	case EA:
		return 9;
	case EO:
		return 10;
		//////////////////////
	case subtract:
		return 11;
	case divide:
		return 12;
		//////////////////////
	}
}
/************赋值语句的的分析***************************/
void lrparse1(int num)

⌨️ 快捷键说明

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