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

📄 symtab.cpp

📁 完成一个简化的C语言编译程序
💻 CPP
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symtab.h"


#define SHIFT 4

static int hash(char* key)
{
  int temp=0;
  int i=0;
  while(key[i]!='\0')
  {
       temp=((temp<<SHIFT)+key[i])%SIZE;
	   ++i;
  }
  return temp;
}

void st_insert(char* name,ExpType type,bool isfunc, int lineno, int loc,tableRec* p)
{
     int h=hash(name);	     
	 BucketList l=p->table[h];
	 while((l!=NULL)&&(strcmp(name,l->name)!=0))
		  l=l->next;
	 if(l==NULL)
	 {
	      l=new BucketListRec;
		  l->name=name;
		  l->type=type;
		  l->func=isfunc;
		  l->lines=new LineListRec;
		  l->lines->lineno=lineno;
		  l->memloc=loc;
		  l->lines->next=NULL;
		  l->next=p->table[h];
		  p->table[h]=l;
	 }
	 else
	 {
	      LineList t=l->lines;
		  while(t->next!=NULL) t=t->next;
		  t->next=new LineListRec;
		  t->next->lineno=lineno;
		  t->next->next=NULL;
	 }
} /*st_insert*/


int st_lookup(char* name,tableRec* p,ExpType &type,int& memloc)
{
	 tableRec* parent;

     int h=hash(name);
	 BucketList l=p->table[h];
	 while((l!=NULL)&&(strcmp(name,l->name)!=0))
		 l=l->next;
	 if(l!=NULL)
     {
		 type=l->type;
		 memloc=l->memloc;
		 return 0;
	 }
	 else
	 {
	    parent=p->parent;
		while(parent!=NULL)
		{
			l=parent->table[h];
			while((l!=NULL)&&(strcmp(name,l->name)!=0))
		             l=l->next;
	       if(l!=NULL)
		   {
		      type=l->type;
		      memloc=l->memloc;
		      return 1;
		   }
		        
		   parent=parent->parent;
		}
		if(l==NULL)
		   return -1;
	 }
	 
}

int st_lookup(char* name,tableRec* p)
{
	 tableRec* parent;
     int h=hash(name);
	 BucketList l=p->table[h];
	 while((l!=NULL)&&(strcmp(name,l->name)!=0))
		 l=l->next;
	 if(l!=NULL)
     {
		 return 0;
	 }
	 else
	 {
	    parent=p->parent;
		while(parent!=NULL)
		{
			l=parent->table[h];
			while((l!=NULL)&&(strcmp(name,l->name)!=0))
		             l=l->next;
	       if(l!=NULL)
		   {
		      return 1;
		   }
		        
		   parent=parent->parent;
		}
		if(l==NULL)
		   return -1;
	 }
	 
}



void printSymTab(FILE* listing,tableRec* p)
{
     int i,j=0;
	 static bool first=true;
	 static int layerno=0;
	 if(first)
	 {
	 fprintf(listing,"Variable Name Location Line Numbers Type\n");
	 fprintf(listing,"-----------------------------------\n");
	 fprintf(listing,"\n---layerno: %d\n",++layerno);
	 first=false;
	 }
	 if(p!=NULL)
	 {
     for(i=0;i<SIZE;++i)
	 {
	          if(p->table[i]!=NULL)
			  {
			       BucketList l=p->table[i];
				   while(l!=NULL)
				   {
				        LineList t=l->lines;
						fprintf(listing,"%-14s ",l->name);
						fprintf(listing,"%-8d  ",l->memloc);
						while(t!=NULL)
						{
							 fprintf(listing,"%4d ", t->lineno);
							 t=t->next;
						}
						fprintf(listing,"%4d",l->type);
						fprintf(listing,"\n");
						l=l->next;

				   }
			  }
	}
	tableRec* nexttoprint=p->sibling;
	while(nexttoprint!=NULL)
	{
		fprintf(listing,"\n");
	    printSymTab(listing,nexttoprint);
		nexttoprint=nexttoprint->sibling;
	}
	if(p->child!=NULL)
	{
	fprintf(listing,"\n---layerno: %d\n",++layerno);
	printSymTab(listing,p->child);
    }
	}

}

⌨️ 快捷键说明

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