#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <fstream>
#include <iostream>
using namespace std;
ifstream infile("in.txt",ios::in);
ofstream outfc("cifafenxi.txt",ios::out);
ofstream outfy("yufafenxi.txt",ios::out);
#define STACKSIZE 100
#define BUFFSIZE 100
#define NULL 0
struct structure
{
int p;
char*adr;
}Buff[BUFFSIZE], TheStack[STACKSIZE];
int Top;
char middle[20];
int n=0;
int Tn=1;
int t=0;
typedef enum youxianguanxi{ ER=0, GT, LT, EQ };
youxianguanxi PreTable[17][17];
void Precedence()
{int i,j;
for(i=0;i<=16;i++)
for(j=0;j<=16;j++)
PreTable[i][j]=ER;
PreTable[1][0]=LT;
for(i=0;i<=8;i++)
{
PreTable[i][8]=LT;
PreTable[i][10]=LT;
PreTable[i][11]=LT;
PreTable[i][14]=LT;
}
PreTable[1][8]=ER;
PreTable[1][11]=ER;
PreTable[1][14]=ER;
PreTable[6][14]=PreTable[7][14]=EQ;
PreTable[1][12]=PreTable[0][13]=EQ;
PreTable[0][15]=LT;
PreTable[2][15]=PreTable[3][15]=PreTable[4][15]=PreTable[5][15]=PreTable[8][15]=EQ;
for(i=9;i<=11;i++)
for(j=1;j<8;j++)
PreTable[i][j]=GT;
PreTable[9][5]=PreTable[11][5]=ER; PreTable[10][5]=EQ;
PreTable[9][9]=PreTable[10][9]=PreTable[11][9]=GT;
PreTable[9][16]=PreTable[10][16]=PreTable[11][16]=GT;
PreTable[12][16]=GT;
PreTable[13][1]=EQ;
PreTable[14][1]=PreTable[14][2]=PreTable[14][3]=PreTable[14][4]=GT;
PreTable[14][6]=PreTable[14][7]=EQ;
PreTable[14][9]=PreTable[14][16]=GT;
PreTable[15][1]=PreTable[15][16]=GT;
PreTable[15][2]=PreTable[15][3]=PreTable[15][4]=PreTable[15][9]=EQ;
PreTable[16][0]=PreTable[16][10]=LT;
}
void InitStack();
bool Push(int t,char*addr);
bool Pop(int c);
void InitStack()
{
Top = 0;
Push(16,NULL);
}
bool Push(int i,char*pt)
{
if (Top < STACKSIZE)
{
TheStack[Top].p= i;
TheStack[Top++].adr=pt;
return true;
}
else
return false;
}
structure Pop()
{
structure c;
if (Top > 0)
{
c = TheStack[--Top];
return c;
}
}
structure GetValue()
{
if (Top > 0)
return TheStack[Top-1];
}
void PrintStack()
{
int i;
for (i=0; i<Top; i++)
switch (TheStack[i].p)
{case 0 :
printf("while ");
outfy<<"while ";
break;
case 1 :
printf("do ");
outfy<<"do ";
break;
case 2 :
printf("> ");
outfy<<"> ";
break;
case 3 :
printf("< ");
outfy<<"> ";
break;
case 4 :
printf("== ");
outfy<<"== ";
break;
case 5 :
printf("= ");
outfy<<"= ";
break;
case 7:
if(t==1)
{
printf("/ ");
outfy<<"/ ";
}
else
{
printf("* ");
outfy<<"* ";
}
break;
case 6 :
if(t==1)
{
printf("- ");
outfy<<"- ";
}
else
{
printf("+ ");
outfy<<"+ ";
}
break;
case 10 :
printf("i ");
outfy<<"i ";
break;
case 11 :
printf("d ");
outfy<<"d ";
break;
case 12 :
printf("S ");
outfy<<"S ";
break;
case 13 :
printf("C ");
outfy<<"C ";
break;
case 14 :
printf("E ");
outfy<<"E ";
break;
case 15 :
printf("F ");
outfy<<"F ";
break;
default :
printf("# ");
outfy<<"# ";
}
printf("\n");
outfy<<endl;
}
void print(char*p)
{if(p!=NULL)
if(*p=='#')
{
p++;
printf("T%c",*p);
}
else if(isdigit(*p))
{
while(isdigit(*p))
printf("%c",*p++);
}
else
{
while(isalnum(*p))
printf("%c",*p++);
}
else
printf("?");
}
void Print(char*p)
{if(p!=NULL)
if(*p=='#')
{
p++;
outfc<<*p;
}
else if(isdigit(*p))
{
while(isdigit(*p))
outfc<<*p++;
}
else
{
while(isalnum(*p))
outfc<<*p++;
}
else
outfc<<"?";
}
youxianguanxi GetPrecedence(const int i, const int j)
{
if (i!=-1 && j!=-1)
return PreTable[i][j];
}
void Reduce()
{ structure outstring[10];
int len;
int c;
len=1;
outstring[len].p=GetValue().p;
outstring[len].adr=Pop().adr;
c=GetValue().p;
while (GetPrecedence(c,outstring[len].p)== EQ)
{
outstring[++len].p=GetValue().p;
outstring[len].adr=Pop().adr;
c=GetValue().p;
if(len==3&&outstring[1].p==14&&outstring[3].p==14)
break;
}
if(len==1&&(outstring[1].p==10||outstring[1].p==11))
{
print(outstring[1].adr);
Push(14,outstring[1].adr);
}
else if (len==1&&outstring[1].p==14)
Push(15,outstring[1].adr);
else if (len==3&&outstring[1].p==14&&outstring[3].p==14)
{
middle[n++]='#';
middle[n++]=Tn++;
Push(15,&middle[n-2]);
}
else if (len==3&&outstring[1].p==9)
Push(14,outstring[1].adr);
else if (len==4&&outstring[1].p==12)
Push(12,NULL);
else if (len==3&&outstring[1].p==15&&outstring[3].p==10)
{
Push(12,NULL);
}
else if (len>=3&&outstring[len].p==15)
{
middle[n++]='#';
middle[n++]=Tn++;
Push(13,&middle[n-2]);
}
else
Push(-1,NULL);
}
bool Syntax()
{
int ip = 0;
printf("语法分析过程为:\n");
InitStack();
while (true)
{
if (GetValue().p==12 && Buff[ip].p==16&&Top==2)
{
printf("语法分析正确结束!\n");
return true;
}
else if (GetValue().p==-1)
{
printf("语法错误!\n");
return false;
}
else
{
switch (GetPrecedence(GetValue().p, Buff[ip].p))
{
case LT:
case EQ:
Push(Buff[ip].p,Buff[ip].adr);
ip++;
PrintStack();
break;
case GT:
Reduce();
PrintStack();
break;
default:
printf("语法错误!\n");
return false;
}
}
}
}
void Lexical()
{char*pch,str[100],*pstr,*pt;
char ch[200]={'\0'};
int length;
int i=0,u=-1;
while(!infile.eof())
{
u++;
ch[u]=infile.get();
}
pch=ch;
length=strlen(ch);
pch=ch;
printf("\n词法分析结果是:\n");
while(pch<=ch+length)
{pstr=str;
while(*pch==' '||*pch=='('||*pch==')') pch++;
pt=pch;
if(isalpha(*pch))
{
while(isalnum(*pch))
{
*pstr=*pch;
pstr++;
pch++;
}
*pstr='\0';
if(strcmp(str,"while")==0)
{
printf("关键字while ");
outfc<<"关键字while "<<endl;
Buff[i].p=0;
Buff[i++].adr=NULL;
}
else if(strcmp(str,"do")==0)
{
printf("关键字do ");
outfc<<"关键字do "<<endl;
Buff[i].p=1;
Buff[i++].adr=NULL;
}
else
{
printf("标识符 ");
outfc<<"标识符 ";
printf(str);
outfc<<str<<endl;
Buff[i].p=10;
Buff[i++].adr=pt;
}
}
else if(isdigit(*pch))
{
pt=pch;
printf("数字");
outfc<<"数字";
while(isdigit(*pch))
{
printf("%c",*pch);
outfc<<*pch;
pch++;
}
outfc<<endl;
printf(" ");
outfc<<" ";
Buff[i].p=11;
Buff[i++].adr=pt;
}
if(*pch=='=')
{
if(*(pch+1)=='=')
{
printf("等于==");
outfc<<"等于=="<<endl;
printf(" ");
pch++; pch++;
Buff[i].p=4;
Buff[i++].adr=NULL;
}
else
{printf("赋值=");
outfc<<"赋值="<<endl;
printf(" ");
pch++;
Buff[i].p=5;
Buff[i++].adr=NULL;
}
}
else if(*pch=='*')
{
printf("乘号* ");
outfc<<"乘号* "<<endl;
pch++;
Buff[i].p=7;
Buff[i++].adr=NULL;
}
else if(*pch=='+')
{
printf("加号+ ");
outfc<<"加号+ "<<endl;
pch++;
Buff[i].p=6;
Buff[i++].adr=NULL;
}
else if(*pch=='-')
{
t=1;
printf("减号- ");
outfc<<"减号- "<<endl;
pch++;
Buff[i].p=6;
Buff[i++].adr=NULL;
}
else if(*pch=='/')
{
t=1;
printf("除号/ ");
outfc<<"除号/ "<<endl;
pch++;
Buff[i].p=7;
Buff[i++].adr=NULL;
}
else if(*pch =='<')
{
printf("小于号< ");
outfc<<"小于号< "<<e
评论8
最新资源