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

📄 table.h

📁 pascal编译器
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef _TABLE_H
#define _TABLE_H

#include <stdio.h>
#include "literal.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>

#define LENS sizeof (struct symbol_table);	//结构体符号表长度,为动态生成链表准备
#define LENL sizeof (struct label_stream);	//结构体记号流长度,为动态生成链表准备
#define	MAX	1000
#define ERRORSTACKNUMBER 400
int NowState=0;
int parseState;

struct symbol_table{						//定义符号表xt
	char	name[63];						//id标识
	int		id_type;							//id内容
	int		id_type_type;				//0基本,1复合
	int		function_type;				//0变量,1函数
	int		function_para;				//函数参数类型
	int		Ln;								//行号
	struct	symbol_table *next;
	struct  symbol_table *func_stack;
};													

struct label_stream{						//定义记号流
	int		label_type;	 						//记号类型
	struct	symbol_table *enter;			//若为id或num记录其在符号表内的地址
	struct	label_stream *next;
	int		lnum;							//linenum;
};

typedef struct symbol_table S_ListNode;
typedef S_ListNode *S_ListNodePtr;
typedef struct label_stream L_ListNode;
typedef L_ListNode *L_ListNodePtr;


S_ListNodePtr Shead;	//符号表头指针
L_ListNodePtr Lhead;	//记号流头指针
S_ListNodePtr SFuncHead;//记录函数起始地址

int notes,array_state;
int func_id,func_var,func_match;
int nodenum;

void initWord(){					//初始化符号表与记号流的指针头
	Shead = new struct symbol_table;
	Shead->next = NULL;
	Lhead = new struct label_stream;
	Lhead->next = NULL;
}
int Samestr (char str1[63],char str2[63]) //  判断字符串是否相等
{
int i;
for (i = 0;i < 64; i++)
{
	if (str1[i] != str2[i])
		return (-1);
	if (str1[i] == '#' && str2[i] == '#')
		break;
}
return (0);
}
void inserts (S_ListNodePtr sPtr,char names[63],int linenum,int wlength,L_ListNodePtr enter)
//参数意义(按序)符号表尾指针,符号名称,符号的行号或类型号
//决定前一参数付值行数域或类型域(1行数域,0类型域).
//插入符号表
{ 
	S_ListNodePtr  S_newPtr,S_previousPtr, S_currentPtr;
	L_ListNodePtr  L_adds;
	int i;
	int same,find;
	L_adds = enter;
	S_newPtr = new struct symbol_table;
	for (i = 0; i <= 64;i++)
	{
		S_newPtr->name[i] = '#';
	}
	if ( S_newPtr != NULL ) 
	{     /* is space available */
		i = 0;
		while (names[i] != '#')
		{
			S_newPtr->name[i] = names[i];
			i++;
		}
		if (func_var == 0)
		{
			S_newPtr->next = NULL;
			S_newPtr->Ln = linenum;
			S_previousPtr = sPtr;
			S_currentPtr = sPtr->next;
			find = 0;
			while ( S_currentPtr != NULL ) 
			{
				i=0;	  
				if(S_currentPtr == NULL)
				{
					same = -1; 
				}
				else
				{
					same = Samestr (S_currentPtr->name,S_newPtr->name);
				}
				if (same != 0)
				{
					S_previousPtr = S_currentPtr;          /* walk to ...   */
					S_currentPtr = S_currentPtr->next;  /* ... next node */
				}
				else
				{
					L_adds->enter = S_currentPtr; 
					find = 1;
					break;
				}
			}
			if (find == 0)
			{		
				S_previousPtr->next = S_newPtr;
				S_newPtr->next = NULL;
				L_adds->enter = S_newPtr;
			}
			if (func_id == 1)
			{
				SFuncHead = S_newPtr;
				SFuncHead->func_stack = NULL;		
			}
		}
		else
		{
			S_newPtr->next = NULL;
			S_newPtr->Ln = linenum;
			S_previousPtr = sPtr;
			S_currentPtr = sPtr;
			find = 0;
			while ( S_currentPtr != NULL ) 
			{
				i=0;	  
				if(S_currentPtr == NULL)
				{
					same = -1; 
				}
				else
				{
					same = Samestr (S_currentPtr->name,S_newPtr->name);
				}
				if (same != 0)
				{
					S_previousPtr = S_currentPtr;          /* walk to ...   */
					S_currentPtr = S_currentPtr->func_stack;  /* ... next node */
				}
				else
				{	 
					L_adds->enter = S_currentPtr; 
					find = 1;
					break;
				}
			}
			if (find == 0)
			{	
				S_previousPtr->func_stack = S_newPtr;
				S_newPtr->func_stack = NULL;
				L_adds->enter = S_newPtr;
			}
		}
	}
	else
		printf( "%c not inserted. No memory available.\n", linenum );
}

void insertl (L_ListNodePtr sPtr,int x,int adds,char name[63],int linenum,int wlength)
//参数意义(按序)记号流尾指针,记号类型,是否加入符号表(1加入,0不加入)
//在符号表中名称,在符号表中的行数,前一参数付值于行数域或是类型域(1行数域,0类型域)
//插入记号流
{ 
   L_ListNodePtr L_newPtr, L_previousPtr,L_currentPtr;  
   L_newPtr = new struct label_stream;
   if ( L_newPtr != NULL ) 
   {     /* is space available */
      L_newPtr->label_type = x;
      L_newPtr->next = NULL;
	  L_newPtr->lnum = linenum;
	  L_previousPtr = NULL;
      L_currentPtr = sPtr;
	if (adds == 1) 
	{
		if ((func_id == 1) && (func_var == 1))
			inserts(SFuncHead,name,linenum,wlength,L_newPtr);
		else
		{
			inserts(Shead,name,linenum,wlength,L_newPtr);
			if (func_id == 1)
				func_var = 1;
		}
	}	
	while ( L_currentPtr != NULL ) { 
         L_previousPtr = L_currentPtr;          /* walk to ...   */
         L_currentPtr = L_currentPtr->next;  /* ... next node */
      }

         L_previousPtr->next = L_newPtr;
         L_newPtr->next = NULL;
   }
   else
      printf( "%c not inserted. No memory available.\n", x );
}


void clear (char sword[63])	//初始化记录字
{
int i;
for (i = 0;i <= 63;i++)
	sword[i] = '#'; 
}
int oftype (char currentchar,char nextchar,char sword[63],int wlength,int result)  //识别读入字符
{
int i,flag;
	result = -1;
	switch (currentchar)
	{
	case '+' : 
		result = op_add;
		return(result);
		break;		//+
	case '-' : 
		result = op_sub;
		return(result);
		break;		//-
	case '*' : 
		result = op_mul;
		return(result);
		break;		//*
	case '/' : 
		result = op_div;
		return(result);
		break;		// /
	case ';' : 
		result = sem;
		return(result);
		break;		//;
	case '(' :
		result = lpare;
		return(result);
		break;		//(
	case ')' : 
		result = rpare;
		return(result);
		break;		//)
	case '{' : 
		result = lnote;
		notes = 0;   //开始注释跳过.
		return(result);
		break;		//{
	case '}' : 
		result = rnote;
		notes = 1;   //结束注释,继续接收
		return(result);
		break;		//}
	case ',' :
		result = comma;
		return (result);
		break;
	case '[' :
		array_state = 1;
		result = lqpare;
		return (result);
		break;
	case ']':
		nodenum = 0;
		array_state = 0;
		result = rqpare;
		return (result);
		break;
	case '=':
		if (wlength == 0)
		{
		result = op_equ;
		return (result);
		
		}
		break;
	case ':' :
		if (nextchar != '='){
		result = colon;
		return (result);}
		break;
	}
	if (wlength == 0)
	{
		if ((sword[0] == '<') && !(isalnum(nextchar)))
		{
			result = op_ls;
			return (result);
		} // <
		if ((sword[0] == '>') && !(isalnum(nextchar)))
		{
			result = op_gr;
			return (result);
		} // >
	} //end_wlength_0
	if (array_state == 1)
	{
		if (currentchar == '.')
		{
			nodenum++;
			result = node;
			return (result);
		}
		if ((isdigit(currentchar)) && (nextchar =='.'))
		{
			result = rw_digits;
			return (result);
		}

⌨️ 快捷键说明

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