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

📄 cifafenxi.cpp

📁 SLR(0)语法分析的简单赋值语句的翻译程序,含词法分析,共800行
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<stack>
#include<list>
#include<cmath>
#include<vector>
#include<iomanip>
#define MAX 50
#define TRUE 1
#define FALSE 0
using namespace std;
typedef struct cl{
    float data;
	//float b;
	//char c[80];
	int add;
	int leibie;
	struct cl* next;
}*cllist;
typedef struct bl{
    char *name;
	int add;
	int leibie;
    struct bl* next;
}*bllist;
struct guanbaozi{
    char *zifu;
	int add;
	int leibie;
}guanbao[32]={{"char",0,1},{"short",1,1},{"int",2,1},{"unsigned",3,1},{"long",4,1},{"float",5,1},
{"double",6,1},{"struct",7,1},{"union",8,1},{"void",9,1},{"enum",10,1},{"signed",11,1},
{"const",12,1},{"include",13,1},{"typedef",14,1},{"auto",15,1},{"register",16,1},{"static",17,1},
{"extern",18,1},{"break",19,1},{"case",20,1},{"continue",21,1},{"default",22,1},{"do",23,1},{"else",24,1},
{"for",25,1},{"goto",26,1},{"if",27,1},{"return",28,1},{"switch",29,1},{"while",30,1},{"sizeof",31,1}};
struct yunsuanfu{
	char suan;
	int add;
	int leibie;
}wordyun[20]={{'+',0,2},{'-',1,2},{'/',2,2},{'*',3,2},{'%',4,2},{'=',5,2},{'>',6,2},{'<',7,2},{'^',8,2},
{'&',9,2},{'#',10,2},{'|',11,2},{'(',12,2},{')',13,2},{'[',14,2},{']',15,2},{'.',16,2},{'!',17,2},{'?',18,2},{':',19,2}};
struct jiefu{
	char jie;
	int add;
	int leibie;
}wordjie[7]={{'{',0,3},{'}',1,3},{'"',2,3},{';',3,3},{',',4,3},{'\'',5,3},{'\\',6,3}};

int pretable[5][9]={
      {1,0,0,0,0,1,0,0,0},
      {0,1,1,0,0,0,1,1,1},
      {1,0,0,0,0,1,0,0,0},
      {0,1,1,1,1,0,1,1,1},
      {1,0,0,0,0,1,0,0,0}};
int d=-1,b=-1;
void clcreatlist(cllist cll)
{           
  cll->add=d;
  cll->next=NULL;
}
int cllistinsert(cllist cll,float e)
{  
   cllist p,s;
   p=cll;
   while (p->next)  
       p=p->next;
   s=(cllist) malloc(sizeof(cl));
   s->data=e;
   s->add=++d;
   s->leibie=4;
   s->next=p->next;
   p->next=s;
   return TRUE;
}
int clfind(cllist cll,float e,int *a,int *l)
{
	cllist p;
	p=cll->next;
	while(p)
	{
		if(e==p->data)
		{
			*a=p->add;
		    *l=p->leibie;
			return TRUE;
		}
		p=p->next;
	}
	return  FALSE;
}
float clfind1(cllist cll,int a)
{
	cllist p;
	p=cll->next;
	while(p)
	{
		if(a==p->add)
		{
			return p->data;
		}
		p=p->next;
	}
	return 0.0;
}
void blcreatlist(bllist bll)
{           
  bll->next=NULL;
  bll->name=NULL;
  bll->add=b;
}
int bllistinsert(bllist bll,char* e,int i)
{  
   bllist p,s;
   p=bll;
   while (p->next)  
       p=p->next;
   s=(bllist) malloc(sizeof(bl));
   //s->name=e;
   //int n=sizeof(e)/sizeof(char);
   s->name=(char*)malloc(i*sizeof(char));
   //printf("%d",n);
   //for(int j=0;j<n;j++)
   	//  s->name[j]=e[j];
   strcpy(s->name,e);
   s->add=++b;
   s->leibie=5;
   s->next=p->next;
   p->next=s;
   return TRUE;
}
int blfind(bllist bll,char* e,int *a,int *l)
{
	bllist p;
    p=bll->next;
	while(p)
	{
		if(!strcmp(p->name,e))
		{
			*a=p->add;
		    *l=p->leibie;
			return TRUE;
		}
		p=p->next;
	}
	return  FALSE;
}
string blfind1(bllist bll,int a)
{
	bllist p;
	p=bll->next;
	while(p)
	{
		if(a==p->add)
		{
			return p->name;
		}
		p=p->next;
	}
	return "";
}
int check1(char ch,int *a)
{ 
	int i;
	for(i=0;i<20;i++)
		if(wordyun[i].suan==ch)
		{
			*a=i;
			return TRUE;
		}
	return FALSE;
}
int check2(char ch,int *a)
{ 
	int i;
	for(i=0;i<7;i++)
		if(wordjie[i].jie==ch)
		{
			*a=i;
			return TRUE;
		}
     return FALSE;
}
int check3(char *ch,int *a)
{ 
	int i;
	for(i=0;i<32;i++)
		if(!strcmp(guanbao[i].zifu,ch))
		{
			*a=i;
			return TRUE;
		}
	return FALSE;
}

string xbfind(int a,int b,cllist cll,bllist bll,guanbaozi *guanbao,yunsuanfu *wordyun,jiefu *wordjie)
{
	char ch[2];
	float fl;
	ch[1]=0;
	switch(a)
	{
	case 1:
        return guanbao[b].zifu;
	case 2:
        ch[0]=wordyun[b].suan;
        return ch;
	case 3:
		ch[0]=wordjie[b].jie;
        return ch;
	case 4:
        fl=clfind1(cll,b);
		char ch[6];
	    _gcvt(fl,6,ch);
		return ch;
	case 5:
        return blfind1(bll,b);
	}
	return "";
}
bool isstrnum(string str)
{   
	int j=str.size();
	for(int i=0;i<j;i++)
		if(!isdigit(str[i])&&(str[i]!='.'))
			return false;
	return true;
}
float strtofloat2(string str)
{
    float m_fl=0.0;
	int j=str.size();
    for(int i=0;i<j;i++)
    m_fl=m_fl*10+str[i]-48;
	return m_fl;
}
float strtofloat1(string str)
{
	int local=str.find('.');
	if(local==-1)
		return strtofloat2(str);
    else
	{
        string str1;
		int num=str.size()-local-1;
		str1=str.substr(0,local);
		str=str.substr(local+1,num);
		return(strtofloat2(str1)+strtofloat2(str)/pow(10,num));
	}
}
int actionAndgoto[18][12]={
	{0,1,0,0,0,0,0,0,0,0,0,0},
	{1,0,0,0,0,0,0,0,0,0,0,0},
	{0,1,0,0,0,0,1,0,0,1,1,1},
	{0,0,1,1,0,0,0,0,3,0,0,0},
	{0,0,2,2,1,1,0,2,2,0,0,0},
	{0,0,2,2,2,2,0,2,2,0,0,0},
	{0,1,0,0,0,0,1,0,0,1,1,1},
	{0,0,2,2,2,2,0,2,2,0,0,0},
	{0,1,0,0,0,0,1,0,0,0,1,1},
	{0,1,0,0,0,0,1,0,0,0,1,1},
	{0,1,0,0,0,0,1,0,0,0,0,1},
	{0,1,0,0,0,0,1,0,0,0,0,1},
	{0,0,1,1,0,0,0,1,0,0,0,0},
	{0,0,2,2,1,1,0,2,2,0,0,0},
	{0,0,2,2,1,1,0,2,2,0,0,0},
	{0,0,2,2,2,2,0,2,2,0,0,0},
	{0,0,2,2,2,2,0,2,2,0,0,0},
	{0,0,2,2,2,2,0,2,2,0,0,0}};
	
int setchartoint(char ch)
{
  switch(ch) 
  {
    case '=':return 0;
	case 'i':return 1;
	case '+':return 2;
	case '-':return 3;
	case '*':return 4;
	case '/':return 5;
    case '(':return 6;
    case ')':return 7;
	case ';':return 8;
	case 'E':return 9;
	case 'T':return 10;
	case 'F':return 11;
	default:
		return -1;
  }
}
int gotofind(int t)
{
  switch(t)
  {
    case 209:
	  return 3;
    case 210:
    case 610:
	  return 4;
    case 211:
    case 611:
    case 811:
    case 911:
	  return 5;
    case 609:
	  return 12;
    case 810:
	  return 13;
    case 910:
	  return 14;
    case 1011:
	  return 15;
	case 1111:
	  return 16;
	default:
		return -1;
  }
}
int doforpush(stack<int>& status_stack,stack<pair<char,string> >& sysm_stack,stack<string>& outputstack,vector<string> &tempstring,int t,pair<char,string> *str,ofstream &m_out)
{ 
  int i=0;
  string tempstr,tempstr1,tempstr2;
  switch(t)
  {
  case 100:
	  status_stack.push(2);
      sysm_stack.push(*str);
      outputstack.push(str->second);
	  return 1;
  case 1:
	  status_stack.push(1);
      sysm_stack.push(*str);
	  outputstack.push(str->second);
	  return 1;
  case 201:
  case 601:
  case 801:
  case 901:
  case 1001:
  case 1101:
	  status_stack.push(7);
      sysm_stack.push(*str);
	  return 1;
  case 302:
  case 1202:
	  status_stack.push(8);
      sysm_stack.push(*str);
	  return 1;
  case 303:
  case 1203:
	  status_stack.push(9);
      sysm_stack.push(*str);
	  return 1;
  case 404:
  case 1304:
  case 1404:
	  status_stack.push(10);
      sysm_stack.push(*str);
	  return 1;
  case 405:
  case 1305:
  case 1405:
	  status_stack.push(11);
      sysm_stack.push(*str);
	  return 1;
  case 206:
  case 606:
  case 806:
  case 906:
  case 1006:
  case 1106:
	  status_stack.push(6);
      sysm_stack.push(*str);
	  return 1;
  case 1207:
	  status_stack.push(17);
      sysm_stack.push(*str);
	  return 1;
  case 402:
  case 403:
  case 407:
  case 408:
	  sysm_stack.pop();
	  sysm_stack.push(pair<char,string>('E',""));
      status_stack.pop();
	  status_stack.push(gotofind(status_stack.top()*100+setchartoint('E')));
	  return 2;
  case 502:
  case 503:
  case 504:
  case 505:
  case 507:
  case 508:
	  sysm_stack.pop();
	  sysm_stack.push(pair<char,string>('T',""));
      status_stack.pop();
	  status_stack.push(gotofind(status_stack.top()*100+setchartoint('T')));
	  return 2;
  case 702:
  case 703:
  case 704:
  case 705:
  case 707:
  case 708:
	  tempstr=sysm_stack.top().second;
      outputstack.push(tempstr);
	  sysm_stack.pop();
	  sysm_stack.push(pair<char,string>('F',""));
      status_stack.pop();
	  status_stack.push(gotofind(status_stack.top()*100+setchartoint('F')));
	  return 2;
  case 1302:

⌨️ 快捷键说明

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