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

📄 analyze.cpp

📁 pascal的编译器 交作业没问题
💻 CPP
字号:
#include "stdio.h"
#include "string.h"
#include "Analyze.h"

Analyze::Analyze()
{
}
void Analyze::Init(FILE *listing)
{
	this->listing=listing;
    location=0;
	Error=FALSE;
	for(int i=0;i<TABLESIZE;i++)
         hashTable[i]=NULL;
}
void Analyze::insertNode(TreeNode * t)  //在符号表里插入一term.
{ switch (t->nodekind)
  { case StmtK:
      switch (t->kind.stmt)
      { case AssignK:
        case ReadK:
          if (st_lookup(t->attr.name) == -1)
            st_insert(t->attr.name,t->lineno,location++);
          else
            st_insert(t->attr.name,t->lineno,0);
          break;
        default:
          break;
      }
      break;
    case ExpK:
      switch (t->kind.exp)
      { case IdK:
          if (st_lookup(t->attr.name) == -1)
            st_insert(t->attr.name,t->lineno,location++);
          else
            st_insert(t->attr.name,t->lineno,0);
          break;
        default:
          break;
      }
      break;
    default:
      break;
  }
}
void Analyze::traverse(TreeNode * t,int type)
{
	if (t != NULL)
	{
	  if(type==0)
	     insertNode(t);
      int i;
      for (i=0; i < MAXCHILDREN; i++)
        traverse(t->child[i],type);  
	  if(type==1)
         checkNode(t);
      traverse(t->sibling,type);
	}
}
 
void Analyze::buildSymtab(TreeNode * syntaxTree)
{
	traverse(syntaxTree,0);
    printSymTab(listing);
}

void Analyze::typeError(TreeNode * t, char * message)
{
	fprintf(listing,"Type error at line %d: %s\n",t->lineno,message);
    Error = TRUE;
}

 
void Analyze::checkNode(TreeNode * t)
{ switch (t->nodekind)
  { case ExpK:
      switch (t->kind.exp)
      { case OpK:
          if ((t->child[0]->type != Integer) ||
              (t->child[1]->type != Integer))
            typeError(t,"Op applied to non-integer");
          if ((t->attr.op == EQ) || (t->attr.op == LT))
            t->type = Boolean;
          else
            t->type = Integer;
          break;
        case ConstK:
        case IdK:
          t->type = Integer;
          break;
        default:
          break;
      }
      break;
    case StmtK:
      switch (t->kind.stmt)
      { case IfK:
          if (t->child[0]->type == Integer)
            typeError(t->child[0],"if test is not Boolean");
          break;
        case AssignK:
          if (t->child[0]->type != Integer)
            typeError(t->child[0],"assignment of non-integer value");
          break;
        case WriteK:
          if (t->child[0]->type != Integer)
            typeError(t->child[0],"write of non-integer value");
          break;
        case RepeatK:
          if (t->child[1]->type == Integer)
            typeError(t->child[1],"repeat test is not Boolean");
          break;
        default:
          break;
      }
      break;
    default:
      break;

  }
}

void Analyze::typeCheck(TreeNode * syntaxTree)
{
	traverse(syntaxTree,1);
}

//==============================================
void Analyze::st_insert( char * name, int lineno, int loc )
{ int h = hash(name);
  BucketList l =  hashTable[h];
  while ((l != NULL) && (strcmp(name,l->name) != 0))
    l = l->next;
  if (l == NULL) 
  { l = (BucketList) new(struct SymTermListRec);
    l->name = name;
    l->lines = (LineList) new(struct LineListRec);
    l->lines->lineno = lineno;
    l->memloc = loc;
    l->lines->next = NULL;
    l->next = hashTable[h];
    hashTable[h] = l; }
  else  
  { LineList t = l->lines;
    while (t->next != NULL) t = t->next;
    t->next = (LineList) new(struct LineListRec);
    t->next->lineno = lineno;
    t->next->next = NULL;
  }
}
//---------------------------------------
int Analyze::st_lookup ( char * name )
{ int h = hash(name);
  BucketList l =  hashTable[h];
  while ((l != NULL) && (strcmp(name,l->name) != 0))
    l = l->next;
  if (l == NULL) return -1;     //不存在,则返回-1。
  else return l->memloc;
}
//----------------------------------------
void Analyze::printSymTab(FILE * listing)
{ int i;
  fprintf(listing,"Variable Name  Location   Line Numbers\n");
  fprintf(listing,"-------------  --------   ------------\n");
  for (i=0;i<TABLESIZE;++i)
  { if (hashTable[i] != NULL)
    { BucketList l = hashTable[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,"\n");
        l = l->next;
      }
    }
  }
}
 //------------------------------------
int Analyze::hash ( char * key )      //哈希函数 
{ int temp = 0;
  int i = 0;
  while (key[i] != '\0')
  { temp = ((temp << 4) + key[i]) % TABLESIZE;
    ++i;
  }
  return temp;
}

⌨️ 快捷键说明

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