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

📄 e.cpp

📁 编译原理 词法分析 编译原理 词法分析
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
int count=0;              /*分解的产生式的个数*/
int number;               /*所有终结符和非终结符的总数*/
char start;               /*开始符号*/
char termin[50];          /*终结符号*/
char non_ter[50];         /*非终结符号*/
char v[50];               /*所有符号*/
char left[50];            /*左部*/
char right[50][50];       /*右部*/
char first[50][50],follow[50][50];       /*各产生式右部的FIRST和左部的FOLLOW集合*/
char first1[50][50];      /*所有单个符号的FIRST集合*/
char select[50][50];      /*各单个产生式的SELECT集合*/
char f[50],F[50];         /*记录各符号的FIRST和FOLLOW是否已求过*/
char empty[20];           /*记录可直接推出^的符号*/
char TEMP[50];            /*求FOLLOW时存放某一符号串的FIRST集合*/
int validity=1;           /*表示输入文法是否有效*/
int ll=1;                 /*表示输入文法是否为LL(1)文法*/
int M[20][20];            /*分析表*/
char choose;              /*用户输入时使用*/
char empt[20];            /*求_emp()时使用*/
char fo[20];              /*求FOLLOW集合时使用*/



typedef struct
{
	char *base, *top;
	int stacksize;
}SqStack;
int GA(char intion[]);
void InitStack(SqStack &s,int n);                                            //创建空栈
void Push(SqStack &s,char e);                                                //进栈
char GetTop(SqStack &s);                                                     //出栈
int son()
{
	int i,j=0,length,kh=0;
	char oution[100];
	char intion[100];
	char judge;
	SqStack s;
	InitStack(s,50);
	Push(s,';');
	cout<<"输入要测试赋值语句,并以“;”结束 "<<endl;
	cin.get(intion,100);
	cout<<endl;
	if(GA(intion)==0)
	{
		cout<<"This sentence is illegal!"<<endl;
		return 0;
	}
	for(i=0;i<=99;i++)
	{
		if(intion[i]=='('||intion[i]==')') kh++;
		if(intion[i]==';')
		{
			length=i-1;
			length=length-kh;
			break;
		}
	}	
	oution[0]=intion[0];
	for(i=1;i<=99;i++)//取等号
	{
		if(intion[i]==' '||intion[i]==','||intion[i]=='+'||intion[i]=='*')
		{
			cout<<"error"<<endl;
			cout<<"This sentence is illegal!"<<endl;
			return 0;
			break;
		}
		if(intion[i]=='=')
		{
			Push(s,'=');
			j=i;
			break;
		}
		oution[i]=intion[i];
	}
	for(i=j+1;i<=99;i++)
	{
		if(intion[i]==';')	break;
		else if(intion[i]=='='||intion[i]==','||intion[i]==' ')
		{
			cout<<"error"<<endl;
			cout<<"This sentence is illegal!"<<endl;
			return 0;
			break;
		}
		else if(intion[i]=='+'||intion[i]=='-')
		{
			judge=GetTop(s);
			while(judge=='*'||judge=='/'||judge=='+'||judge=='-')
			{
				oution[j]=judge;
				j++;
				s.top--; 
				judge=GetTop(s);
			}
			Push(s,intion[i]);
		}
		else if(intion[i]=='*'||intion[i]=='/')
		{
			judge=GetTop(s);
			while(judge=='*'||judge=='/')
			{
				oution[j]=judge;
				j++;
				s.top--; 
				judge=GetTop(s);
			}
			Push(s,intion[i]);
		}
		else if(intion[i]=='(')
		{
			Push(s,intion[i]);
		}
		else if(intion[i]==')')
		{
			judge=GetTop(s);
			while(judge=='+'||judge=='-'||judge=='*'||judge=='/')
			{
				oution[j]=judge;
				j++;
				s.top--;
				judge=GetTop(s);
			}			
			if(judge=='(')
			{
				s.top--;
			}
			else Push(s,intion[i]);
		}
		else
		{
			oution[j]=intion[i];
			j++;
		}
	}
	while(GetTop(s)!=';')
	{
		if(GetTop(s)!='(')
		{
			oution[j]=GetTop(s);
			s.top--;
			j++;
		}
        else if(GetTop(s)=='(') s.top--;
	}
	cout<<"the answer is :"<<endl;
	for(i=0;i<=length;i++)
	{
		cout<<oution[i];
	}cout<<endl;
	return 0;
}
void InitStack(SqStack &s,int n)
{
	s.base=(char *)malloc(n*sizeof(int));
	if(!s.base)exit(false);
	s.stacksize=n;
	s.top=s.base;
}
void Push(SqStack &s,char e)
{
	*s.top++=e;
}
char GetTop(SqStack &s)
{
	char e;
	if(s.top==s.base) return false;
	e=*(s.top-1);
	return e;
}
int GA(char intion[])
{
	int i,zkh=0,ykh=0;
	if(intion[0]>='0'&&intion[0]<='9')
		{
			cout<<"error0"<<endl;
			return 0;
		}
	if(intion[0]==' '||intion[0]=='+'||intion[0]=='*')
		{
			cout<<"error1"<<endl;
			return 0;
		}
	for(i=1;i<=99;i++)
	{
		if(intion[i]=='(') zkh++;
		else if(intion[i]==')') ykh++;
	}
	if(zkh!=ykh)
	{
		cout<<"error"<<endl;
		return 0;
	}
	zkh=0;
	ykh=0;
	for(i=1;i<=99;i++)
	{
		if(intion[i]=='+'||intion[i]=='-'||intion[i]=='*'||intion[i]=='/')
		{
			if(intion[i-1]=='='||intion[i-1]=='+'||intion[i-1]=='-'||intion[i-1]=='*'||intion[i-1]=='/'||intion[i-1]=='(')
			{
				cout<<"error2"<<endl;
				return 0;
				break;
			}
		}
		else if(intion[i]=='(')
		{
			zkh++;
			if(intion[i-1]=='='||intion[i-1]=='+'||intion[i-1]=='-'||intion[i-1]=='*'||intion[i-1]=='/'||intion[i-1]=='(')
			{
			}
			else
			{
				cout<<"error3"<<endl;
				return 0;
				break;
			}
		}
		else if(intion[i]==')')
		{
			ykh++;
			if(zkh<ykh)
			{
				cout<<"error4"<<endl;
				return 0;
				break;
			}
			if(intion[i-1]=='('||intion[i-1]=='='||intion[i-1]=='+'||intion[i-1]=='-'||intion[i-1]=='*'||intion[i-1]=='/')
			{
				cout<<"error5"<<endl;
				return 0;
				break;
			}
		}
		else if(intion[i]!=';')
		{
			if(intion[i-1]==')')
			{
				cout<<"error6"<<endl;
				return 0;
				break;
			}
		}
	}
	return 1;
}



/*******************************************
 判断一个字符是否在指定字符串中
********************************************/
int in(char c,char *p)
{
 int i;
 if(strlen(p)==0)
  return(0);
 for(i=0;;i++)
 { 
  if(p[i]==c)
   return(1);       /*若在,返回1*/
  if(i==strlen(p))
      return(0);       /*若不在,返回0*/
 }
}
/*******************************************
 得到一个不是非终结符的符号
********************************************/
char c()
{
 char c='A';
    while(in(c,non_ter)==1)
  c++;
 return(c);
}
/*******************************************
 分解含有左递归的产生式
********************************************/
void recur(char *point)
{                     /*完整的产生式在point[]中*/
    int j,m=0,n=3,k;
 char temp[20],ch;
 ch=c();           /*得到一个非终结符*/
 k=strlen(non_ter);
 non_ter[k]=ch;
 non_ter[k+1]='\0';
 for(j=0;j<=strlen(point)-1;j++)
 { 
  if(point[n]==point[0])
  {                          /*如果‘|’后的首符号和左部相同*/
   for(j=n+1;j<=strlen(point)-1;j++)
   {
       while(point[j]!='|'&&point[j]!='\0')
        temp[m++]=point[j++];
    left[count]=ch;
    memcpy(right[count],temp,m);
    right[count][m]=ch;
    right[count][m+1]='\0';
    m=0;
    count++;
    if(point[j]=='|')
    {
     n=j+1;
     break;
    }
   }
  }
  else
  {                          /*如果‘|’后的首符号和左部不同*/
   left[count]=ch;
   right[count][0]='^';
   right[count][1]='\0';
   count++;
   for(j=n;j<=strlen(point)-1;j++)
   {
       if(point[j]!='|')
           temp[m++]=point[j];
       else
    {
        left[count]=point[0];
        memcpy(right[count],temp,m);
        right[count][m]=ch;
        right[count][m+1]='\0';
     printf(" count=%d ",count);
     m=0;
        count++;
    }
   }
            left[count]=point[0];
      memcpy(right[count],temp,m);
      right[count][m]=ch;
        right[count][m+1]='\0';
   count++;
      m=0;
  }
 }
}
/*******************************************
 分解不含有左递归的产生式
********************************************/
void non_re(char *point)
{
    int m=0,j;
 char temp[20];
 for(j=3;j<=strlen(point)-1;j++)
 {
     if(point[j]!='|')
      temp[m++]=point[j];
  else
  {
      left[count]=point[0];
      memcpy(right[count],temp,m);
      right[count][m]='\0';
   m=0;
   count++;
  }
 }
    left[count]=point[0];
    memcpy(right[count],temp,m);
    right[count][m]='\0';
    count++;
 m=0;
}
/*******************************************
 读入一个文法
********************************************/
char grammer(char *t,char *n,char *left,char right[50][50])
{
 char vn[50],vt[50];
 char s;
 char p[50][50];
 int i,j,k;
 printf("\n请输入文法的非终结符号串:");
    scanf("%s",vn);
 getchar();
    i=strlen(vn);
    memcpy(n,vn,i);
 n[i]='\0';
 printf("请输入文法的终结符号串:");
    scanf("%s",vt);
 getchar();
    i=strlen(vt);
    memcpy(t,vt,i);
 t[i]='\0';
    printf("请输入文法的开始符号:");
 scanf("%c",&s);
 getchar();
 printf("请输入文法产生式的条数:");
    scanf("%d",&i);
 getchar();
    for(j=1;j<=i;j++)
 {
  printf("请输入文法的第%d条(共%d条)产生式:",j,i);
  scanf("%s",p[j-1]);
        getchar();
 }
    for(j=0;j<=i-1;j++)
  if(p[j][1]!='-'||p[j][2]!='>')
  { printf("\ninput error!");
      validity=0;
   return('\0');
        }            /*检测输入错误*/
   for(k=0;k<=i-1;k++)
   {                        /*分解输入的各产生式*/
        if(p[k][3]==p[k][0])
            recur(p[k]);
  else
      non_re(p[k]);
 }
 return(s);
}
/*******************************************
 将单个符号或符号串并入另一符号串
********************************************/
void merge(char *d,char *s,int type)
{                 /*d是目标符号串,s是源串,type=1,源串中的‘ ^ ’一并并入目串;
                type=2,源串中的‘ ^ ’不并入目串*/
    int i,j;
 for(i=0;i<=strlen(s)-1;i++)
 {
        if(type==2&&s[i]=='^')
   ;
  else
  {
   for(j=0;;j++)
   {
       if(j<strlen(d)&&s[i]==d[j])
       break;
                if(j==strlen(d))
    {
        d[j]=s[i];
        d[j+1]='\0';
        break;
    }
   }
  }
 }
}
/*******************************************
 求所有能直接推出^的符号
********************************************/
void emp(char c)
{                   /*即求所有由‘ ^ ’推出的符号*/
 char temp[10];
 int i;
 for(i=0;i<=count-1;i++)
 {
  if(right[i][0]==c&&strlen(right[i])==1)
  {
   temp[0]=left[i];
   temp[1]='\0';
   merge(empty,temp,1);
   emp(left[i]);
  }
 }
}
/*******************************************
 求某一符号能否推出‘ ^ ’
********************************************/
int _emp(char c)
{                  /*若能推出,返回1;否则,返回0*/
 int i,j,k,result=1,mark=0;
 char temp[20];
 temp[0]=c;
 temp[1]='\0';
 merge(empt,temp,1);
 if(in(c,empty)==1)
  return(1);
 for(i=0;;i++)
 {
  if(i==count)
            return(0);
  if(left[i]==c)         /*找一个左部为c的产生式*/
  {
            j=strlen(right[i]);    /*j为右部的长度*/
   if(j==1&&in(right[i][0],empty)==1)
       return(1);
   else if(j==1&&in(right[i][0],termin)==1)
    return(0);
   else 
   {
                for(k=0;k<=j-1;k++)
                    if(in(right[i][k],empt)==1)
      mark=1;
    if(mark==1)
     continue;
    else
                {
     for(k=0;k<=j-1;k++)

⌨️ 快捷键说明

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